Please read this disclaimer before proceeding:
�This document is confidential and intended solely for the educational purpose of RMK Group of Educational Institutions. If you have received this document through email in error, please notify the system manager. This document contains proprietary information and is intended only to the respective group / learning community as intended. If you are not the addressee you should not disseminate, distribute or copy through e-mail. Please notify the sender immediately by e-mail if you have received this document by mistake and delete this document from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.
20CS302 �Object Oriented Programming��Unit III
Department : CSE, ADS, IT and EIE � Batch/Year :2020-2024/II � Created by : Dr. C. Geetha
Mr. S.VijayaKumar
Ms.A.Jasmine Gilda
Ms. S. Sajithra
Date : 01-10-2021
�
5
S. No. | Topic | Page No. |
1 | Course Objectives | 6 |
2 | Pre-Requisites | 7 |
3 | Syllabus | 8 |
4 | Course outcomes | 9 |
5 | CO- PO/PSO Mapping | 10 |
6 | Lecture Plan | 11 |
7 | Activity based Learning | 12 |
8 | Lecture Notes | 16 |
9 | Assignments | 78 |
10 | Part A Questions With Answers | 79 |
11 | Part B Questions | 87 |
12 | Online Certifications | 88 |
13 | Real Time Applications | 89 |
14 | Contents Beyond the Syllabus | 90 |
15 | Assessment Schedule | 92 |
16 | Text Books & Reference Books | 93 |
17 | Mini Project suggestions | 94 |
Table Of Contents
Course Objectives
3. PRE REQUISITES
7
20CS302 – OBJECT ORIENTED PROGRAMMING
20GE101 – PROBLEM SOLVING AND C PROGRAMMING
Syllabus
20CS302 | OBJECT ORIENTED PROGRAMMING | L T P | C |
| | 3 0 0 | 3 |
UNIT I INTRODUCTION TO OOP AND JAVA FUNDAMENTALS 9
An Overview of Java - Data Types, Variables, and Arrays – Operators - Control Statements – Class Fundamentals – Declaring objects – Methods – Constructors – this keyword - Overloading methods - Overloading constructors - Access Control – Static – Final.
UNIT II INHERITANCE, INTERFACES AND EXCEPTION HANDLING 9
Inheritance: Inheritance basics, Using super, Method Overriding, Using Abstract Classes, Using final with Inheritance – Package and Interfaces: Packages, Packages and member access, Importing Packages, Interfaces, Static Methods in an Interface – Exception Handling: Exception-Handling Fundamentals, Exception Types, Uncaught Exceptions, Using try and catch, Multiple catch Clauses, Nested try Statements, throw, throws, finally, Java’s Built-in Exceptions.
UNIT III MULTITHREADING, I/O AND GENERIC PROGRAMMING 9
Multithreaded Programming: Creating a Thread, Thread Priorities, Synchronization, Interthread Communication – I/O: I/O Basics, Reading Console Input, Writing Console Output, Reading and Writing Files – Generics: Introduction, Generic class, Bounded Types, Generic Methods, Generic Interfaces, Generic Restrictions.
UNIT IV STRING HANDLING AND COLLECTIONS 9
Lambda Expressions - String Handling – Collections: The Collection Interfaces, The Collection Classes – Iterator – Map - Regular Expression Processing.
UNIT V EVENT DRIVEN PROGRAMMING 9
Event Handling - Introducing the AWT: Working with Windows, Graphics, and Text - Using AWT Controls, Layout Managers, and Menus - Introducing Swing - Exploring Swing.
Course Outcomes
At the end of this course, the students will be able to:
CO1: Explain the object oriented programming concepts and fundamentals of Java
CO2: Develop Java programs with the packages, inheritance, interfaces and exceptions
CO3: Build Java applications with I/O streams, threads and generics classes
CO4: Apply strings and collections in applications
CO5: Develop interactive Java applications using swings and event handling mechanism
CO-PO/PSO Mapping
| |||||||||||||||
COs | POs/PSOs | ||||||||||||||
PO 1 | PO 2 | PO 3 | PO 4 | PO 5 | PO 6 | PO 7 | PO 8 | PO 9 | PO1 0 | PO1 1 | PO1 2 | PSO 1 | PSO 2 | PS O 3 | |
CO1 | 3 | 3 | 3 | - | - | - | - | - | - | - | - | - | 3 | 2 | 2 |
CO2 | 3 | 2 | 2 | - | - | - | - | - | - | - | - | - | 3 | 2 | 2 |
CO3 | 3 | 2 | 2 | - | - | - | - | - | - | - | - | - | 3 | 2 | 2 |
CO4 | 3 | 2 | 2 | - | - | - | - | - | - | - | - | - | 3 | 2 | 2 |
CO5 | 3 | 2 | 2 | - | - | - | - | - | - | - | - | - | 3 | 2 | 2 |
| |||||||||||||||
Lecture Plan
UNIT - II | ||||||
S. No. | Topic | Scheduled Date | Actual Date of Completion | CO | Mode of Delivery | Taxonomy Level |
1 | Multithreaded Programming, Thread Priorities | 27.09.2021 | | CO3 | Video/ PPT | K2 |
2 | Creating a Thread | 28.09.2021 | | CO3 | PPT/ Demo | K3 |
3 | Synchronization, Interthread Communication | 29.09.2021 | | CO3 | PPT/ Demo | K3 |
4 | I/O: I/O Basics | 30.09.2021 | | CO3 | PPT/ Demo | K3 |
5 | Reading Console Input, Writing Console Output | 01.10.2021 | | CO3 | PPT/ Demo | K3 |
6 | Reading and Writing Files | 04.10.2021 | | CO3 | PPT/ Demo | K3 |
7 | Generic Programming Generic classes –Bounded Types | 05.10.2021 | | CO3 | PPT/ Demo | K3 |
8 | Generic methods Generic Interfaces | 06.10.2021 | | CO3 | PPT/ Demo | K3 |
9 | Generic Restrictions | 07.10.2021 | | CO3 | PPT/ Demo | K3 |
| ||||||
ACTIVITY BASED LEARNING – UNIT III
Complete The Puzzle:
https://crosswordlabs.com/view/unit-3-oop-2
ACTIVITY BASED LEARNING – UNIT IV
QUIZ- LINKS
Multithreading
Generics in Java
�ACTIVITY BASED LEARNING – UNIT III
Test Yourself:
1.Thread priority in Java is?�a) Integer�b) Float�c) double�d) long
Ans :a
2.What will happen if two thread of the same priority are called to be processed simultaneously?�a) Anyone will be executed first lexographically�b) Both of them will be executed simultaneously�c) None of them will be executed�d) It is dependent on the operating system
Ans:d
3.What will be the output of the following Java code?
classmultithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
System.out.println(t);
}
}
Ans: Thread[main,5,main]
�ACTIVITY BASED LEARNING – UNIT IV
4. Which of the following cannot be Type parameterized?�a) Overloaded Methods�b) Generic methods�c) Class methods�d) Overriding methods
Ans:a
5. Which of these instance cannot be created?�a) Integer instance�b) Generic class instance�c) Generic type instance�d) Collection instances
Ans:c
UNIT – III�MULTITHREADING,I/O AND GENERIC PROGRAMMING
Thread:
A thread is referred as a light weight smallest part of a process. Threads are independent because all the threads follow a separate path of execution within the program. Each thread has its own program counter, stack and local variables. It uses a shared memory area, files and per-process state.
Multithreading
The process of executing multiple threads simultaneously is known as multithreading. Multithreading is a programming concept where a program (process) is divided into two or more subprograms (process), which can be implemented at the same time in parallel. A multithreaded program contains two or more parts that can run concurrently and each part can perform different task at the same time making optimal use of the available resources. Each part of such a program is called a thread, and each thread defines a separate path of execution.
Advantages of Java Multithreading
Multitasking
Multitasking is a process of executing multiple tasks simultaneously. Multitasking is when multiple processes share common processing resources such as a CPU.
Multitasking can be achieved in two ways:
Differences between Multi-threading and Multi-tasking�
Executing various jobs or process together where each process is a separate independent operation is called process-based multi-tasking. Each process has a separate address in memory and switching from one process to another requires some time for saving and loading registers and updating lists. A process is heavy weight and cost of communication between the process is high.
Executing several tasks simultaneously where each task is a separate independent part of the same program is called Thread-based multitasking and each independent part is called Thread. The main goal of multi-tasking is to make or do a better performance of the system by reducing response time. Threads share the same address space. A thread is light weight and cost of communication between the thread is low.
Life Cycle of a Thread
A thread can be in any of the following states:
(a) Thread give up its control on its own and it can happen in the following
situations:
(b) A thread is pre-empted by a higher priority thread.
Life Cycle of a Thread
The Thread life cycle is given in below Fig.4.1.
Fig.4.1 Life Cycle of a Thread
The Main() thread
Two important things to know about main thread are,
Thread Priorities
Thread priorities
Get and Set Thread Priority:
Example:
public class Demo extends Thread
{
public void run()
{
System.out.println("Inside run method");
}
Thread Priorities
public static void main(String args[])
{
Demo t1 = new Demo();
Demo t2 = new Demo();
System.out.println("t1 thread priority:"+t1.getPriority());
System.out.println("t2 thread priority:"+t2.getPriority());
t1.setPriority(2);
t2.setPriority(5);
System.out.println("t1 thread priority:"+t1.getPriority());
System.out.println("t2 thread priority:"+t2.getPriority());
System.out.println(Thread.currentThread().getName());
System.out.println("Main thread Priority:“ +Thread.currentThread(). getPriority());
Thread.currentThread().setPriority(10);
System.out.println("Main thread Priority:“ +Thread.currentThread().getPriority());
}
}
Output:
t1 thread priority:5
t2 thread priority:5
t1 thread priority:2
t2 thread priority:5
Main
Main thread priority:5
Main thread priority:10
Methods of Thread Class
Methods
Commonly used Constructors of Thread Class
Creation of Thread�
There are two ways to create a thread in Java:
1. By Implementing Runnable Interface
The easiest way to create a thread is to create a class that implements the Runnable interface. To implement Runnable, a class need only implement a single method called run().
Example:
class Demo implements Runnable
{
public void run()
{
try
{
for (int i = 4; i > 0; i--)
{
System.out.println("Child Thread:" +i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted");
}
System.out.println("Exiting Child Thread");
}
}
Creation of Thread - By Implementing Runnable Interface
public class MainThread
{
public static void main(String args[])
{
Demo d = new Demo();
Thread s = new Thread(d);
s.start();
try
{
for (int i = 4; i > 0; i--)
{
System.out.println("Main Thread:" +i);
Thread.sleep(5000);
}
}
catch (InterruptedException e)
{
System.out.println("Main interrupted");
}
System.out.println("Exiting Main Thread");
}
}
Output:
Main Thread:4
Child Thread:4
Child Thread:3
Child Thread:2
Child Thread:1
Main Thread:3
Exiting Child Thread
Main Thread:2
Main Thread:1
Exiting Main Thread
Creation of Thread – By Extending Thread Class�
2. By Extending Thread Class
The second way to create a thread is to create a new class that extends Thread, and then to create an instance of that class. The extending class must override the run( ) method, which is the entry point for the new thread. It must also call start( ) to begin execution of the new thread.
Example:
class Demo1 extends Thread
{
public void run()
{
try
{
for (int i = 4; i > 0; i--)
{
System.out.println("Child Thread:" +i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted");
}
System.out.println("Exiting Child Thread");
}
}
public class MainThread
{
public static void main(String args[])
{
Creation of Thread – By Extending Thread Class�
Demo1 d = new Demo1();
d.start();
try
{
for (int i = 4; i > 0; i--)
{
System.out.println("Main Thread:" +i);
Thread.sleep(5000);
}
}
catch (InterruptedException e)
{
System.out.println("Main interrupted");
}
System.out.println("Exiting Main Thread");
}
}
Output:
Main Thread:4
Child Thread:4
Child Thread:3
Child Thread:2
Child Thread:1
Main Thread:3
Exiting Child Thread
Main Thread:2
Main Thread:1
Exiting Main Thread
Synchronizing Threads
Java Synchronization:
Synchronization in java is the capability to control the access of multiple threads to any shared resource. Java Synchronization is a better choice if we want to allow only one thread to access the shared resource.
Need for Synchronization:
When two or more threads start within a program, there arises a situation where multiple threads try to access the same resources and finally produce erroneous and unforeseen results. For ex., if multiple threads try to write within a same file then they may corrupt the data because one of the threads can override data or while one thread is opening the same file at the same time another thread might be closing the same file. So there is a need to synchronize the action of multiple threads and make sure that only one thread can access the resource at a given point in time. This is implemented using a concept called monitors. Each object in Java is associated with a monitor, which a thread can lock or unlock. Only one thread at a time may hold a lock on a monitor. Java programming language provides a way of creating threads and synchronizing their task by using synchronized blocks.
General Syntax:
synchronized(object)
{
//statement to be synchronized
}
Synchronization is mainly used to
Synchronizing Threads
Types of Synchronization:
There are two types of synchronization
Thread Synchronization:
There are two types of thread synchronization namely mutual exclusive and inter-thread communication.
1. Mutual Exclusive helps keeping threads from interfering with one another while sharing data. This can be done by three ways in java:
Synchronized method:
Synchronized Method
Example of synchronized method
public class SynThread
{
public static void main(String args[])
{
sync s = new sync();
ThreadDemo m1 = new ThreadDemo(s, "Thread1");
ThreadDemo m2 = new ThreadDemo(s, "Thread2");
ThreadDemo m3 = new ThreadDemo(s, "Thread3");
}
}
class ThreadDemo extends Thread
{
sync s;
ThreadDemo(sync s, String str)
{
super(str);
this.s = s;
start();
}
public void run()
{
s.print(Thread.currentThread().getName());
}
}
class sync
{
public synchronized void print(String str)
{
Synchronized Method
for (int i = 0; i < 3; i++)
{
System.out.println("Started:" + str);
try
{
Thread.sleep(1000);
}
catch (Exception e)
{
}
}
}
}
Output:
Started:Thread1
Started:Thread1
Started:Thread1
Started:Thread3
Started:Thread3
Started:Thread3
Started:Thread2
Started:Thread2
Started:Thread2
Synchronized Block
Synchronized Block
Syntax for synchronized block
synchronized (object reference expression)
{
//code block
}
An example of Synchronized block is given in the below Fig.4.2.
Fig.4.2 Synchronized Block
Synchronized Block
class syncblock
{
void print(int n)
{
synchronized(this)
{ //synchronized block
for(int i=1;i<=5;i++)
{
System.out.println(n*i);
try
{
Thread.sleep(500);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
}
class Thread1 extends Thread
{
syncblock s;
Thread1(syncblock s)
{
this.s=s;
}
` public void run()
{
s.print(5);
}
}
Synchronized Block
class Thread2 extends Thread
{
syncblock s;
Thread2(syncblock s)
{
this.s=s;
}
public void run()
{
s.print(100);
}
}
public class SynchBlock
{
public static void main(String args[])
{
syncblock obj = new syncblock(); //only one object
Thread1 t1=new Thread1(obj);
Thread2 t2=new Thread2(obj);
t1.start();
t2.start();
}
}
Output:
5
10
15
20
25
100
200
300
400
500
�Static synchronization �
If you make any static method as synchronized, the lock will be on the class not on object.
Use synchronized keyword on the static method to perform static synchronization.
Example:
class staticsync
{
synchronized static void print(int n)
{
for(int i=1;i<=3;i++)
{
System.out.println(n*i);
try
{
Thread.sleep(500);
}
catch(Exception e)
{
}
}
}
}
class Thread1 extends Thread
{
public void run()
{
staticsync.print(1);
}
}
class Thread2 extends Thread
{
public void run()
{
staticsync.print(3);
}
}
Static synchronization
class Thread3 extends Thread
{
public void run()
{
staticsync.print(50);
}
}
public class TestSynch
{
public static void main(String args[])
{
Thread1 t1=new Thread1();
Thread2 t2=new Thread2();
Thread3 t3=new Thread3();
t1.start();
t2.start();
t3.start();
}
}
Output:
3
6
9
1
2
3
50
100
150
INTER-THREAD COMMUNICATION�
Definition
Methods of Object class used to implement Inter-Thread Communication
Inter-Thread Communication can be implemented by following methods of Object class
wait() Method causes current thread to release the lock and wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed. Figure 4.1 illustrates the functionality of wait() method.
Figure 2.1: wait() Method Role
Syntax:
public final void wait()throws InterruptedException
public final void wait(long timeout)throws InterruptedException
notify() method wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. Figure 4.2 illustrates the functionality of notify() method in Inter-Process Communication.
Figure 4.2: Role of notify() method
Syntax: public final void notify()
notifyAll() wakes up all the threads that called wait() on the same object.
Syntax: public void notifyAll()
Example for Inter-Thread Communication (Producer Consumer Problem)
Figure 4.3: Producer-Consumer Problem
While implementing, the following two conditions should be satisfied:
PROGRAM FOR PRODUCER CONSUMER PROBLEM IMPLEMENTATION
public class ProducerConsumerTest {
public static void main(String[] args) {
Buffer c = new Buffer();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1);
p1.start();
c1.start();
}
}
class Buffer {
private int contents;
private boolean available = false;
public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) {}
}
available = false;
notifyAll();
return contents;
}
public synchronized void put(int value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) { }
}
contents = value;
available = true;
notifyAll();
}
}
class Consumer extends Thread {
private Buffer buff;
private int number;
public Consumer( Buffer c, int number) {
buff = c;
this.number = number;
}
public void run() {
int value = 0;
for (int i = 0; i < 5; i++) {
value = buff.get();
System.out.println("Consumer got: " + value);
}
}
}
class Producer extends Thread {
private Buffer buff;
private int number;
public Producer( Buffer c, int number) {
buff = c;
this.number = number;
}
public void run() {
for (int i = 0; i < 5; i++) {
buff.put(i);
System.out.println("Producer put: " + i);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}
OUTPUT
Producer put: 0
Consumer got: 0
Producer put: 1
Consumer got: 1
Producer put: 2
Consumer got: 2
Producer put: 3
Consumer got: 3
Producer put: 4
Consumer got: 4
Input / Output Basics
Input / Output Basics
Stream
Byte Streams
Java byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams but the most frequently used classes are, FileInputStream and FileOutputStream. Following is an example which makes use of these two classes to copy an input file into an output file −
Example
import java.io.*;
public class CopyFile {
public static void main(String args[]) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("input.txt");
out = new FileOutputStream("output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
Now let's have a file input.txt with the following content −
This is test for copy file.
As a next step, compile the above program and execute it, which will result in creating output.txt file with the same content as we have in input.txt. So let's put the above code in CopyFile.java file and do the following −
$javac CopyFile.java
$java CopyFile
Character Streams
Java Byte streams are used to perform input and output of 8-bit bytes, whereas Java Character streams are used to perform input and output for 16-bit unicode. Though there are many classes related to character streams but the most frequently used classes are, FileReader and FileWriter. Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream but here the major difference is that FileReader reads two bytes at a time and FileWriter writes two bytes at a time.
import java.io.*;
public class CopyFile {
public static void main(String args[]) throws IOException {
FileReader in = null;
FileWriter out = null;
try {
in = new FileReader("input.txt");
out = new FileWriter("output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
Now let's have a file input.txt with the following content −
This is test for copy file.
As a next step, compile the above program and execute it, which will result in creating output.txt file with the same content as we have in input.txt. So let's put the above code in CopyFile.java file and do the following −
$javac CopyFile.java
$java CopyFile
Standard Streams
All the programming languages provide support for standard I/O where the user's program can take input from a keyboard and then produce an output on the computer screen.
In Java, 3 streams are created for us automatically with the console.
1) System.out: standard output stream
2) System.in: standard input stream
3) System.err: standard error stream
Let's see the code to print output and an error message to the console.
System.out.println("simple message");
int i=System.in.read();//returns ASCII code of 1st character
System.out.println((char)i);//will print the character
System.err.println("error message");
Reading and Writing Console
Reading and Writing Console�
Reading Console Input:
The preferred method of reading console input is to use a character-oriented stream.
There are three different ways for reading input from the user in the command line environment(console).
1.Using Buffered Reader Class
2. Using Scanner Class
3. Using Console Class
1.Using Buffered Reader Class
BufferedReader(Reader inputReader)
InputStreamReader(InputStream inputStream)
Because System.in refers to an object of type InputStream, it can be used for inputStream. Putting it all together, the following line of code creates a BufferedReader that is connected to the keyboard:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
After this statement executes, br is a character-based stream that is linked to the console through System.in.
import java.io.*;
public class Test
{
public static void main(String[] args) throws IOException
{
//Enter data using BufferReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = br.readLine();
System.out.println(name);
}
}
Input
Computer
Output
Computer
2. Using Scanner Class
This is probably the most preferred method to take input. The main purpose of the Scanner class is to parse primitive types and strings using regular expressions, however it is also can be used to read input from the user in the command line.
Advantages:
• Convenient methods for parsing primitives (nextInt(), nextFloat(), …) from the tokenized input.
• Regular expressions can be used to find tokens.
Example
// Java program to demonstrate working of Scanner in Java
import java.util.Scanner;
class GetInputFromUser
{
public static void main(String args[])
{
// Using Scanner for Getting Input from User
Scanner in = new Scanner(System.in);
String s = in.nextLine();
System.out.println("You entered string: "+s);
int a = in.nextInt();
System.out.println("You entered integer: "+a);
float b = in.nextFloat();
System.out.println("You entered float: "+b);
}
}
Input
Computer
12
14.3
Output
You entered string: Computer
You entered integer: 12
You entered float: 14.3
3. Using Console Class
It is a preferred way for reading user’s input from the command line. In addition, it can be used for reading password-like input without echoing the characters entered by the user; the format string syntax can also be used (like System.out.printf()).
Advantages:
•Reading password without echoing the entered characters.
•Reading methods are synchronized.
•Format string syntax can be used.
Drawback:
•Does not work in non-interactive environment (such as in an IDE).
Example
//Java program to demonstrate working of System.console(). Note that //this program does not work on IDEs as System.console() may require //console
public class Sample
{
public static void main(String[] args)
{
// Using Console to input data from user
String name = System.console().readLine();
System.out.println(name);
}
}
Writing Console Output
void write(int byteval)
class WriteConsoleOutput
{
public static void main(String args[])
{
int y;
y = 'X';
System.out.write(y);
System.out.write('\n');
}
}
Output
X
You will not often use write() to perform console output (although doing so might be useful in some situations) because print() and println() are substantially easier to use.
Reading and Writing Files
Reading and Writing Files�
Reading and Writing Files
A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.
Here is a hierarchy of classes to deal with Input and Output streams.
The two important streams are FileInputStream and FileOutputStream
FileInputStream
This stream is used for reading data from the files. Objects can be created using the keyword new and there are several types of constructors available.
Following constructor takes a file name as a string to create an input stream object to read the file −
InputStream f = new FileInputStream("C:/java/hello");
Following constructor takes a file object to create an input stream object to read the file. First we create a file object using File() method as follows −
File f = new File("C:/java/hello");
InputStream f = new FileInputStream(f);
Once you have InputStream object in hand, then there is a list of helper methods which can be used to read to stream or to do other operations on the stream.
Sr.No. | Method & Description |
1 | public void close() throws IOException{} This method closes the file output stream. Releases any system resources associated with the file. Throws an IOException. |
2 | protected void finalize()throws IOException {} This method cleans up the connection to the file. Ensures that the close method of this file output stream is called when there are no more references to this stream. Throws an IOException. |
3 | public int read(int r)throws IOException{} This method reads the specified byte of data from the InputStream. Returns an int. Returns the next byte of data and -1 will be returned if it's the end of the file. |
4 | public int read(byte[] r) throws IOException{} This method reads r.length bytes from the input stream into an array. Returns the total number of bytes read. If it is the end of the file, -1 will be returned. |
5 | public int available() throws IOException{} Gives the number of bytes that can be read from this file input stream. Returns an int. |
There are other important input streams available, for more detail you can refer to the following links −
FileOutputStream
FileOutputStream is used to create a file and write data into it. The stream would create a file, if it doesn't already exist, before opening it for output.
Here are two constructors which can be used to create a FileOutputStream object.
Following constructor takes a file name as a string to create an input stream object to write the file −
OutputStream f = new FileOutputStream("C:/java/hello")
Following constructor takes a file object to create an output stream object to write the file. First, we create a file object using File() method as follows −
File f = new File("C:/java/hello");
OutputStream f = new FileOutputStream(f);
Once you have OutputStream object in hand, then there is a list of helper methods, which can be used to write to stream or to do other operations on the stream.
There are other important output streams available, for more detail you can refer to the following links −
• ByteArrayOutputStream
• DataOutputStream
Sr.No. | Method & Description |
1 | public void close() throws IOException{} This method closes the file output stream. Releases any system resources associated with the file. Throws an IOException. |
2 | protected void finalize()throws IOException {} This method cleans up the connection to the file. Ensures that the close method of this file output stream is called when there are no more references to this stream. Throws an IOException. |
3 | public void write(int w)throws IOException{} This methods writes the specified byte to the output stream. |
4 | public void write(byte[] w) Writes w.length bytes from the mentioned byte array to the OutputStream. |
Example
Following is the example to demonstrate InputStream and OutputStream −
import java.io.*;
public class fileStreamTest {
public static void main(String args[]) {
try {
byte bWrite [] = {11,21,3,40,5};
OutputStream os = new FileOutputStream("test.txt");
for(int x = 0; x < bWrite.length ; x++) {
os.write( bWrite[x] ); // writes the bytes
}
os.close();
InputStream is = new FileInputStream("test.txt");
int size = is.available();
for(int i = 0; i < size; i++) {
System.out.print((char)is.read() + " ");
}
is.close();
} catch (IOException e) {
System.out.print("Exception");
}
}
}
The above code would create file test.txt and would write given numbers in binary format. Same would be the output on the stdout screen.
Last two classes FileReader and FileWriter are equivalent to using InputStreamReader and OutputStreamWriter with Charset.defaultCharset().name() as encoding. Default encoding (charset) may differ between various JVM’s, it is always better to use InputStreamReader and OutputStreamWriter with explicit encoding than to relay on FileReader and FileWriter classes.
class diagrams that connects classes described above with abstract classes like Reader and Writer:
Stream | Read | Write |
Byte | FileInputStream | FileOutputStream |
Character | InputStreamReader | OutputStreamWriter |
Character | FileReader | FileWriter |
9. Which of these class contains the methods print() & println()?
A. System
B. System.out
C. BUfferedOutputStream
D. PrintStream
10. Which of these methods can be used to writing console output?
A. print()
B. println()
C. write()
D. All of the mentioned
11. Which of these class is used to create an object whose character sequence is mutable?
A. String()
B. StringBuffer()
C. Both of the mentioned
D. None of the mentioned
12. Which of these method of class StringBuffer is used to reverse sequence of characters?
A. reverse()
B. reverseall()
C. Reverse()
D. reverseAll()
13. Can System.out.println() be used to write data to a file?
Yes No
import java.io.*;
class Chararrayinput
{
public static void main(String[] args)
{
String obj = "abcdef";
int length = obj.length();
char c[] = new char[length];
obj.getChars(0,length,c,0);
CharArrayReader input1 = new CharArrayReader(c);
CharArrayReader input2 = new CharArrayReader(c, 0, 3);
int i;
try
{
while ((i = input1.read()) != -1)
{
System.out.print((char)i);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
a) abc�b) abcd�c) abcde�d) abcdef
import java.io.*;
class Chararrayinput
{
public static void main(String[] args)
{
String obj = "abcdef";
int length = obj.length();
char c[] = new char[length];
obj.getChars(0, length, c, 0);
CharArrayReader input1 = new CharArrayReader(c);
CharArrayReader input2 = new CharArrayReader(c, 0, 3);
int i;
try
{
while ((i = input2.read()) != -1)
{
System.out.print((char)i);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
a) abc�b) abcd�c) abcde�d) abcdef
What will be the output of the following Java program? (Note: inputoutput.java is stored in the disk.)
import java.io.*;
class filesinputoutput
{
public static void main(String args[])
{
InputStream obj = new FileInputStream("inputoutput.java");
System.out.print(obj.available());
}
}
a) true�b) false�c) prints number of bytes in file�d) prints number of characters in the file
What will be the output of the following Java program?
import java.io.*;
public class filesinputoutput
{
public static void main(String[] args)
{
String obj = "abc";
byte b[] = obj.getBytes();
ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
for (int i = 0; i < 2; ++ i)
{
int c;
while ((c = obj1.read()) != -1)
{
if(i == 0)
{
System.out.print((char)c);
}
}
}
}
}
a) abc�b) ABC�c) ab�d) AB
GENERIC PROGRAMMING�
Generic programming enables the programmer to create classes, interfaces and methods that automatically works with all types of data(Integer, String, Float etc).
It has expanded the ability to reuse the code safely and easily.
Advantages
There are 3 main advantages of Java generics
Type-safety: We can hold only a single type of objects in generics. It doesn’t allow to store other objects
Type casting is not required: There is no need to typecast the object
Compile-Time Checking: It is checked at compile time so problem will not occur at runtime
Generic class
A generic class is a class with one or more type variables. Generics means parameterized types. Parameterized types enables to create classes, interfaces, and methods in which the type of data upon which they operate is specified as a parameter. Class, interface, or method that operates on a parameterized type is called generic, as in generic class or generic method.
Syntax for declaring a generic class:
class class-name<type-param-list >
{ // …
Syntax for declaring a reference to a generic class:
class-name<type-arg-list > var-name =new class-name<type-arg-list >(cons-arg-list);
Example Program:
// A simple generic class.
// Here, T is a type parameter that
class Gen<T>
{
T ob; // declare an object of type T
Gen(T o)
{
ob = o;
}
// Return ob.
T getob()
{
return ob;
}
// Show type of T.
void showType()
{
System.out.println("Type of T is " +ob.getClass().getName());
}
}
public class GenDemo
{
public static void main(String[] args)
{
// Create a Gen reference for Integers.
Gen<Integer> iOb= new Gen<Integer>(88);
iOb.showType();
int v = iOb.getob();
System.out.println("value: " + v);
// Create a Gen object for Strings.
Gen<String> strOb = new Gen<String> ("RMDEC");
strOb.showType();
String str = strOb.getob();
System.out.println("value: " + str);
}
}
Output:
Type of T is java.lang.Integer
value: 88
Type of T is java.lang.String
value: RMDEC
Note: Generics Work Only with Objects
When declaring an instance of a generic type, the type argument passed to the type parameter must be a class type.Primitive type, such as int or char can’t be used.
Gen<int> intOb = new Gen<int>(53); // Error, can't use primitive type
Java’s autoboxing and auto-unboxing mechanism makes the use of the type wrapper transparent.
A Generic Class with Two Type Parameters
It is possible to declare more than one type parameter in a generic type. To specify two or more type parameters, simply use a comma-separated list.
class TwoGen<T, V>
{
T ob1;
V ob2;
TwoGen(T o1, V o2)
{
ob1 = o1;
ob2 = o2;
}
void showTypes()
{
System.out.println("Type of T is “ +ob1.getClass().getName());
System.out.println("Type of V is " +ob2.getClass().getName());
}
T getob1()
{
return ob1;
}
V getob2()
{
return ob2;
}
}
public class GenDemo
{
public static void main(String[] args)
{
// Create a Gen reference for Integers.
TwoGen<Integer, String> tgObj =new TwoGen<Integer, String>
(88, " R.M.D Engineering College");
tgObj.showTypes();
int v = tgObj.getob1();
System.out.println("value: " + v);
String str = tgObj.getob2();
System.out.println("value: " + str);
}
}
Output:
Type of T is java.lang.Integer
Type of V is java.lang.String
value: 88
value: R.M.D Engineering College
Here, class TwoGen<T, V> has two type parameters: T and V, separated by a comma. Because it has two type parameters, two type arguments must be passed to TwoGen when an object is created. In this case, Integer is substituted for T, and String is substituted for V.
Generic methods:�
Methods inside a generic class can make use of a class’ type parameter and are, therefore, automatically generic relative to the type parameter. However, it is possible to declare a generic method that uses one or more type parameters of its own. The scope of arguments is limited to the method where it is declared. It allows static as well as non-static methods.
Syntax for a generic method:
<type-parameter> return_type method_name (parameters)
{
...
}
Example 1:
class Demo
{
static <V, T> void display (V v, T t)
{
System.out.println(v.getClass().getName()+" = " +v);
System.out.println(t.getClass().getName()+" = " +t);
}
public static void main(String[] args)
{
display(88," R.M.D Engineering College ");
}
}
Output:
java lang.Integer = 88
java lang.String = R.M.D Engineering College
Example 2:
Following example illustrates how we can print an array of different type using a single Generic method
public class GenericMethodTest
{
// generic method printArray
public static < E > void printArray( E[] inputArray )
{
// Display array elements
for(E element : inputArray)
{
System.out.printf("%s ", element);
}
System.out.println();
}
public static void main(String args[])
{
// Create arrays of Integer, Double and Character
Integer[] intArray = { 1, 2, 3, 4, 5 };
Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
Character[] charArray = { 'R', 'M', 'D', 'E', 'C' };
System.out.println("Array integerArray contains:");
printArray(intArray); // pass an Integer array
System.out.println("Array doubleArray contains:");
printArray(doubleArray); // pass a Double array
System.out.println("Array characterArray contains:");
printArray(charArray); // pass a Character array
}
}
Output
Array integerArray contains:
1 2 3 4 5
Array doubleArray contains:
1.1 2.2 3.3 4.4
Array characterArray contains:
R M D E C
Bounded Types:
In Java Generics it is possible to set restriction on the type that will be allowed to pass to a type-parameter. This is done with the help of extends keyword when specifying the type parameter.
< T extends Number >
Here we have taken Number class, it can be any wrapper class name. This specifies that T can be only be replaced by Number class data itself or any of its subclass.
Example: Create a generic class that contains a method that returns the average of an array of numbers.
class Stats<T extends Number>
{
T[] nums; // array of Number or subclass
Stats(T[] o)
{
nums = o;
}
double average()
{
double sum = 0.0;
for(int i=0; i < nums.length; i++)
sum += nums[i].doubleValue();
return sum / nums.length;
}
}
public class GenDemo
{
public static void main(String[] args)
{
Integer inums[] = { 1, 2, 3, 4, 5 };
Stats<Integer> iob = new Stats<Integer>(inums);
double v = iob.average();
System.out.println("iob average is " + v);
Double dnums[] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
Stats<Double> dob = new Stats<Double>(dnums);
double w = dob.average();
System.out.println("dob average is " + w);
}
}
Output:
iob average is 3.0
dob average is 3.3
Generic Interface
Comparable interface is a great example of Generics in interfaces and it’s written as:
package java.lang;
import java.util.*;
public interface Comparable<T>
{
public int compareTo(T o);
}
In similar way, we can create generic interfaces in java. We can also have multiple type parameters as in Map interface. Again we can provide parameterized value to a parameterized type also, for example new HashMap<String, List<String>>(); is valid.
�
Restrictions and Limitations:
There are a few restrictions that you need to keep in mind when using generics. They involve creating objects of a type parameter, static members, exceptions, and arrays.
It is not possible to create an instance of a type parameter.For example, consider this class:
class Gen<T>
{
T ob;
Gen()
{
ob = new T(); // Illegal!!!
}
}
Here, it is illegal to attempt to create an instance of T. The reason is since T does not exist at run time, how the compiler doesn’t know what type of object to create.
No static member can use a type parameter declared by the enclosing class. For example,both of the static members of this class are illegal:
class Wrong<T>
{
// Wrong, no static variables of type T.
static T ob;
// Wrong, no static method can use T.
static T getob()
{
return ob;
}
}
Although we can’t declare static members that use a type parameter declared by the enclosing class, we can declare static generic methods, which define their own type parameters.
There are two important generics restrictions that apply to arrays.
First, you cannot instantiate an array whose element type is a type parameter. Second, you cannot create an array of type-specific generic references. The following short program shows both situations:
// Generics and arrays.
class Gen<T extends Number>
{
T ob;
T vals[]; // OK
Gen(T o, T[] nums)
{
ob = o;
// This statement is illegal.
// vals = new T[10]; // can't create an array of T
// But, this statement is OK.
vals = nums; // OK to assign reference to existent array
}
}
class GenArrays
{
public static void main(String args[])
{
Integer n[] = { 1, 2, 3, 4, 5 };
Gen<Integer> iOb = new Gen<Integer>(50, n);
// Can't create an array of type-specific generic references.
// Gen<Integer> gens[] = new Gen<Integer>[10]; // Wrong!
// This is OK.
Gen<?> gens[] = new Gen<?>[10]; // OK
}
}
The reason you can’t create an array of T is that T does not exist at run time, so there is no way for the compiler to know what type of array to actually create. However, you can pass a reference to a type-compatible array to Gen( ) when an object is created and assign that reference to vals, as the program does in this line:
vals = nums; // OK to assign reference to existent array
This works because the array passed to Gen has a known type, which will be the same type as T at the time of object creation.
A generic class cannot extend Throwable. This means that you cannot create generic exception classes.
Objects in the virtual machine always have a specific nongeneric type. Therefore, all type inquiries yield only the raw type. you will get a compiler error (with instanceof) or warning (with casts) when you try to inquire whether an object belongs to a generic type.
It is illegal to create conditions that cause clashes when generic types are erased.Here is an example. Suppose we add an equals method to the Pair class,
public class Pair<T>
{
public boolean equals(T value)
{
return first.equals(value) && second.equals(value); . . .
}
Consider a Pair<String>. Conceptually, it has two equals methods:
boolean equals(String) // defined in Pair<T>
boolean equals(Object) // inherited from Object
But the intuition leads us astray. The erasure of the method boolean equals(T) is boolean equals(Object) which clashes with the Object.equals method. The remedy is, to rename the offending method.
Assignments
PART – A
1. What is thread in Java? (K1, CO4)
2. What is Multithreading? (K1, CO4)
3. What is the difference between a process and a thread in Java? (K1, CO4)
4. What are the two ways of creating a thread? (K1, CO4)
5. What is life cycle of thread? (K1, CO4)
PART – A
6. Can we start a thread twice in Java? (K1, CO4)
7. Can we call run() method of Thread class? (K1, CO4)
8. What is deadlock? (K1, CO4)
9. What is synchronization? (K1, CO4)
10. What is the difference between notify() and notifyAll()? (K1, CO4)
11. What does join() method do? (K1, CO4)
12. How does thread communicate with each other? (K1, CO4)
PART – A
13. What is the difference between wait() and sleep() method? (K1, CO4)
14. What is Daemon Threads in Java? (K1, CO4)
1. setDaemon(boolean on)
2. isDeamon()
15. What is thread group? (K1, CO4)
1. activeCount()
2. activeGroupCount()
3. checkAccess()
4. getName()
5. destroy()
6. enumerate()
7. interrupt()
�
wait() | sleep() |
The wait() method is defined in Object class. | The sleep() method is defined in Thread class. |
The wait() method releases the lock. | The sleep() method doesn't release the lock. |
PART – A
16. What are Generics? (K1, CO4)
17. What is Generic Programming? (K1, CO4)
18. What is Generic class? (K1, CO4)
class classname <type-param-list>
{
// statements
}
19. What is Generic Method? (K1, CO4)
public <type-parameter> <returntype> function_name()
{
// statements
}
20. What is a generic type parameter? (K1, CO4)
PART – A
21. List out commonly used type parameters. (K4, CO4)
22. What are Some Advantages of Using Generic Types? (K1, CO4)
23. What is Type Erasure? (K1, CO4)
24. What is Type Inference? (K1, CO4)
25. What is a Bounded Type Parameter? (K1, CO4)
<Type extends superclass>
26. Is it Possible to Declared a Multiple Bounded Type Parameter? (K1, CO4)
PART – A
27. What is a Wildcard Type? (K1, CO4)
A wildcard type represents an unknown type. It's detonated with a question mark as follows:
public static void f1(List<?> list)
There are 3 kinds of wildcard types:
1. wildcard with lower bound
2. wildcard with upper bound
3. unbounded wildcard
28. What is lower bound wildcard? (K1, CO4)
A lower bounded wildcard means we are forcing the type to be a superclass of our bounded type
< ? Extends E > any sub types of E
29. What is upper bound wildcard? (K1, CO4)
An upper bounded wildcard is when a wildcard type inherits from a concrete type.
< ? Super N > any super type of N
30. What is unbound wildcard? (K1, CO4)
An unbounded wildcard is a wildcard with no upper or lower bound, that can represent any type.
<?> any type
31.Define Stream. (K1, CO3)
A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination
32. list the type of streams supported in Java. (K3, CO3)
1. Byte Stream : It is used for handling input and output of 8 bit bytes. The frequently
used classes are FileInputStream and FileOutputStream.
2. Character Stream : It is used for handling input and output of characters. Charac- ter stream uses 16 bit Unicode. The frequently used classes are FileReader and File Writer.
PART – A
33. List out the predefined streams available in Java. (K3, CO3)
Java provides the following three standard streams −
• Standard Input − refers to the standard InputStream which is the keyboard by default. This is used to feed the data to user’s program and represented as system.in.
• Standard Output − refers to the standard OutputStream by default,this is console and represented as system.out.
• Standard Error − This is used to output the error data produced by the user’s program and usually a computer screen is used for standard error stream and represented as system.err.
34. What is Byte stream in Java? list some of the Bytestream classes. (K1, CO3)
The byte stream classes provide a rich environment for handling byte-oriented I/O.
List of Byte Stream classes
• Byte Array Input Stream
• Byte Array Output Stream
• Filtered Byte Streams
• Buffered Byte Streams
35. What is character stream in Java? list some of the characterstream classes. (K1, CO3)
The Character Stream classes provide a rich environment for handling character-oriented I/O.
List of Character Stream classes
• File Reader
• File Writer
• Char Array Reader
• Char Array Writer
36. What are the two variations of read() method? (K1, CO3)
public int read(byte[] bytes, int offset, int length) throws IOException
public int read(byte[] bytes) throws IOException
PART – A
37. What are the two variations of write() method? (K1, CO3)
public void write(byte[] bytes, int offset, int length) throws IOException
public void write(byte[] bytes) throws IOException
38. What’s the difference between println(), print() and printf()? (K1, CO3)
print() - prints string inside the quotes println() - prints string inside the quotes similar like print() method. Then the cursor moves to the beginning of the next line. printf() - it provides string formatting (similar to printf in C/C++ programming).
39. Write about Fileinputstream. (K1, CO3)
• This stream is used for reading data from the files.
• Objects can be created using the keyword new and there are several types of constructors available.
• The two constructors which can be used to create a FileInputStream object:
i) Following constructor takes a file name as a string to create an input stream
object to read the file:
OutputStream f = new FileOutputStream("filename ");
ii) Following constructor takes a file object to create an input stream object to read
the file. First we create a file object using File() method as follows:
File f = new File(“filename “);
InputStream f = new FileInputStream(f);
40. Write about Fileoutputstream. (K1, CO3)
• FileOutputStream is used to create a file and write data into it.
• The stream would create a file, if it doesn’t already exist, before opening it for output.
• The two constructors which can be used to create a FileOutputStream object:
i) Following constructor takes a file name as a string to create an input stream object to write the file:
OutputStream f = new FileOutputStream(“filename “);
ii) Following constructor takes a file object to create an output stream object to
write the file. First, we create a file object using File() method as follows:
File f = new File(“filename “);
OutputStream f = new FileOutputStream(f);
PART - B
1. What is a thread? Describe the lifecycle and states of thread with example. (K1, K4, CO4)
2. Summarize briefly about thread synchronization with an example. (K2, CO4)
3. Demonstrate Inter thread communication and suspending, resuming and stopping threads. (K2, CO4)
4. List the two ways of implementing threads with example. (K4, CO4)
5. Show how to extend thread class and how to implement runnable interface for creating and starting threads? (K2, CO4)
6. Classify the generic in java with example. (K2, CO4)
7. Explain briefly about generic classes and methods in java programming. (K5, CO4)
8. Evaluate how generic methods and generic expressions are translated. (K6, CO4)
9. Design two threads to display odd numbered element in an array of size 50 and even numbered elements in another array in size 50. create instances of the above thread and run them. (K6, CO4)
10. Explain the concept of streams and its byte stream classes in detail. (K2, CO3)
11. Explain the use of File stream classes and file modes. Write an example program for file manipulation. (K2, CO3)
12. Explain I/O streams with suitable examples. (K2, CO3)
Online Certifications
Real Time Applications
.
89
Content Beyond Syllabus - Java Thread Pool
ThreadPoolExecutor
Java Concurrency
Assessment Schedule
114
S.NO | Name of the Assessment | Start Date | End Date | Portion |
1 | IAT 1 | 20.09.2021 | 25.09.2021 | UNIT 1 & 2 |
2 | IAT 2 | 22.10.2021 | 28.10.2021 | UNIT 3 & 4 |
3 | REVISION | 13.11.2021 | 16.11.2021 | UNIT 5 , 1 & 2 |
4 | MODEL | 18.11.2021 | 29.11.2021 | ALL 5 UNITS |
Text Books & References
TEXT BOOKS
REFERENCES
Mini Project
Interest Calculator
Calculate interest based on the type of the account and the status of the account holder. The rates of interest changes according to the amount (greater than or less than 1 crore), age of account holder (General or Senior citizen) and number of days if the type of account is FD or RD.
Some sample rates are given in the below tables: Rate of FD citizen) and interest for amounts below 1 Crore:
| Current Rates of interest | |
Maturity Period | General | Senior Citizen |
7 days to 14 days | 4.50 | 5.00 |
15 days to 29 days | 4.75 | 5.25 |
30 days to 45 days | 5.50 | 6.00 |
45 days to 60 days | 7 | 7.50 |
61 days to 184 days | 7.50 | 8.00 |
185 days to 1 year | 8.00 | 8.50 |
Rate of FD interest for amounts above 1 Crore:
Maturity Period | Interest Rate |
7 days to 14 days | 6.50 |
15 days to 29 days | 6.75 |
30 days to 45 days | 6.75 |
45 days to 60 days | 8 |
61 days to 184 days | 8.50 |
185 days to 1 year | 10.00 |
Hint: Use necessary Built-in and User defined Exceptions and multithreading concept
| Current Rates of interest | |
Maturity Period | General | Senior Citizen |
6 months | 7.50 | 8.00 |
9 months | 7.75 | 8.25 |
12 months | 8.00 | 8.50 |
15 months | 8.25 | 8.75 |
18 months | 8.50 | 9.00 |
21 months | 8.75 | 9.25 |
SB Account interest rates:
Type of Account | Interest Rate |
Normal | 4% |
NRI | 6% |
Requirements:
Sample class structure is given below:
Account(Abstract) |
double interestRate |
double amount |
Rate of RD interests:
abstract double
calculateInterest()
FDAccount |
double interestRate |
double amount |
int noOfDays |
ageOfACHolder |
abstract double calculateInterest() |
SBAccount |
double interestRate |
double amount |
abstract double calculateInterest() |
RDAccount |
double interestRate |
double amount |
int noOfMonths; |
double monthlyAmount; |
abstract double calculateInterest() |
Select the option:
1
Enter the Average amount in your account: 10000
Interest gained: Rs. 400 Select the option:
2
Enter the FD amount: 10000
Enter the number of days: 91
Enter your age: 65
Interest gained is: 800 Select the option:
Sample Output:
2
Enter the FD amount:
10000
Enter the number of days: 91
Enter your age: 34
Interest gained is: 750
Select the option:
2
Enter the FD amount: 10000
Enter the number of days: -7
Enter your age: 78
Invalid Number of days. Please enter correct values.
Disclaimer:��This document is confidential and intended solely for the educational purpose of RMK Group of Educational Institutions. If you have received this document through email in error, please notify the system manager. This document contains proprietary information and is intended only to the respective group / learning community as intended. If you are not the addressee you should not disseminate, distribute or copy through e-mail. Please notify the sender immediately by e-mail if you have received this document by mistake and delete this document from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.
Thank you