Skill Programming EXPT 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Experiment No.

2
Aim: To Write Java Program on accepting input through keyboard.

Input Specification: x,y,z,w of type int.


Output Specification:. max of type int

Objectives:-Student/we will learn the standard IO classes and the method from that class to
read the input accepted from user through keyboard.

Theory:
As we are studying the methods of accepting the input from user, we will see
DataInputStream , BufferedReader and Scanner class in detail here .

1)Command line Argument : We have learned this in the previous experiment.

2) DataInputStream class : A data input stream enables an application to read primitive Java
data types from an underlying input stream in a machine-independent way(instead of raw bytes).
That is why it is called DataInputStream – because it reads data (numbers) instead of just bytes.
An application uses a data output stream to write data that can later be read by a data input
stream.

3) , BufferedReader class

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. It
inherits Reader class.

4) Scanner class

Scanner is a class in java.util package used for obtaining the input of the primitive types like int,
double, etc. and strings. It is the easiest way to read input in a Java program, though not very
efficient if you want an input method for scenarios where time is a constraint like in competitive
programming.

logic of the program.


In this program , maxim class is created which consists of data members x,y,z,w,max of
int . Input variables are initialized in such a way that for x input value is accepted through
command line argument .for y , input value is accepted through keyboard from user and
readline() method of DataInputStream class is used to read the input. for z , input value is
accepted through keyboard from user and readline() method of BufferedReader class is used to
read the input for w, input value is accepted through keyboard from user and nextInt()method of
Scanner class is used to read the input. Based on input values of x,y,z,w maximum is found out
by comparing first value with remaining three values using if statement and comparison
operator(<),and maximum value is displlayed
Algorithm:

Step 1: Start

Step 2: Create a class maxim and define it.

Step 3: In class maxim, Declare n initialize input variables x,y,z,w of type int and max of
type int use Command line argument to give input value to x, for y, use readline() method
of DataInputStream classto read the input value accepted through key board. for z, use
readline() method of BufferedReader class to read the input value accepted through key
board. for w, use nextInt() method of scanner class to read the input value accepted through
key board.

Step 4: Assume x is maximum and comparing x with remaining values, Based on the status
of the conditions, find out the maximum value.

Step 5: Display the maximum value

Step 6: stop
Program(Code):

// Program no.2: program on accepting input through keyboard.

import java.io.*;
import java.util.*;
class maxim
{
public static void main(String args[])throws Exception
{
int x,y=0,z=0,w=0,max;
DataInputStream d1 =new DataInputStream(System.in);
BufferedReader obj= new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);

x=Integer.parseInt(args[0]);
try
{
System.out.println("Enter value of y");
y=Integer.parseInt(d1.readLine());

System.out.println("Enter value of z");


z=Integer.parseInt(obj.readLine());

System.out.println("Enter value of w");


w=sc.nextInt();
}
catch(Exception e)
{};
max=x;
if(max<y)
max=y;
if(max<z)
max=z;
if(max<w)
max=w;
System.out.println("maximum="+max);

}
}
Output:

C:\>d:

D:\>cd programs

D:\programs>path="C:\jdk1.6.0_23\bin";

D:\programs>javac maxim.java
Note: maxim.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

D:\programs>java maxim 7
Enter value of y
23
Enter value of z
41
Enter value of w
4
maximum=41

Outcome: With this we/student learned to use method like readLine(() method of
DataInputStream class , BufferedReader class and nextInt() of scanner class for reading
input accepted through keyboard in java program.

You might also like