View
386
Download
14
Category
Tags:
Preview:
Citation preview
Program 1WAP to find greatest of three numbers.
class ABC
{
void biggest(int x , int y , int z)
{
if((x>y)&&(x>z))
System.out.println(x+"is greatest of "+x+", "+y+"," +z);
else if((y>x)&&(y>z))
System.out.println(y+"is greatest of "+x+"," +y+"," +z);
else if((z>x)&&(z>y))
System.out.println(z+" is greatest of "+x+"," +y+"," +z);
}
}
class greatest
{
public static void main(String args[])
{
ABC a =new ABC();
a.biggest(5,6,7);
}
}
OutputD:\LAB>javac greatest.java
D:\LAB>java greatest
7 is greatest of 5,6,7
Program 2WAP to calculate factorial of a number using command line arguments.
class factorial
{
public static void main(String args[])
{
int fact=1;
String s=args[0];
System.out.println("The number is :"+s);
int n=Integer.parseInt(s);
for(int i=1;i<n;i++)
fact*=i;
System.out.println("The factorial of "+n+" is "+fact);
}
}
Output
D:\LAB>javac factorial.java
D:\LAB>java factorial 9
The number is :9
The factorial of 9 is 40320
Program 3WAP to read a set of numbers in an array & to find the sum and average of them.
class sum_avg
{
public static void main(String args[])
{
int sum=0;
int[] a={10,20,30,40,50};
for(int i=0;i<5;i++)
{
sum+=a[i];
}
float avg=sum/5;
System.out.println("Sum is "+sum);
System.out.println("Average is "+avg);
}
}
OUTPUTD:\LAB>javac sum_avg.java
D:\LAB>java sum_avg
Sum is 150
Average is 30.0
Program 4WAP a program ro maintain the student record containing roll number , Name, marks1, marks2, marks3 as data member and getdata(), display() and setdata() as member functions.
class record
{
int rollno;
String name;
int marks1;
int marks2;
int marks3;
void getdata(int rn, String nam, int mar1, int mar2 , int mar3)
{
rollno=r;
name=n;
marks1=m1;
marks2=m2;
marks3=m3;
}
void display()
{
System.out.println("Roll No :" +rollno);
System.out.println("Name :" +name);
System.out.println("Marks 1 :"+marks1);
System.out.println("Marks 2 :"+marks2);
System.out.println("Marks 3 :"+marks3);
}
}
class student
{
public static void main(String args[])
{
record r=new record();
r.getdata(12,"Sugandha",98,97,96);
r.display();
}
}
OutputD:\LAB>javac student.java
D:\LAB>java student
Rollno:12
Name :Sugandha
Marks1 :98
Marks2 :97
Marks3 :96
Program 5WAP to increment the employee salaries on the basis of there designation(Manager-5000, General Manager-10000, CEO-20000, worker-2000). Use employee name, id, designation , salary as data member and inc_sal as member function.class salary
{
String name;
int emp_id;
String desig;
float salary;
void getdata(String nam , int no, String des, float sal)
{
String name=nam;
int emp_id=no;
String desig=des;
float salary=sal;
}
float inc_sal(String des,int s)
{
if (des=="Manager")
s=s+5000;
else if(des=="GeneralManager")
s=s+10000;
else if(des=="CEO")
s=s+20000;
else if(des=="Worker")
s=s+2000;
return s;
}
void putdata(String n,int num,String d,float sal)
{
System.out.println("Name: "+n);
System.out.println("Employee id "+num);
System.out.println("Designation"+d);
System.out.println("Incremented salary "+sal);
}
}
class employee
{
public static void main(String args[])
{
salary s1=new salary();
s1.getdata("Sugandha",1234,"CEO",8900)
float insal=s1.inc_sal("CEO",8900);
s1.putdata("Sugandha",1234,"CEO",insal);
}
}
OutputD:\LAB>javac employee.java
D:\LAB>java employee
Name: Sugandha
Employee id 1234
DesignationCEO
Incremented salary 28900.0
Program 6Write a class bank, containing data member: Name of Depositor, A/c type, Type of A/c, Balance amount. Member function: To assign initial value, To deposit an amount , to withdraw an amount after checking the balance (which should be greater than Rs. 500) , To display name & balance.class account
{
String name;
String type;
float amount;
account(String n,String t,float amt)
{
name=n;
type=t;
amount=amt;
}
void getdata()
{
System.out.println("Account details");
System.out.println("Name :"+name);
System.out.println("Account type :"+type);
System.out.println("Amount :"+amount);
}
void deposit(int am)
{
System.out.println("Amount Deposited is:"+am);
amount=amount+am;
}
void withdraw(int amnt)
{
if(amnt>=500)
{
System.out.println("Amount withdrawn is:"+amnt) ;
amount=amount-amnt ;
}
}
void display()
{
System.out.println("After transaction.....................");
System.out.println("Name:"+name);
System.out.println("Balance amount:"+amount);
}
class bank
{ public static void main(String args[])
{
account a1=new account("Sugandha","Saving",2000);
a1.getdata();
a1.deposit(200);
a1.display();
a1.withdraw(500);
a1.display();
}
}
OUTPUTD:\LAB>javac bank.java
D:\LAB>java bank
Account details
Name :Sugandha
Account type :Saving
Amount :2000.0
Amount Deposited is:200
After transaction.....................
Name:Sugandha
Balance amount:2200.0
Amount withdrawn is:500
After transaction.....................
Name:Sugandha
Balance amount:1700.0
Program 7Design three classes: Student, Exam and Result. The student class has data members such as roll no, name etc. Create a class Exam by inheriting the Student class. The Exam class adds data members representing the marks scored in six subjects. Derive the Result from class Exam and it has its own members such as total marks and average. Calculate the total marks and average.class student
{ int roll;
String name;
void getdata(int r , String n)
{
roll=r;
name=n;
System.out.println("Roll no :"+roll);
System.out.println("Name :"+name);
}
}
class exam extends student
{
int[] marks=new int[6];
void getmarks(int m[])
{
for(int i=0;i<6;i++)
{
marks[i]=m[i];
System.out.println("Marks "+(i+1)+" :"+marks[i]);
}
}
}
class result extends exam
{
float tot;
float avg;
void compute(int marks[])
{
for(int i=0;i<6;i++)
tot=tot+marks[i];
avg=tot/6;
System.out.println("Total :"+tot);
System.out.println("Average :"+avg);
}
}
class inherit
{
public static void main(String args[])
{
result r=new result();
int m[]={70,78,79,80,81,82};
r.getdata(26,"Sugandha");
r.getmarks(m);
r.compute(m);
}
}
OUTPUT
D:\LAB>javac inherit.java
D:\LAB>java inherit
Roll no :26
Name :Sugandha
Marks 1 :70
Marks 2 :78
Marks 3 :79
Marks 4 :80
Marks 5 :81
Marks 6 :82
Total :470.0
Average :78.333336
Program 8Calculate area of different geometrical figures(circle, rectangle,square, triangle) using function overloading.class A
{
double ar;
void area(double radius)
{
ar=3.14*radius*radius;
System.out.println("Area:"+ar);
}
void area(int length ,int breadth)
{
ar=length*breadth;
System.out.println("Area:"+ar);
}
void area(int side)
{
ar=side*side;
System.out.println("Area:"+ar);
}
void area(double height,double base)
{
ar=(height*base)/2;
System.out.println("Area:"+ar);
}
}
class over
{
public static void main(String args[])
{
A o=new A();
System.out.println("Circle");
o.area(9.2);
System.out.println("Rectangle");
o.area(10,12);
System.out.println("Square");
o.area(10);
System.out.println("Triangle");
o.area(2.2,3.5);
}
}
OUTPUTD:\LAB>javac over.java
D:\LAB>java over
Circle
Area:265.76959999999997
Rectangle
Area:120.0
Square
Area:100.0
Triangle
Area:3.8500000000000005
Program 9Create a class Employee. Derive 3 classes from this class namely, Programmer, Analyst & Project Leader. Take attributes and operations on your own. interface employee
{ String name="Sugandha";
int emp_id=7456;
float sal=50000;
void putdata();
}
class Programmer implements employee
{ public void putdata()
{
float temp=20000+sal;
System.out.println("Programmer Details:");
System.out.println("Programmer's Name:"+name);
System.out.println("Programmer's id:"+emp_id);
System.out.println("Programmer's salary:"+temp);
}
}
class Analyst implements employee
{
public void putdata()
{
float temp=sal+10000;
System.out.println("Analyst Details:");
System.out.println("Analyst Name:"+name);
System.out.println("Analyst id:"+emp_id);
System.out.println("Analyst salary:"+temp);
}
}
class projectLeader implements employee
{
public void putdata()
{
float temp=3*sal;
System.out.println("Project Leader Details:");
System.out.println("Project Leader's Name:"+name);
System.out.println("Project Leader's id:"+emp_id);
System.out.println("Project Leader's salary:"+temp);
}
}
class emp
{ public static void main(String args[])
{
Programmer p=new Programmer();
p.putdata();
Analyst a=new Analyst();
a.putdata();
projectLeader pj=new projectLeader();
pj.putdata();
}
}
OutputD:\LAB>javac emp.java
D:\LAB>java emp
Programmer Details:
Programmer's Name:Sugandha
Programmer's id:7456
Programmer's salary:70000.0
Analyst Details:
Analyst Name:Sugandha
Analyst id:7456
Analyst salary:60000.0
Project Leader Details:
Project Leader's Name:Sugandha
Project Leader's id:7456
Project Leader's salary:150000.0
Program 10WAP to implement multiple Inheritance using Interface.
interface Area
{
float compute(float x, float y);
}
class Rectangle implements Area
{
public float compute(float x, float y)
{
return(x * y);
}
}
class Triangle implements Area
{
public float compute(float x,float y)
{
return(x * y/2);
}
}
class InterfaceArea
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Triangle tri = new Triangle();
Area area;
area = rect;
System.out.println("Area Of Rectangle = "+ area.compute(1,2));
area = tri;
System.out.println("Area Of Triangle = "+ area.compute(10,2));
}
}
OUTPUTD:\LAB>javac InterfaceArea.java
D:\LAB>java InterfaceArea
Area Of Rectangle = 2.0
Area Of Triangle = 10.0
Program 11WAP to create Student class in package1 and Marks class in package2 which inherit Student class. Calculate the total and average of marks in Result class.package p1;public class Student1{ String name; int roll; public Student1(String n,int r) { String name= new String(); name=n ; roll=r; System.out.println("Name :"+n); System.out.println("Roll no :"+r); }}
package p2;import p1.*;class marks extends Student1 { int mrk1; int mrk2; int mrk3; marks(String n,int r,int a,int b,int c) { super(n,r); mrk1=a; mrk2=b; mrk3=c; } }class result extends marks{ int total; double avg;
result(String n,int r,int a,int b,int c)
{ super(n,r,a,b,c); total= mrk1+mrk2+mrk3; avg=total/3; System.out.println("Total marks:"+total); System.out.println("Avg :"+avg); }}
class pack{ public static void main(String args[]) { result r1=new result("sugandha",26,90,70,80); }}
Output
Name :SugandhaRoll no :26Total marks:240Avg :80.0
Program 12WAP to handle ArithmeticException and ArrayIndexOutOfBounds Exception.
class except{
public static void main(String args[]){
int a[]={5,10};int b=5;try{
int x=a[2]/(b-a[0]);}catch(ArithmeticException e){
System.out.println("Divide by 0 error !!!");}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array index error !!!");}finally{
int y=a[1]/a[0];System.out.println("Handling the exception...");System.out.println("Y :"+y);
}}
}
Output
D:\LAB>javac except.javaD:\LAB>java exceptArray index error !!!Handling the exception...Y :2
Program 13WAP to create and handle your own Exceptionimport java.lang.Exception;
class Myexception extends Exception
{
Myexception(String msg)
{
super(msg);
}
}
class TEST1
{
public static void main(String args[])
{
int x=5,y=1000;
try
{
float z=(float)x/(float)y;
if(z<0.01)
{
throw new Myexception("No is too small");
}
}
catch (Myexception e)
{
System.out.println(e.getMessage());
}
}
}
OUTPUTD:\LAB>javac TEST1.java
D:\LAB>java TEST1
No is too small
Program 14WAP to create a Thread by extending Thread class.
class MyThread extends Thread{
MyThread(String s){
super(s);start();
}
public void run(){
for(int i=0;i<5;i++){
System.out.println("Thread Name :"+Thread.currentThread().getName());try{
Thread.sleep(1000);}catch(Exception e){}
}
}
}
public class MultiThread1{
public static void main(String args[]){
System.out.println("Thread Name :"+Thread.currentThread().getName()); MyThread m1=new MyThread("My Thread 1");MyThread m2=new MyThread("My Thread 2");MyThread m3=new MyThread("My Thread 3");MyThread m4=new MyThread("My Thread 4");
}
}
OutputD:\LAB>javac MultiThread1.java
D:\LAB>java MultiThread1
Thread Name :main
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 4
Thread Name :My Thread 3
Thread Name :My Thread 1
Thread Name :My Thread 4
Thread Name :My Thread 3
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 3
Thread Name :My Thread 2
Thread Name :My Thread 4
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 4
Thread Name :My Thread 3
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 3
Thread Name :My Thread 4
Program 15WAP to create a Thread by implementing Runnable interfaceclass MyThread1 implements Runnable
{
Thread t;
MyThread1(String s)
{
t=new Thread(this,s);
t.start();
}
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("Thread Name :"+Thread.currentThread().getName());
try
{
Thread.sleep(1000);
}catch(Exception e){}
}
}
}
public class RunnableThread1
{
public static void main(String args[])
{
System.out.println("Thread Name :"+Thread.currentThread().getName());
MyThread1 m1=new MyThread1("My Thread 1");
MyThread1 m2=new MyThread1("My Thread 2");
MyThread1 m3=new MyThread1("My Thread 3");
}
}
OutputD:\LAB>javac RunnableThread1.java
D:\LAB>java RunnableThread1
Thread Name :main
Thread Name :My Thread 1
Thread Name :My Thread 3
Thread Name :My Thread 2
Thread Name :My Thread 3
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 3
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 3
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 3
Thread Name :My Thread 2
Program 16Program to read a number from keyboard using BufferedReader classes & to find out whether the number is prime or not.
import java.io.*;
class BRead
{
public static void main(String args[]) throws IOException
{
char c;
int i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter The number");
c=(char) br.read();
for(i=2;i<c;i++)
{
int x=(c%2);
if(x==0)
{
System.out.println(c+ "is not a prime number");
break;
}
}
if(i==c)
System.out.println(c+ "is a prime number");
}
}
OUTPUTD:\LAB>javac BRead.java
D:\LAB>java BRead
Enter The number
7
7is a prime number
Program 17Program to design a simple Applet and show it within web browser.Java Code
import java.awt.*;
import java.applet.*;
public class a extends Applet
{
public void paint(Graphics g)
{
g.drawString("This is an applet",100,200);
}
}
html code
<html>
<head>
<title> Welcome To applet
</title>
</head>
<body> Hello Every one !!!
<applet
code=a.class
width=400
height=600 >
</applet>
</body>
</html>
Output
Program 18Program to design a Frameimport java.awt.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.*;
class FIR extends JFrame
{
Font dFont=new Font("courier new",Font.PLAIN+Font.BOLD,12);
JLabel l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14,l15;
JTextField t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13;
JButton b1,b2,b3;
JFrame fra1;
public FIR ()
{
fra1=new JFrame();
fra1.setResizable(false);
fra1.setTitle("Vehicle Investigation System");
fra1.setFont(dFont);
fra1.setLayout(null);
l1=new JLabel("Vehicle Details");
l1.setBounds(150,40,160,25);
fra1.add(l1);
l2=new JLabel("Registration Number");
l2.setBounds(30,70,160,25);
t1=new JTextField();
t1.setBounds(220,70,160,25);
fra1.add(l2);
fra1.add(t1);
l3=new JLabel("Vehicle Number");
l3.setBounds(30,100,160,25);
t2=new JTextField();
t2.setBounds(220,100,160,25);
fra1.add(l3);
fra1.add(t2);
l4=new JLabel("Vehicle Class");
l4.setBounds(30,130,160,25);
t3=new JTextField();
t3.setBounds(220,130,160,25);
fra1.add(l4);
fra1.add(t3);
l5=new JLabel("Vehicle Make/Model");
l5.setBounds(30,160,160,25);
t4=new JTextField();
t4.setBounds(220,160,160,25);
fra1.add(l5);
fra1.add(t4);
l6=new JLabel("Manufacturing Year");
l6.setBounds(30,190,160,25);
t5=new JTextField();
t5.setBounds(220,190,160,25);
fra1.add(l6);
fra1.add(t5);
l7=new JLabel("Engine Number");
l7.setBounds(30,220,160,25);
t6=new JTextField();
t6.setBounds(220,220,160,25);
fra1.add(l7);
fra1.add(t6);
l8=new JLabel("Vehicle Identification Number ");
l8.setBounds(30,250,200,25);
t7=new JTextField();
t7.setBounds(220,250,160,25);
fra1.add(l8);
fra1.add(t7);
l9=new JLabel("Fuel Used");
l9.setBounds(30,280,160,25);
t8=new JTextField();
t8.setBounds(220,280,160,25);
fra1.add(l9);
fra1.add(t8);
l10=new JLabel("Colour");
l10.setBounds(30,310,160,25);
t9=new JTextField();
t9.setBounds(220,310,160,25);
fra1.add(l10);
fra1.add(t9);
l11=new JLabel("Owner Details");
l11.setBounds(150,350,160,25);
fra1.add(l11);
l12=new JLabel("Owner’s Name ");
l12.setBounds(30,380,160,25);
t10=new JTextField();
t10.setBounds(220,380,160,25);
fra1.add(l12);
fra1.add(t10);
l13=new JLabel("Owner’s Phone No.");
l13.setBounds(30,410,160,25);
t11=new JTextField();
t11.setBounds(220,410,160,25);
fra1.add(l13);
fra1.add(t11);
l14=new JLabel("Owner’s Address");
l14.setBounds(30,440,160,25);
t12=new JTextField();
t12.setBounds(220,440,160,25);
fra1.add(l14);
fra1.add(t12);
l15=new JLabel("Owner’s E-mail-ID");
l15.setBounds(30,470,160,25);
t13=new JTextField();
t13.setBounds(220,470,160,25);
fra1.add(l15);
fra1.add(t13);
b1=new JButton("Submit");
b1.setBounds( 150,530,90,25);
b2=new JButton("Reset");
b2.setBounds( 250,530,90,25);
b3=new JButton("Back");
b3.setBounds( 350,530,90,25);
fra1.add(b1);
fra1.add(b2);
fra1.add(b3);
fra1.setSize(500,700);
fra1.show();
fra1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String s[])
{
FIR v2=new FIR();
}
}
OUTPUT
Program 19(a)WAP to demonstrate mouse event handlerimport java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class mouse extends Applet implements MouseMotionListener
{
public void mouseMoved(MouseEvent me)
{
int a=me.getX();
int b=me.getY();
showStatus(a+" , "+b);
}
public void mouseDragged(MouseEvent me)
{
}
public void init()
{
addMouseMotionListener(this);
}
}
OUTPUT
Program 19(B)WAP to demonstrate key event handlerimport java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class key extends Applet implements KeyListener
{
TextField tf,tf1;
public void keyPressed(KeyEvent ke)
{
int a=ke.getKeyCode();
tf1.setText(Integer.toString(a));
}
public void init()
{
tf=new TextField(20);
tf1=new TextField(20);
tf.addKeyListener(this);
add(tf);
add(tf1);
}
public void keyReleased(KeyEvent ke)
{
}
public void keyTyped(KeyEvent ke)
{
}
}
OUTPUT
Program 20Program to design the interface of calculator using Grid Layoutimport java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Cal extends Applet
implements ActionListener
{
String msg=" ";
int v1,v2,result;
TextField t1;
Button b[]=new Button[10];
Button add,sub,mul,div,clear,mod,EQ;
char OP;
public void init()
{
Color k=new Color(100,100,100);
setBackground(k);
t1=new TextField(10);
GridLayout gl=new GridLayout(4,4);
setLayout(gl);
for(int i=0;i<10;i++)
{
b[i]=new Button(""+i);
}
add=new Button("+");
sub=new Button("-");
mul=new Button("*");
div=new Button("/");
mod=new Button("%");
clear=new Button("Clr");
EQ=new Button("=");
t1.addActionListener(this);
add(t1);
for(int i=0;i<10;i++)
{
add(b[i]);
}
add(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
char ch=str.charAt(0);
if ( Character.isDigit(ch))
t1.setText(t1.getText()+str);
else
if(str.equals("+"))
{
v1=Integer.parseInt(t1.getText());
OP='+';
t1.setText("");
}
else if(str.equals("-"))
{
v1=Integer.parseInt(t1.getText());
OP='-';
t1.setText("");
}
else if(str.equals("*"))
{
v1=Integer.parseInt(t1.getText());
OP='*';
t1.setText("");
}
else if(str.equals("/"))
{
v1=Integer.parseInt(t1.getText());
OP='/';
t1.setText("");
}
else if(str.equals("%"))
{
v1=Integer.parseInt(t1.getText());
OP='%';
t1.setText("");
}
if(str.equals("="))
{
v2=Integer.parseInt(t1.getText());
if(OP=='+')
result=v1+v2;
else if(OP=='-')
result=v1-v2;
else if(OP=='*')
result=v1*v2;
else if(OP=='/')
result=v1/v2;
else if(OP=='%')
result=v1%v2;
t1.setText(""+result);
}
if(str.equals("Clr"))
{
t1.setText("");
}
}
}
OUTPUT
Recommended