Scanner.
java
Source Code
import java.util.Scanner;
public class ScannerAdd {
public static void main(String args [])
{ Scanner keyboard= new Scanner(System.in);
System.out.println("Enter two integers: ");
int x=keyboard.nextInt();
int y=keyboard.nextInt();
int z=x+y;
System.out.println("Sum = "+ z);
}
}
Output
Enter two integers:
8
5
Sum = 13
BufferedReader.java
Source Code
import java.io.*;
public class BufferedReader{
public static void main(String[] args) throws IOException{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.println("Enter two decimal values:");
String s1 = br.readLine();
String s2 = br.readLine();
Double d1 = Double.parseDouble(s1);
Double d2 = Double.parseDouble(s2);
System.out.println("First: "+d1+"\nSecond: "+d2);
System.out.println("Sum: "+(d1+d2));
System.out.printf("Sum: %.2f",(d1+d2));
}
}
Output
Enter two integers:
3
4
Sum = 7
CommandLineArgument.java
Source Code
public class CommandLineArgument {
public static void main(String ar[]){
int a,b,c;
System.out.println("Enter two integers:");
a=Integer.parseInt(ar[0]);
b=Integer.parseInt(ar[1]);
c=a+b;
System.out.println("sum is " +c);
}
}
}
Output
Enter two integers:
4
6
10
Cylinder.java
Source Code
import java.util.Scanner;
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(System.in);
System.out.println("Enter the Height and Radius of
Cylinder");
h=keyboard.nextFloat();
r=keyboard.nextFloat();
Cylinder c1=new Cylinder(h,r);
System.out.println("Surface Area and Volume of cylinder
is:");
System.out.printf("Surface Area: " + c1.surfaceArea()+
"Volume: " + c1.volume());
Output
Enter the Height and Radius of Cylinder
10
15
Surface Area and Volume of cylinder is:
Surface Area: 2356.1925Volume: 7068.5775
Cone.java
Source Code
import java.util.Scanner;
import java.lang.Math;
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=Math.pow(height, 2)+Math.pow(radius,
2);
return Math.sqrt(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(System.in);
System.out.println("Enter the Height and
Radius of Cone");
h=keyboard.nextFloat();
r=keyboard.nextFloat();
Cylinder c1=new Cylinder(h,r);
System.out.println("Surface Area and Volume of
cone is:");
System.out.printf("Surface Area: " +
c1.surfaceArea()+ "Volume: " + c1.volume());
}
}
Output
Enter the Height and Radius of Cone
12
19
Surface Area and Volume of cone is:
Surface Area: 3700.79302Volume: 13609.367880000002
CubeTest.java
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[]) {
System.out.println("Length = "+ Cube.length);
System.out.println("Volume of Cube = "+
Cube.getvolume());
}
}
Output
Length = 10
Volume of Cube = 1000
AreaTest.java
Source Code
import java.util.Scanner;
class Area {
//instance variable
private float length,breadth;
//parameterized constructor
public Area(float length,float breadth ) {
this.length=length;
this.breadth=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 Math.sqrt(length*length+breadth*breadth);
}
}
public class AreaTest{
public static void main(String args[]) {
float l,b;
Scanner keyboard =new Scanner (System.in);
System.out.println("Enter the length and breadth of
rectangle:");
l=keyboard.nextInt();
b=keyboard.nextInt();
Area a1= new Area();
Area a2= new Area(l,b);
System.out.println("The Area of default rectangle is:");
System.out.println(a1.area());
System.out.println("The Area of customized rectangle
is:");
System.out.println(a2.area());
}
}
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