Oop EXP1 2
Oop EXP1 2
Experiment No -1
Aim: -
Write a program to accept a number and display its square using BufferedReader
class.
Theory:
Java BufferedReader class is used to read the text from a character-based input
stream. It can be used to read data line by line by readLine() method. It makes the
performance fast.This method is non static and is in the class BufferedReader. Hence
we need to make an object of this class to use the readLine() method.
The class BufferedReader is in the package “java.io”. When making an object of the
class BufferedReader, we need to pass an object of InputStreamReader to the
constructor f this class. Hence we need to also create and pass an object to the
constructor of this class. But to create an object of the class InputStreamReader, we
need to pass to the constructor a variable f the class “System” called as “in”. This
variable refers to the standard input device i.e. keyboard. Hence the object of the class
BufferedReader is to be created as shown below:
Since this class “BufferedReader” is in the package “java.io”, we need to import all
the classes of this package by writing the statement “import java.io.*” in the
beginning of the program.
The readLine() method stated above can accept only a string. This string can be
converted to int, float etc. with the help of wrapper classes. To convert string in to int
int a = Integer.parseInt(str) ;
Similarly, to convert a string to float number, the wrapper class “Float” with its static
method parseFloat is to be used.
Program:-
import java.io.* ;
class Square
{
public static void main(String args[]) throws IOException
{
int a ,res;
BufferedReader br = new BufferedReader ( new InputStreamReader(System.in))
String str:
System.out.println(“ \n Enter a number: ”);
str=br.readLine();
a= Integer.parseInt(str) ;
res=a * a;
System.out.println(“ The Square=” +res);
}
}
Output:
C:\java program\ javac Square.java
C:\java program\ java Square
Enter a number :
3
The Square = 9
OOPM Lab/ III 2
Shivajirao S Jondhale College of Engineering, Dombivli (E)
Department of Computer Engineering
Experiment No - 2
Aim:
Write a program to calculate and display simple interest using Scanner class.
Theory:
The Scanner class is used to get user input, and it is found in the java.util package.
To use the Scanner class, create an object of the class and use any of the available
methods found in the Scanner class documentation.
For Scanner class, we directly accepts the required data type value. There are
methods to accept various data type values in the class Scanner. These methods are
non- static and are in the Scanner class. Hence we need to make an object of this
class to use these methods. When making an object of the class Scanner, we need to
pass an object to the constructor a variable of the class System called as “in”. This
variable refers to the standard input device i.e. keyboard. Hence the object of the
class Scanner is to be created as shown below:
Here sc is object of Scanner class. The new operator is is used to create an object of
a class. Since Scanner class is in the “java.util.” package, we need to import all the
classes of this package by writing the statement “import java.util.* ” in the beginning
of the program.
Conclusion: - In this way we have learn to accept an input using Scanner class.
Program:-
import java.util.Scanner;
class SimpleInterest
{
public static void main(String args[])
{
float p, r, t;
Scanner s = new Scanner(System.in);
System.out.print("Enter the Principal : ");
p = s.nextFloat();
System.out.print("Enter the Rate of interest : ");
r = s.nextFloat();
System.out.print("Enter the Time period : ");
t = s.nextFloat();
float si;
si = (r * t * p) / 100;
System.out.print("The Simple Interest is : " + si);
}
}
Output:
Enter the Principal : 13000
Enter the Rate of interest : 12
Enter the Time period : 2
The Simple Interest is : 3120.0