05_InputInJavaStatements
05_InputInJavaStatements
Lesson 2
Various Inputs
Input means giving values to the program. There are three ways to give values to a program.:
1) Initialization (Giving values in the program itself)
2) Parameter input (User input in the beginning of program)
3) Input stream - Scanner class input (User input during program run)
Initialization of Values
Initialization means values are written to the variables when program writing.
Various Initializations
2. Initialize name, standard, division, mark of computer and maths of a student. Find average.
Print all details.
public class Student
{
public static void main()
{
String na="Anu";
int std=9;
char div='A';
double m1=100,m2=99.5;
double avg=(m1+m2)/2.0;
System.out.println("Name: "+na);
System.out.println("Class: "+std+" "+div);
System.out.println("Marks: "+m1+", "+m2);
System.out.println("Average: "+avg);
}
}
Input of Values Using Parameter
Values are accepted from the user when the program starts not during execution.
Variables are declared individully using comma separator with method prototype (header).
First line of a method definition is known as method prototype or header.
Various Parameter Elements
2. Input through parameter name, standard, division, mark of computer and maths of a
student. Find average. Print all details.
public class Student
{
public static void main(String name, int std, char div, double m1, double m2)
{
double avg=(m1+m2)/2.0;
System.out.println("Name: "+name);
System.out.println("Class: "+std+" "+div);
System.out.println("Marks: "+m1+", "+m2);
System.out.println("Average: "+avg);
}
}
Input Stream - Scanner Class Input
Using the input stream class Scanner values can input during program run. To use the Scanner
class the import java.util statement is required. The import is a keyword and java.util is a package.
Package
The java.util is a package. A package contain classes. The Scanner class resides in java.util package.
To input values methods are required. To use methods object of Scanner class is required. The sc is
an object of Scanner class.
Comments
A comment is a remark on a program or part of a program. The compiler ignores the comment; it is not
considered as part of the program. It is used for the users.
Comments and their uses with examples.
1) // Single line comment
It is used write a comment in single line: E.g.:
s=a+b; // To find sum
2) /* */ Multi-line comment
It is used when there is more than one line to be remarked. E.g.:
/* The following lines are
to find result of students in a class */
3) /** */ Documentation comment
It is used to write description of a program. E.g.:
/** Automation of library management
© Raju Xavier */
Answer:
3) //
4) /* */
6) /** */