0% found this document useful (0 votes)
7 views

Oop EXP1 2

Uploaded by

TEJAS DALVI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Oop EXP1 2

Uploaded by

TEJAS DALVI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Shivajirao S Jondhale College of Engineering, Dombivli (E)

Department of Computer Engineering

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:

BufferedReader br = new BufferedReader( new InputStreamReader(System.in))

Here br is object of class BufferedReader. The new operator is used to create an


object of class.

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

OOPM Lab/ III 1


Shivajirao S Jondhale College of Engineering, Dombivli (E)
Department of Computer Engineering

we have to use a static method parseInt() from a wrapper class”Interger” as given


below:

int a = Integer.parseInt(str) ;

where a is an interger and str is a string.

Similarly, to convert a string to float number, the wrapper class “Float” with its static
method parseFloat is to be used.

Conclusion: - In this way we have learn to accept an input using BufferedReader


class.

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:

Scanner sc = new Scanner (System.in);

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.

A List of methods to accept various types of inputs is given below

nextBoolean() : Reads a boolean value from the user

nextByte() : Reads a byte value from the user

nextDouble() : Reads a double value from the user

nextFloat(): Reads a float value from the user


OOPM Lab/ III 3
Shivajirao S Jondhale College of Engineering, Dombivli (E)
Department of Computer Engineering

nextInt() : Reads a int value from the user

nextLine() : Reads a String value from the user

nextLong() : Reads a long value from the user

nextShort() : Reads a short value from the user

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

OOPM Lab/ III 4

You might also like