Page 84 - CA_Blue( J )_Class10
P. 84
7 avg=sum/3.0;
8 System.out.println(" Tabular Format");
9 System.out.println("Sum"+" \t "+"Average");
10 System.out.println(sum+" \t "+avg);
11 }
12 }
You will get the following output:
Note: In this program, since there is no main() method, a temporary instance of object name
"methodcall" is created.
5.4 USING SCANNER CLASS
The Scanner class is a predefined class in Java. It is a part of the java.util package and included in JDK 5. It is used to
input different types of values for primitive data types like int, float, string, etc.
To utilise this class, we need to import the "java.util" package into our program. Following is the syntax to import the
java.util package:
import java.util.*; //Imports all the classes of the java.util package
OR
import java.util.Scanner; //Imports only the Scanner class
We also need to create an object of the class so that its methods can be used. Following is the syntax to create the
object of the Scanner class:
Scanner sc= new Scanner(System.in));
In the preceding syntax, you may have noticed that the System.in parameter is passed while creating the object of
the Scanner class. The System.in parameter represents the standard input stream. It allows us to take input from a
standard input device connected to a computer, like a keyboard. Java also provides System.out as a standard output
stream that displays the output on the standard output screen like terminal.
The Scanner class provides various built-in methods. The following table shows different methods of the Scanner class:
Method Description
next() It scans the next value from the terminal.
nextBoolean() It scans the next Boolean value from the terminal.
nextByte() It scans the next Byte type value from the terminal.
nextDouble() It scans the next Double type value from the terminal.
nextFloat() It scans the next Float type value from the terminal.
8282 Touchpad Computer Applications-X

