Java File
Java File
class Fib
{
public static void main(String args[])
{
int a=-1,b=1,c;
System.out.println("Fibonaccie series is ::");
for(int i=1;i<=10;i++)
{
c=a+b;
System.out.print(" "+c);
a=b;
b=c;
}}}
Output:
Fibonaccie Series is ::
0 1 1 2 3 5 8 13 21 34
12
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
Program 12: To print grades for given input percentage from command line using
nested if-else.
class Grade{
String g;
double per;
public Grade(double per){
g="";
this.per=per;
}
public void grade(){
if(per>=80)
g="A+";
else if(per<80 && per>=70)
g="A";
else if(per<70 && per>=60)
g="B+";
else if(per<60 && per>=50)
g="B";
else if(per<50 && per>=40)
g="C";
else
g="Fail";
}
void display(){
System.out.println("Per(%)="+per+"\tGrade="+g);
}}
class GradeExp{
public static void main(String args[]){
double p=Double.parseDouble(args[0]);
Grade ami=new Grade(p);
ami.grade();
ami.display();
}}
Output:
Run using it by command line as:
Java GradeExp 75
Per(%)=75.0 Grade=A
13
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
class PrimeFirstN {
public static void main(String args[])
{
int n=11,t=0,num=1,c;
while(t!=n)
{
c=0;
for(int j=2;j<num/2;j++) {
if(num%j==0)
c++;
}
if(c==0) {
System.out.print("\t"+num);
t++;
}
num++;
}}}
Output:
1 2 3 5 7 9 11 13 17 19 23
14
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
class Prime{
public static void main(String args[]){
int n=Integer.parseInt(args[0]);
Prime p=new Prime();
p.listprime(n);
}
public void listprime(int a){
int i,j,c;
System.out.println("prime no.s upto "+a);
for(i=2;i<=a;i++){
c=0;
for(j=1;j<=i;j++){
if(i%j==0){
c++;
}}
if(c==2){
System.out.println(i);
}}}}
Output:
Run using it by command line as:
Java Prime 20
Prime no.s up to 20
2
3
5
7
11
13
17
19
15
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
class Reverse
{
public static void main(String args[])
{
int n=Integer.parseInt(args[0]);
Reverse r=new Reverse();
r.rev(n);
}
public void rev(int a)
{
int rem,rev=0;
do
{
rem=a%10;
rev=(rev*10)+rem;
a=a/10;
}while(a!=0);
System.out.println("Reverse number= "+rev);
}
}
Output:
Run using it by command line as:
Reverse number=321
16
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
Output:
1
1 2 3
1 2 3 2 1
17
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
Program 17: Rewrite box program to modify volume method containing return statement.
class Box{
private int l,h,b;
public void getValue(int x,int y,int z){
l=x;
h=y;
b=z;
}
public long volume(){
return(l*h*b);
}
public void print(){
System.out.println("Box dimension is \nl="+l+"\th="+h+"\tb="+b);
}}
public class BoxExp2{
public static void main(String args[]){
long v;
Box ami=new Box();
ami.getValue(5,4,3);
v=ami.volume();
ami.print();
System.out.println("Volume is "+v);
Box ami1=new Box();
ami1.getValue(5,4,3);
v=ami1.volume();
ami1.print();
System.out.println("Volume is "+v);
}}
Output:
Box dimension is
l=5 h=4 b=3
Volume of Box is 60
Box dimension is
l=2 h=6 b=5
Volume is 60
18
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
Program 18: Create a class to compute area of square, rectangle and triangle (use method
overloading concept)
class Area{
public void area(int l){
System.out.println("Area of Square is "+l*l);
}
public void area(int l,int b){
System.out.println("Area of rectangle is "+l*b);
}
public void area(int a,int b,int c){
int s=(a+b+c)/2;
double ar=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("Area of triangle is "+ar);
}}
public class Overload{
public static void main(String args[]){
Area obj=new Area();
obj.area(5);
obj.area(3,8);
obj.area(3,4,5);
}}
Output:
Area os square is 25
Area of rectangle is 24
Area of triangle is 6.0
19
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
class Boxc
{
int l,w,b;
public Boxc()
{
l=1;
w=1;
b=1;
}
public Boxc(int x)
{
l=x;
w=x;
b=x;
}
public Boxc(int x,int y,int z)
{
l=x;
w=y;
b=z;
}
public double volume()
{
return l*w*b;
}
public static void main(String args[])
{
double v1,v2,v3;
Boxc b1=new Boxc();
v1=b1.volume();
Boxc b2=new Boxc(2);
v2=b2.volume();
Boxc b3=new Boxc(2,3,5);
v3=b3.volume();
System.out.println("v1="+v1+"v2="+v2+"v3="+v3);
}
}
Output:
V1=0 V2=8 V3=30
20
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
class Box{
int l,h,b;
public Box(){
System.out.println("\nBox fields initialize with zeroes ");
l=0;
h=0;
b=0;
}
public Box(int x){
System.out.println("\nOne parameters found,Box fields initialize with this
value");
l=x;
h=x;
b=x;
}
public Box(int x,int y,int z){
System.out.println("\nThree parameters found,Box fields initialize with
these value");
l=x;
h=y;
b=z;
}
public void display(){
System.out.println("Box dimension are as::\nl="+l+"\th="+h+"\tb="+b);
}}
public class BoxExp{
public static void main(String args[]){
Box ami=new Box();
ami.display();
Box ami1=new Box(5);
ami1.display();
Box ami2=new Box(4,5,6);
ami2.display();
}}
Output:
Box fields initialize with zero
Box dimension are as ::
l=0 h=0 b=0
One parameter found Box fields initialize with this value
Box dimension are as ::
l=5 h=5 b=5
21
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
class Arithmetic{
int a,b;
public void getVal(int a,int b){
this.a=a;
this.b=b;
}
public void add(){
System.out.println("Addition of "+a+" and "+b+" is "+(a+b));
}
public void sub(){
System.out.println("Subtraction of "+a+" and "+b+" is "+(a-b));
}
public void multi(){
System.out.println("Multiplication of "+a+" and "+b+" is "+(a*b));
}
public void div(){
System.out.println("Division of "+a+" and "+b+" is "+(a/b));
}}
class ArithmeticExp{
public static void main(String args[]){
Arithmetic ami=new Arithmetic();
ami.getVal(8,4);
ami.add();
ami.sub();
ami.multi();
ami.div();
}}
Output:
Addition of 8 and 4 is 12
Subtraction of 8 and 4 is 4
Multiplication of 8 and 4 is 32
Division of 8 and 4 is 2
22
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
Output:
Call by value
Before call the method a=55 b=5
After call the method a=55 b=5
Call by reference
Before call the method a=2 b=3
After call the method a=5 b=9
23
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
Program 23: Create a base class shape. It contains 2 methods get() and print () to
accept and display parameters of shape respectively. Create a subclass
Rectangle. It contains a method to display length and breadth of rectangle
(Method overrifing).
class Shape{
int a,b;
String shapetype;
public Shape(String s){
a=b=0;
shapetype=s;
}
public void get(int a,int b){
this.a=a;
this.b=b;
}
public void print(){
System.out.println("Shape Dimension\ta="+a+"\tb="+b);
}}
class Rectangle extends Shape{
public Rectangle(){
super("Rectangle");
}
public void print(){
System.out.println("Shape type is Rectangle");
super.print();
}}
class ShapeExp {
public static void main(String args[]){
Rectangle ami=new Rectangle();
ami.get(5,6);
ami.print(); } }
Output:
Shape type is rectangle
Shape dimension length=5 breadth=6
24
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
class A{
void print(){
System.out.println("you call the class A method");
}}
class B extends A{
void print(){
System.out.println("you call the class B method");
}}
class C extends A{
void print(){
System.out.println("you call the class A method");
}}
class DynamicMethod {
public static void main(String args[]){
A obj1=new A();
B obj2=new B();
C obj3=new C();
obj1.print();
obj2.print();
obj3.print();
A r;
r=obj2;
r.print();
r=obj3;
r.print(); } }
Output:
You call the class A method
You call the class Bmethod
You call the class C method
You call the class B method
You call the class C method
25
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
Program 25: Write an abtract class Employee with three variables name, sal and
Grosssal , suitable constructors, mprint method and two abstract methods
calculategrosssalary() and annualincrement(). Create a manager sub class
of employee with Hra as member variable and write implimentation of the
abstract method. Also create a subclass of manager as sales manger with
commision as member variable and override the calulategrosssalary() method.
}
public SalesPerson(double b,String c){
ta=0;
com=0;
basic=b;
name=c;
}
double calculateSal(){
ta=1000;
com=basic*5/100;
gross=basic+ta+com;
return gross;
}
void annualIncrement(){
double insal=basic*5/100;
basic=basic+insal;
System.out.println("Annual Increment in salary is "+insal);
}
void print(){
System.out.println("Employee Name::"+name+"\tBasic
pay::"+basic+"\tTa::"+ta+"\tCommision::"+com);
}}
class Emp{
public static void main(String args[]){
double v;
Manager obj1=new Manager(5000.0,"Suneel");
System.out.println("For Manager class \n");
v=obj1.calculateSal();
obj1.print();
System.out.println("Gross Salary::"+v);
obj1.annualIncrement();
v=obj1.calculateSal();
System.out.println("After annual Increment ");
obj1.print();
System.out.println("Gross Salary::"+v);
System.out.println("\n\nFor Sales Person class \n");
SalesPerson obj2=new SalesPerson(5000.0,"Suneel");
v=obj2.calculateSal();
obj2.print();
System.out.println("Gross Salary::"+v);
obj2.annualIncrement();
v=obj2.calculateSal();
System.out.println("After annual Increment ");
obj2.print();
System.out.println("Gross Salary::"+v);
}}
27
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
Program 26: Write a program to show the use of Interfaces (show multipe inheritence as
discussed in class).
class Student{
String name;
int rollNo;
Student(){
name="";
rollNo=0;
}
public final void input(String name,int rollNo){
this.name=name;
this.rollNo=rollNo;
}
public void output(){
System.out.println("Roll No::"+rollNo+"\tName::"+name);
}}
interface Exam
{
final static int TOTAL=300;
public void total(double phy,double chem,double math);
public void per();
}
class StudentDetails extends Student implements Exam{
double phy,chem,math,t,p;
public StudentDetails(){
super();
phy=chem=math=0;
}
public void total(double phy,double chem,double math){
t=phy+chem+math;
}
public void per(){
p=(t/TOTAL)*100;
}
public void output(){
super.output();
System.out.println("Total="+t+"\tMarks(%)="+p);
}}
class Students{
public static void main(String args[]){
StudentDetails ami=new StudentDetails();
ami.input("Arun",105);
ami.total(78,79,88);
ami.per();
ami.output();
}}
28
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
Program 27: Demonstrate Exception handling using single try catch block.
class Exception1{
public static void main(String args[]){
int a=5,b=0,c=0;
try{
c=a/b;
System.out.println("a/b= "+c);
}
catch(ArithmeticException e){
System.out.println("Error Detect:"+e);
}}}
Output:
29
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
class Exception2{
public static void main(String args[]){
int arr[]={3,5,6,7,4,12};
int a=8,b=0,c;
try{
int ar[]=new int[5];
for(int i=0;i<arr.length;i++)
ar[i]=arr[i]*2;
c=a/b;
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Error Detect "+e);
}
catch(NegativeArraySizeException e){
System.out.println("Error Detect "+e);
}
catch(ArithmeticException e){
System.out.println("Error Detect "+e);
}}}
Output:
30
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
class Exception3{
public static void main(String args[]){
int arr[]={3,5,6,7,4,12};
int a=8,b=0,c;
try{
int ar[]=new int[5];
for(int i=0;i<arr.length;i++)
ar[i]=arr[i]*2;
c=a/b;
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Error Detect "+e);
}
catch(NegativeArraySizeException e){
System.out.println("Error Detect "+e);
}
catch(ArithmeticException e){
System.out.println("Error Detect "+e);
}
finally{
System.out.println("Finally block");
for(int i=0;i<arr.length;i++)
System.out.print("\t"+arr[i]);
}}}
Output:
Finally block
3 5 6 7 4 12
31
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
Program 30: Write a program to show use of throw and trhows keyword. Create your own
exception class AgeException. Also create two subclassess TooYoungException and
InvalidAgeException of AgeExcep-tion as discussed in class.
32
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
Program 31:Tto create an applet showing different AWT controls like buttons,check box,list
box,choice,labels
import java.awt.*;
import java.applet.*;
33
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
36
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
import java.awt.*;
import java.applet.*;
inport java.awt.event.*;
37
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
Label l1,l2,l3;
TextField t1,t2,t3;
Button b1,b2,b3,b4;
t1=new TextField(20);
t2=new TextField(20);
t3=new TextField(20);
b1=new Button("+");
b2=new Button("-");
b3=new Button("*");
b4=new Button("/");
add(l1);
add(t1);
add(l2);
add(t2);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
add(b1);
add(b2);
add(b3);
add(b4);
add(l3);
38
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
add(t3);
}
if(s=="+")
{
c=a+b;
}
if(s=="-")
{
c=a-b;
}
if(s=="*")
{
c=a*b;
}
if(s=="/")
{
c=a/b;
}
String r=Double.toString(c);
t3.setText(r);
}
}
39
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
public MenuDemo()
{
add(l1);
MenuBar m=new MenuBar();
setMenuBar(m);
n.addActionListener(this);
o.addActionListener(this);
s.addActionListener(this);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
}
{
String s=e.getActionCommand();
if(s=="New")
l1.setText("New is clicked");
if(s=="Open")
l1.setText("Open is clicked");
if(s=="Save")
l1.setText("Save is clicked");
if(s=="Cut")
l1.setText("Cut is clicked");
if(s=="Copy")
l1.setText("Copy is clicked");
if(s=="Paste")
l1.setText("Paste is clicked");
41
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
import java.io.*;
class ConsoleIO {
public static void main(String[] args) throws IOException {
// In order to read a line at a time from System.in,
// which is type InputStream, it must be wrapped into
// a BufferedReader, which requires wrapping it first
// in an InputStreamReader.
// Note the "throws" clause on the enclosing method (main).
42
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114