Unit - III Computer Notes
Unit - III Computer Notes
2. Declaration of a classcompulsory
Body of main()
} end of main
}end of class.
Types of Comments:
Syntax: -
// comments.
Syntax: -
/*opening
Statements
*/closing
third variable*/
Syntax: -
/**-------
-----------*/
Taking input:
There are various ways to input the values on an operation. This can
be done by the following ways:
class sum
{ int a=20,b=30,c;
c=a+b;
System.out.println(“sum=”+c);
}}
Output: sum=50
This is one of the methods to accept the value from the user at the
time of execution of the program. The variables must be defined
within main() as arguments.
class sum
{
int sum,avg;
sum=a+b;
avg=sum/2;
System.out.println(“sum=”+sum);
System.out.println(“average=”+avg);
}}
There are some steps that you all must follow in order to take input
from Scanner class.
e.g. int x;
x=sc.nextInt();
e.g. float x;
x=sc.nextFloat();
e.g. double x;
x=sc.nextDouble();
str=sc.next();
str=sc.nextLine();
import java.util.*;
class Input
int x;
float y;
double z;
String str;
System.out.println(“enter an integer”);
x=sc.nextInt();
y=sc.nextFloat();
z=sc.nextDouble();
str=sc.next();
System.out.println(“x=”+x+”\ty=”+y+”\tz=”+z+”\tstr=”+str);
}}
Steps: -
1. import java.io.*;
2. class <classname>
{
Body
Where throwskeyword
IOExceptionpredefined class.
This statement is used to create an object ‘in’ to store the input data
from the keyboard temporarily.
To accept an integer
int n;
n=Integer.parseInt(in.readLine());
To accept float value
float n;
n=Float.parseFloat(in.readLine());
To accept double type value
double n;
n=Double.parseDouble(in.readLine());
To accept character
char ch;
ch=(char)(in.read());
To accept a string
String str;
str=in.readLine();
FLOW OF CONTROL: -