import java.io.*;
import java.util.*;
public class SumOfInt{
Scanner scan=new Scanner(System.in); /*Scanner is declaired for input purpose.
You can even use BufferedReader (BufferedReader name)=new BufferedReader(new InputStreamReader(System.in));*/
int a, b, sum=0; //sum is to store a+b.
public void input()throws IOException{ /*"throws IOException" is important while value is to be entered by the user.
This is use to catch any error and throw it to the compiler i.e. JVM.*/
System.out.println("Enter two numbers: ");
a=scan.nextInt();
b=scan.nextInt();
//values of a and b would be input by the user.
}
public void compute(){ //compute() is used for calculation purpose.
sum=a+b; //Sum of 'a' and 'b'will be calculated here.
}
public void display(){ //display() is used for output. Note: Name of the function is user dependent.
System.out.println("Sum of "+a+" + "+b+" is: "+sum); // Outout of this line would be: Sum of 10 + 20 is: 30 (if a=10 and b=20).
}
public static void main(String[] args)throws IOException{
SumOfInt obj=new SumOfInt();
obj.input();//Same as the function name and in same sequence.
obj.compute();
obj.display();
}
}//class ends.
If want to use "Scanner" then you should know its syntax.
Syntax of "Scanner":-
Scanner <Scanner_Name>=new Scanner(System.in);
To use "Scanner" you should have imported "util" package or "util.Scanner" package
Syntax for Importing any package:-
import java.<package_name>.*;
or
import java.<package_name>.<class_name>;
Else if you want to use "BufferedReader" then you should know its syntax.
Syntax of "BufferReader":-
BufferedReader <buffer_name>=new BufferedReader(new InputStreamReader(System.in));
Syntax for using "Scanner" for input purpose:-
<variable_name>=<Scanner_Name>.next<data_type>();
(Data Type's first letter should be capital.)
Syntax for using "BufferedReader":-
<variable_name>=<data_type>.parse<data_type>(<buffer_name>.readLine());
(Data Type's first letter should be capital.)
Note:-
If you want to input the value in a integer variable using BufferReader, it would be like this:-
int a=Integer.parseInt(buff.readLine());
For String:-
String name=buff.readLine();
For char:-
char character=(char)buff.read();
If you want to input the value of String using Scanner:-
String name=scan.nextLine();
or
String name=scan.next();
For char:-
String string;
char character;
string=scan.next();
character=string.charAt(0);
import java.util.*;
public class SumOfInt{
Scanner scan=new Scanner(System.in); /*Scanner is declaired for input purpose.
You can even use BufferedReader (BufferedReader name)=new BufferedReader(new InputStreamReader(System.in));*/
int a, b, sum=0; //sum is to store a+b.
public void input()throws IOException{ /*"throws IOException" is important while value is to be entered by the user.
This is use to catch any error and throw it to the compiler i.e. JVM.*/
System.out.println("Enter two numbers: ");
a=scan.nextInt();
b=scan.nextInt();
//values of a and b would be input by the user.
}
public void compute(){ //compute() is used for calculation purpose.
sum=a+b; //Sum of 'a' and 'b'will be calculated here.
}
public void display(){ //display() is used for output. Note: Name of the function is user dependent.
System.out.println("Sum of "+a+" + "+b+" is: "+sum); // Outout of this line would be: Sum of 10 + 20 is: 30 (if a=10 and b=20).
}
public static void main(String[] args)throws IOException{
SumOfInt obj=new SumOfInt();
obj.input();//Same as the function name and in same sequence.
obj.compute();
obj.display();
}
}//class ends.
If want to use "Scanner" then you should know its syntax.
Syntax of "Scanner":-
Scanner <Scanner_Name>=new Scanner(System.in);
To use "Scanner" you should have imported "util" package or "util.Scanner" package
Syntax for Importing any package:-
import java.<package_name>.*;
or
import java.<package_name>.<class_name>;
Else if you want to use "BufferedReader" then you should know its syntax.
Syntax of "BufferReader":-
BufferedReader <buffer_name>=new BufferedReader(new InputStreamReader(System.in));
Syntax for using "Scanner" for input purpose:-
<variable_name>=<Scanner_Name>.next<data_type>();
(Data Type's first letter should be capital.)
Syntax for using "BufferedReader":-
<variable_name>=<data_type>.parse<data_type>(<buffer_name>.readLine());
(Data Type's first letter should be capital.)
Note:-
If you want to input the value in a integer variable using BufferReader, it would be like this:-
int a=Integer.parseInt(buff.readLine());
For String:-
String name=buff.readLine();
For char:-
char character=(char)buff.read();
If you want to input the value of String using Scanner:-
String name=scan.nextLine();
or
String name=scan.next();
For char:-
String string;
char character;
string=scan.next();
character=string.charAt(0);
Comments