Professional Documents
Culture Documents
First method is by using the instance of a class named java.io.BufferedReader and the
second method is by using the instance of a class named java.util.Scanner.
1.1 Using BufferedReader for text based user input from the keyboard The following
example will teach you how to read user inputs using BufferedReader class.
Type the following code in file named Example1.java and save it in your respective
work space.
/* Step II */
/* Step III */
/* Call readLine method on br reference variable which
is of type BufferedReader */
Compile the above code and note down what type of errors you will get during
compilation.
In order to successfully compile the above file, you can adopt any of the following two
methods [ Note: We have not covered Exceptions in class so far. So as of now you have to
adopt the methods as it is.]
class Example1
{
public static void main(String args[]) throws IOException
{
/* Step I */
/* Step II */
Method 2: Put the call to readLine() method in try catch block. [As shown in red
color].
class Example1
{
public static void main(String args[])
{
/* Step I */
/* Step II */
/* Step III */
/* Call readLine method on br reference variable which
is of type BufferedReader */
try
{
String name = br.readLine();
System.out.println("Your name is: " + name);
}
Catch(IOException ae) {}
} // End of main
}// End of class Example1
NOTE : All the parse() methods shown in the above table, executes/works without runtime
errors/exceptions if the string type value represents their exact type. For example, the
statement Integer.parseInt(10) executes successfully because 10 can be reliably
parsed into an integer type. On the other hand, the execution of the statement
Integer.parseInt(10.56) results in an runtime exception named
NumberFormatException. Some more examples about the use of parse methods are
given below:
Float.parseFloat(10.56f); Executes Successfully
Write a program in java to take 10 integer numbers as user input using the
BufferedReader and print the sum of these numbers.
// Note : The above statement can be bifurcated in two statements also as follows
// First Create an InputStreamReader instance using System.in
// InputStreamReader ins = new InputStreamReader(System.in);
// Create a BufferedReader instance br using InputStreamReader ins
// BufferedReader br = new BufferedReader(ins);
Execute the above file for the following two cases and observe the output
1. Execute the file by giving ten inputs as [1 2 2 3 4 5 6 7 8 9] [ All Valid Integers]
2. Execute the file by giving ten inputs as [1 2 3 4.5 6 7 8 9 10 67] [ One Invalid
Input]
Write a program in java which reads the values of five attributes related with a
student such as name (name of a student), age (age of a student), marks_in_phy
(marks in physics out of 100), marks_in_chem(marks in chemistry out of 100) and
marks_in_math(marks in mathematics out of 100). In the end program displays the
The latest version of Java especially that are introduced after JDK 1.5, supports user
input via class named java.util.Scanner. The use of this class is simple and overcomes
the difficulties that are encountered during the use of BufferedReader class.
Note that System.in is the default input stream supported in Java. Also remember that
System.out is the default output stream. In order to read input via Scanner class, you
have to first create an object/instance of type Scanner by passing the default input
stream System.in to it as follows
Note : If the type of the input value is not according to the type of the variable
then program generates a runtime exception named InputMismatchException.
For example, if an input value 10.5 is supplied for an int type variable then it
results in InputMismatchException during execution.
/* Step 1 */
/* Create/Instantiate a scanner class instance by passing
System.in to its constructor */
Compile and run the above code and observe the output?
Rewrite the Code given in Exercise 1 and 2 Using Scanner class and these
programs are to recorded in the Lab file in handwritten form.
A class provides a template for defining the attributes (known as instance fields or object
variables in Java) and operations (known as functions in C++ and methods in Java). A
simple class template is shown below
class class-name
{
Instance Fields;
Instance Method;
}// End of class
A classs body is enclosed within a pair of curly braces {}.
After defining the ComplexNumber class, wrtite a driver class named Lab2Ex3 which performs
the following tasks in sequential order:
Create an instance named cn1 of type ComplexNumber
Display the values of instance fields associated with cn1.
Set the value of real field of cn1 to 10.5
Set the value of imag field of cn1 to 20.5
Display the updated values of instance fields associated with cn1.
Create an instance named cn2 of type ComplexNumber
Set the value of real field of cn2 to 10.5
// Driver class
class ComplexNumberTest
{
public static void main(String args[])
{
// Create a ComplexNumber instance named cn1
ComplexNumber cn1 = new ComplexNumber();
Compile and Execute the Above Code and Observe the Output.
Define a class named Point which encapsulates a point in 2-dimensional space. A point
p is represented by two co-ordinates name x (x-coordinate) and y (y-coordinate).
You have to define the class as per following specification
Add two attributes in class Point named x and y of type double and having
class scope
Add two methods for reading/getting the values of x and y fields
Add methods for setting/updating the values of x and y field
Add a method for checking the equality of any two Point type instances.
Suppose p1 and p2 are two Point type instances. They are equal iff the values
their x and y fields are equal
Add a method to compute the distance between two points.
Add a method to display the details of any point type instance.
Define a driver class named PointTest which performs the following tasks sequentially
Create two Point type instances named p1 and p2.
Display the details of p1 and p2
Update the values of x and y fields of instance p1 to 3.4 and 5.6 respectively
You are given an incomplete Java code of the above-mentioned two classes named Point
and PointTest. You have to complete as per the mentioned specification.
//Update the values of x and y fields of instance p1 to 3.4 and 5.6 respectively
//Update the values of x and y fields of instance p2 to 13.4 and 15.6 respectively