Scanner.
java
Source Code
import [Link];
public class ScannerAdd {
public static void main(String args [])
{ Scanner keyboard= new Scanner([Link]);
[Link]("Enter two integers: ");
int x=[Link]();
int y=[Link]();
int z=x+y;
[Link]("Sum = "+ z);
}
}
Output
Enter two integers:
8
5
Sum = 13
[Link]
Source Code
import [Link].*;
public class BufferedReader{
public static void main(String[] args) throws IOException{
InputStreamReader in = new InputStreamReader([Link]);
BufferedReader br = new BufferedReader(in);
[Link]("Enter two decimal values:");
String s1 = [Link]();
String s2 = [Link]();
Double d1 = [Link](s1);
Double d2 = [Link](s2);
[Link]("First: "+d1+"\nSecond: "+d2);
[Link]("Sum: "+(d1+d2));
[Link]("Sum: %.2f",(d1+d2));
}
}
Output
Enter two integers:
3
4
Sum = 7
[Link]
Source Code
public class CommandLineArgument {
public static void main(String ar[]){
int a,b,c;
[Link]("Enter two integers:");
a=[Link](ar[0]);
b=[Link](ar[1]);
c=a+b;
[Link]("sum is " +c);
}
}
}
Output
Enter two integers:
4
6
10
[Link]
Source Code
import [Link];
public class Cylinder {
//instance variable
private float height,radius;
double pi=3.14159;
//constructor
public Cylinder(float h,float r){
height=h;
radius =r;
}
//instance methods
/*------------------------getter*/
public float getheight() {
return height;
}
public float getradius() {
return height;
}
public double areaOfBase() {
return pi*radius*radius;
}
public double volume() {
return (areaOfBase()*height);
}
public double perimeterOfBase() {
return 2*pi*radius;
}
public double outerArea() {
return perimeterOfBase()*height ;
}
public double surfaceArea() {
return outerArea()+2*areaOfBase() ;
}
public static void main(String args[]) {
float h,r;
Scanner keyboard=new Scanner([Link]);
[Link]("Enter the Height and Radius of
Cylinder");
h=[Link]();
r=[Link]();
Cylinder c1=new Cylinder(h,r);
[Link]("Surface Area and Volume of cylinder
is:");
[Link]("Surface Area: " + [Link]()+
"Volume: " + [Link]());
Output
Enter the Height and Radius of Cylinder
10
15
Surface Area and Volume of cylinder is:
Surface Area: 2356.1925Volume: 7068.5775
[Link]
Source Code
import [Link];
import [Link];
public class Cone {
//instance variable
private float height,radius;
double pi=3.14159;
//constructor
public Cone(float h,float r){
height=h;
radius =r;
}
//instance methods
/*------------------------getter*/
public float getheight() {
return height;
}
public float getradius() {
return height;
}
public double areaOfBase() {
return pi*radius*radius;
}
public double perimeterOfBase() {
return 2*pi*radius;
}
public double slantHeight() {
double a=[Link](height, 2)+[Link](radius,
2);
return [Link](a);
}
public double surfaceArea() {
return (slantHeight()+radius)*pi*radius;
}
public double volume() {
return 1/3*areaOfBase()*height;
}
public static void main(String args[]) {
float h,r;
Scanner keyboard=new Scanner([Link]);
[Link]("Enter the Height and
Radius of Cone");
h=[Link]();
r=[Link]();
Cylinder c1=new Cylinder(h,r);
[Link]("Surface Area and Volume of
cone is:");
[Link]("Surface Area: " +
[Link]()+ "Volume: " + [Link]());
}
}
Output
Enter the Height and Radius of Cone
12
19
Surface Area and Volume of cone is:
Surface Area: 3700.79302Volume: 13609.367880000002
[Link]
Source Code
class Cube {
//static class fields
static int length=10;
static int breadth =10;
static int height =10;
//static class methods
static int getvolume() {
return length*breadth*height;
}
}
public class CubeTest {
public static void main(String args[]) {
[Link]("Length = "+ [Link]);
[Link]("Volume of Cube = "+
[Link]());
}
}
Output
Length = 10
Volume of Cube = 1000
[Link]
Source Code
import [Link];
class Area {
//instance variable
private float length,breadth;
//parameterized constructor
public Area(float length,float breadth ) {
[Link]=length;
[Link]=breadth;
}
//default no-argument constructor
public Area() {
length=10;
breadth=10;
}
//instance method
public float getlength() {
return length;
}
public float getbreadth() {
return breadth;
}
public double area() {
return length*breadth;
}
public float perimeter() {
return 2*(length+breadth);
}
public double diagonal() {
return [Link](length*length+breadth*breadth);
}
}
public class AreaTest{
public static void main(String args[]) {
float l,b;
Scanner keyboard =new Scanner ([Link]);
[Link]("Enter the length and breadth of
rectangle:");
l=[Link]();
b=[Link]();
Area a1= new Area();
Area a2= new Area(l,b);
[Link]("The Area of default rectangle is:");
[Link]([Link]());
[Link]("The Area of customized rectangle
is:");
[Link]([Link]());
}
}
Output
Enter the length and breadth of rectangle:
10
20
The Area of default rectangle is:
100.0
The Area of customized rectangle is:
200.0