Академический Документы
Профессиональный Документы
Культура Документы
AIM:
To prepare a student mark list implementing classes and objects using java.
PROGRAM:
import java.io.*;
class student1
{
String name,regno,res,grade;
int m1,m2,m3,total;
float avg;
void getdata()throws IOException
{
System.out.println("ENTER THE NAME:");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
name=br.readLine();
System.out.println("ENTER THE REGISTER NUMBER:");
regno=br.readLine();
System.out.println("ENTER THE MARK 1:");
m1=Integer.parseInt(br.readLine());
System.out.println("ENTER THE MARK 2:");
m2=Integer.parseInt(br.readLine());
System.out.println("ENTER THE MARK 3:");
m3=Integer.parseInt(br.readLine());
}
void calculate()
{
total=m1+m2+m3;
avg=total/(float)3;
if((m1>=40)&&(m2>=40)&&(m3>=40))
{
res="PASS";
if(avg>=80)
grade="1ST CLASS";
else if((avg<=79)&&(avg>=60))
grade="2ND CLASS";
else if((avg<=59)&&(avg>=40))
grade="3RD CLASS";
}
else
{
res="FAIL";
grade="\n------\n FAIL \n------";
RESULT:
The preparation of student mark list using classes and objects has been executed
successfully.
AIM:
PROGRAM:
import java.io.*;
class stud
{
String name,regno,res;
int m1,m2,m3,total;
float avg;
void getdata()throws IOException
{
System.out.println("ENTER THE NAME:");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
name=br.readLine();
System.out.println("ENTER THE REGISTER NUMBER:");
regno=br.readLine();
System.out.println("ENTER THE 1ST MARK:");
m1=Integer.parseInt(br.readLine());
System.out.println("ENTER THE 2ND MARK:");
m2=Integer.parseInt(br.readLine());
System.out.println("ENTER THE 3RD MARK:");
m3=Integer.parseInt(br.readLine());
}
void calculate()
{
total=m1+m2+m3;
avg=total/(float)3;
if((m1>=40)&&(m2>=40)&&(m3>=40))
{
res="PASS";
}
else
{
res="FAIL";
}
}
void display()
{
System.out.println(name+"\t\t\t"+regno+"\t\t"+m1+"\t\t\t"+m2+"\t\t\t"+m3
+"\t\t\t"+total+"\t\t\t"+avg+"\t\t"+res+"\t\t\t");
}
RESULT:
The preparation of student mark list using array of objects has been executed
successfully.
PROGRAM:
class area
{
int l,b;
area()
{
l=10;b=15;
}
area(int x,int y)
{
l=x;b=y;
}
area(area a)
{
l=a.l;
b=a.b;
}
void disp()
{
System.out.println("The area of rectangle is "+(l*b));
}
}
class DemoConstructor
{
public static void main(String args[])
{
area a1=new area();//Default Constructor
area a2=new area(1,3); //parameterized constructor
area a3= new area(a1); // Copy constructor
a1.disp();
a2.disp();
a3.disp();
}
}
RESULT:
The area of rectangle using all types of constructors has been found successfully.
AIM:
PROGRAM:
class Rectangle
{
double length, breath;
double Area(double l, double b)
{
length=l;
breath=b;
return length*breath;
}
double Area(double a )
{ //overloading method
length=a;
breath=a;
return length*breath;
}
}
class Overload
{
public static void main(String args[])
{
double area;
Rectangle rec=new Rectangle();
area=rec.Area(7.5,4.4);
System.out.println("Area of rectangle is "+area);
area=rec.Area(8.5);
System.out.println("Area of square is "+area);
}
}
RESULT:
The area of rectangle and square using method overloading has been found
successfully.
To find area, volume and surface area of a cylinder implementing method overriding
using java.
PROGRAM:
class circle
{
double radius;
final double PI=3.14;
circle(double r)
{
radius=r;
}
double Area()
{
return PI*radius*radius;
}
double Circum()
{
return 2*PI*radius;
}
}
class Cylinder extends circle
{
double height;
Cylinder(double r,double h)
{
super(r);
height=h;
}
double Area()
{
return Circum()*height;
}
double Volume()
{
return PI*radius*radius*height;
}
}
class MethodOverring
RESULT:
The area, volume and surface area of a cylinder using method overriding has been
found successfully.
To find area and volume of a room implementing single inheritance using java.
PROGRAM:
class Room
{
int length,breadth;
Room (int x,int y)
{
length=x;
breadth=y;
}
int area()
{
return (length*breadth);
}
}
class BedRoom extends Room
{
int height;
BedRoom(int x,int y,int z)
{
super(x,y);
height=z;
}
int volume()
{
return(length*breadth*height);
}
}
class SingleInheritance
{
public static void main(String args[])
{
BedRoom room1=new BedRoom(4,5,10);
int area1=room1.area(); int volume1=room1.volume();
OUTPUT:
RESULT:
The area and volume of a room implementing single inheritance has been found
successfully.
AIM:
PROGRAM:
import java.io.*;
class students
{
String name,rollno;
void input()throws IOException
{
System.out.println("ENTER THE NAME:");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
name=br.readLine();
System.out.println("ENTER THE REGISTER NUMBER:");
rollno=br.readLine();
}
void output()
{
System.out.println("NAME:" +name);
System.out.println("REGISTER NUMBER:" +rollno);
}
}
class semesters extends students
{
int m1,m2,m3;
void getdata()throws IOException
{
System.out.println("ENTER THE 1ST MARK: ");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
m1=Integer.parseInt(br.readLine());
System.out.println("ENTER THE 2ND MARK: ");
m2=Integer.parseInt(br.readLine());
System.out.println("ENTER THE 3RD MARK: ");
m3=Integer.parseInt(br.readLine());
}
void putdata()
{
System.out.println("MARK 1: " +m1);
System.out.println("MARK 2: " +m2);
System.out.println("MARK 3: " +m3);
}
RESULT:
The student mark list implementing multi-level inheritance has been created
successfully.
To find sum and factorial of given numbers implementing hybrid inheritance using
java.
PROGRAM:
class a
{
int i,j;
void show()
{
System.out.println("Value of i is "+i);
System.out.println("Value of j is "+j);
}
}
class b extends a
{
void sum()
{
int k=i+j;
System.out.println("Sum of i and j is "+k);
}
}
class c extends a
{
void fact()
{
int j=1;
for(int a=1;a<=i;a++)
{
j=j*a;
}
System.out.println("The factorial value is "+j);
}
}
class Final extends c
{
public static void main(String arg[])
{
a obj=new a();
obj.i=10;
obj.j=20;
obj.show();
b obj1=new b();
obj1.i=100;
obj1.j=200;
OUTPUT:
RESULT:
The sum and factorial of given numbers implementing hybrid inheritance has been
found successfully.
AIM:
To find area of square and cube implementing hierarchical inheritance using java.
PROGRAM:
class side
{
int l;
void set_value(int x)
{
l=x;
}
}
class square extends side
{
int sq()
{
return(l*l);
}
}
class cube extends side
{
int cub()
{
return(l*l*l);
}
}
class Hierarchical
{
public static void main(String[] args)
{
square s=new square();
{
s.set_value(10);
System.out.println("Area of the square is " +s.sq());
}
cube c=new cube();
{
c.set_value(20);
System.out.println("Area of the cube is " +c.cub());
}
}
}
RESULT:
The area of square and cube implementing hierarchical inheritance has been found
successfully.
AIM:
PROGRAM:
//Step 1: Create a package named “college” & a class named “course.java” in it.
package college;
public class course
{
String dept;
String year;
public course(String d,String y)
{
dept=d;
year=y;
}
public void display()
{
System.out.println("Department: "+dept);
System.out.println("Year: "+year);
}
}
//Step 2: Compile (F9) the above program and create another package named “dept” & a
class named “faculty.java” in it.
package dept;
public class faculty
{
String staff_name;
String subject;
public faculty(String sn,String sb)
{
staff_name=sn;
subject=sb;
}
public void display()
{
System.out.println("Name of the faculty: "+staff_name);
System.out.println("Subject handled: "+subject);
}
}
import college.*;
import dept.*;
class disp_final
{
public static void main(String args[])
{
course c=new course("Comp. Sci","II year");
faculty f=new faculty("SDR","JAVA");
c.display();
f.display();
}
}
RESULT:
The college class details implementing packages has been displayed successfully.
AIM:
PROGRAM:
interface Area
{
final static float pi = 3.14f;
float compute (float x, float y); //Intereface implemented
}
class Rect implements Area
{
public float compute(float x, float y)
{
return (x*y);
}
}
class Circle implements Area
{
public float compute (float x, float y)
{
return(pi*x*x);
}
}
class DemoInerface
{
public static void main (String args[])
{
Rect rect = new Rect();
Circle cir = new Circle();
Area area;
area = rect;
System.out.println("Area of Rectangle = "+area.compute(10,20));
area = cir;
System.out.println("Area of Circle = " +area.compute(10,0));
}
}
RESULT:
The area of rectangle and circle implementing interfaces has been found
successfully.
AIM:
PROGRAM:
class ExceptionHandling
{
public static void main(String args[])
{
int vec []={3,5,4,10,2};
int nmr=24;
int quot,sum=0;
for(int i=0;i<=5;i++)
{
try
{
quot=nmr/(2-i);
try
{
sum+=vec[i];
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("\nArray index out of bounds at index
try(inner try)="+i);
}
catch(ArithmeticException e)
{
System.out.println("\nDivide by zero error at i(inner try)="+i);
}
System.out.println("\nQuotient ="+quot);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("\n Array index out of bounds at index(outer
try)"+i);
}
Quotient =12
Finally Block
Quotient =24
Finally Block
Quotient =-24
Finally Block
Quotient =-12
Finally Block
Quotient =-8
Finally Block
Sum of Numbers=20
RESULT:
AIM:
PROGRAM:
class Printing
{
synchronized void printnumber(int n)
{
System.out.println("Start");
for(int j=n;j>0;j--)
{
try
{
if(j==n/2)
Thread.sleep(100);
}
catch (InterruptedException e)
{
;
}
System.out.print(" "+j);
}
System.out.println("\nEnd");
}
}
class Threadserve implements Runnable
{
int n;
Printing pt;
Thread th;
Threadserve(Printing p,int x)
{
n=x;
pt=p;
th=new Thread(this);
th.start();
}
public void run()
{
pt.printnumber(n);
}
}
Start
16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
End
Start
10 9 8 7 6 5 4 3 2 1
End
Start
87654321
End
RESULT:
AIM:
PROGRAM:
RESULT:
The factorial and sum of given numbers implementing multi-threading has been
found successfully.
AIM:
PROGRAM:
import java.io.*;
import java.util.*;
class Vectorclass
{
public static void main(String agrs[])throws IOException
{
int n,i,x;
Vector v=new Vector(100);
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("\tIMPLEMENTATION OF VECTOR CLASS");
System.out.println("\t* * * * * * * * * * * * * * **");
System.out.println();
System.out.println("Input");
System.out.println("-----");
System.out.println("Enter the value of n");
n=Integer.parseInt(br.readLine());
System.out.println("Enter " + n + " names");
for(i=1;i<=n;i++)
{
String S=br.readLine();
v.addElement(S);
}
System.out.println();
System.out.println("Output");
System.out.println("------");
System.out.println("First name: "+v.firstElement());
System.out.println("Last name: "+v.lastElement());
Enumeration enum1=v.elements();
System.out.println("\nElements in the vector");
System.out.println("----------------------");
while(enum1.hasMoreElements())
{
System.out.println(enum1.nextElement());
}
System.out.println();
}
}
Input
-----
Enter the value of n
3
Enter 3 names
ARAVIND
GIRI
THAMARAI
Output
------
First name: ARAVIND
Last name: THAMARAI
RESULT:
The name list using Vector class utilities has been created successfully.
AIM:
To print current date and time implementing Calendar class using java utilities.
PROGRAM:
import java.io.*;
import java.util.*;
class CalendarClass
{
public static void main(String args[])
{
String months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
"Oct","Nov","Dec"};
String ampm[]={"AM","PM"};
Calendar cal=Calendar.getInstance();
System.out.println();
System.out.println("\t\tCalendar Application for current Date and Time");
System.out.println("\t\t---------------------------------------------------------------");
System.out.println("The current date and time is");
System.out.print("\nDate : ");
System.out.print(cal.get(Calendar.DATE));
System.out.print("-"+(cal.get(Calendar.MONTH)+1));
System.out.print("-"+(cal.get(Calendar.YEAR)));
System.out.println();
System.out.println("Month : "+months[cal.get(Calendar.MONTH)]);
System.out.println("Year : "+cal.get(Calendar.YEAR));
System.out.print("Time : ");
System.out.print(cal.get(Calendar.HOUR));
System.out.print(":"+cal.get(Calendar.MINUTE));
System.out.print(":"+cal.get(Calendar.SECOND));
System.out.print(" "+ampm[cal.get(Calendar.AM_PM)]);
System.out.println();
cal.set(Calendar.HOUR,11);
cal.set(Calendar.MINUTE,30);
cal.set(Calendar.SECOND,28);
System.out.println();
System.out.print("Updated Time: ");
System.out.print(cal.get(Calendar.HOUR));
System.out.print(":"+cal.get(Calendar.MINUTE));
System.out.print(":"+cal.get(Calendar.SECOND));
System.out.print(" "+ampm[cal.get(Calendar.AM_PM)]);
System.out.println();
}
}
Date : 9-10-2014
Month : Oct
Year : 2014
Time : 0:54:44 AM
RESULT:
The current date and time using Calendar class utilities has been displayed
successfully.
AIM:
PROGRAM:
import java.util.Random;
import java.io.*;
class RandomClass
{
public static void main(String args[]) throws IOException
{
int n,x,i,j;
int a[]=new int[10];
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Input");
System.out.println("*****");
System.out.println("How many random numbers you want?");
n=Integer.parseInt(br.readLine());
Random r= new Random();
for(i=0;i<n;i++)
{
x=r.nextInt(100);
a[i]=Math.abs(x);
}
System.out.println("OUTPUT:");
System.out.println("******");
System.out.println("The Random numbers generated are");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
//Sorting Begins
for (i=0;i<n-1;i++)
for (j=i+1;j<n;j++)
{
if (a[i]>=a[j])
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
System.out.println("The Random numbers in Ascending order are");
Input
*****
How many random numbers you want?
5
OUTPUT:
******
The Random numbers generated are
48
7
91
39
15
The Random numbers in Ascending order are
7
15
39
48
91
The Random numbers in Descending order are
91
48
39
15
7
RESULT:
The random numbers using Random class utilities has been displayed successfully.
AIM:
PROGRAM:
import java.applet.*;
import java.awt.*;
public class AppletleLifeCycle extends Applet
{
String mesg1,mesg2,mesg3,mesg4;
public void init()
{
mesg1="message from init method";
}
public void start()
{
mesg2="message form start method";
}
public void stop()
{
mesg3="message form stop method";
}
public void destroy()
{
mesg4="System is destroying your applet";
}
public void paint(Graphics gp)
{
gp.drawString("Demo for basic methods of applet",20,40);
if(mesg1!=null)
gp.drawString(mesg1, 20,80);
if(mesg2!=null)
gp.drawString(mesg2, 20,100);
if(mesg3!=null)
gp.drawString(mesg3, 20,120);
if(mesg4!=null)
gp.drawString(mesg4, 20,140);
}
}
RESULT:
AIM:
PROGRAM:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
public class Mouse1 extends Applet implements MouseListener,MouseMotionListener
{
String txt="Nothing";
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
txt="Mouse Clicked";
repaint();
}
public void mouseEntered(MouseEvent me)
{
txt="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
txt="Mouse Exited";
repaint();
}
public void mousePressed(MouseEvent me)
{
txt="Mouse pressed ";
setForeground(Color.cyan);
repaint();
}
public void mouseReleased(MouseEvent me)
{
txt="Mouse Released";
setForeground(Color.magenta);
repaint();
}
RESULT:
AIM:
PROGRAM:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class DemoKeyEvents extends Applet implements KeyListener
{
String txt=" ";
String txt1=" ";
String txt2=" ";
String txt3=" ";
int kcode;
char ch;
int drawnnumber;
public void init()
{
addKeyListener(this);
requestFocus();
}
public void keyTyped(KeyEvent ke)
{
ch=ke.getKeyChar();
txt1+=ch;
if(txt1.length()>25)
txt1="";
txt ="Key typed";
repaint();
}
public void keyPressed(KeyEvent ke)
{
kcode=ke.getKeyCode();
if(kcode==ke.VK_F1)
txt2="You have typed F1 key";
if(kcode==ke.VK_SHIFT)
txt3="You have typed shift key";
txt="Key Pressed";
repaint();
}
public void keyReleased(KeyEvent ke)
{
RESULT:
AIM:
PROGRAM:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
add(grid);
add(gridbag);
add(flow);
add(card);
add(border);
grid.addActionListener(this);
gridbag.addActionListener(this);
flow.addActionListener(this);
card.addActionListener(this);
border.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if (str.equals("GridLayout"))
g1();
else if(str.equals("GridBagLayout"))
gb1();
else if(str.equals("FlowLayout"))
f1();
else if(str.equals("CardLayout"))
c1();
else
b1();
repaint();
}
public void g1()
{
myframe mf=new myframe();
mf.setSize(300,200);
mf.setBackground(Color.cyan);
mf.setVisible(true);
mf.setTitle("GridLayout");
int n=4;
mf.setLayout(new GridLayout(n,n));
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int k=i*n+j;
if(k>0)
mf.add(new Button(""+k));
}
gbc=new GridBagConstraints();
Button b1=new Button("Button 1");
Button b2=new Button("Button 2");
Button b3=new Button("Button 3");
gbc.gridwidth=GridBagConstraints.REMAINDER;
g.setConstraints(b1,gbc);
mf.add(b1);
gbc.gridwidth=GridBagConstraints.REMAINDER;
g.setConstraints(b2,gbc);
mf.add(b2);
gbc.gridwidth=GridBagConstraints.REMAINDER;
g.setConstraints(b3,gbc);
mf.add(b3);
}
public void f1()
{
myframe mf=new myframe();
mf.setSize(300,200);
mf.setBackground(Color.cyan);
mf.setVisible(true);
mf.setTitle("FlowLayout");
String s[]={"one","two","three"};
mf.setLayout(new FlowLayout());
for(int i=0;i<3;i++)
mf.add(new Button(s[i]));
}
public void c1()
{
myframe mf=new myframe();
Button s1,s2,s3;
CardLayout c;
Panel p;
c=new CardLayout();
p=new Panel();
mf.add(p);
p.setLayout(c);
s1=new Button("Yes");
s2=new Button("No");
s3=new Button("YesNo");
s1.addActionListener(this);
s2.addActionListener(this);
s3.addActionListener(this);
p.add("yes",s1);
p.add("no",s2);
p.add("yesno",s3);
c.last(p);
}
public void b1()
{
myframe mf=new myframe();
mf.setSize(300,200);
mf.setBackground(Color.cyan);
mf.setVisible(true);
mf.setTitle("BorderLayout");
Button e,w,n,s,c;
mf.setLayout(new BorderLayout());
e=new Button("East");
w=new Button("West");
n=new Button("North");
s=new Button("South");
c=new Button("Center");
mf.add("East",e);
mf.add("West",w);
mf.add("North",n);
mf.add("South",s);
mf.add("Center",c);
AIM:
PROGRAM:
import java.awt.*;
import java.awt.event.*;
public class MenuTest extends Frame implements ActionListener, ItemListener
{
MenuBar mb;
Menu f,s,c;
MenuItem n,o,b,g;
CheckboxMenuItem r;
MenuTest()
{
mb=new MenuBar();
f=new Menu("File");
s=new Menu("Settings");
c=new Menu("Color");
r=new CheckboxMenuItem("Red");
b=new MenuItem("Blue");
g=new MenuItem("Green");
f.add(n);
f.addSeparator();
f.add(o);
c.add(r);
c.add(b);
c.add(g);
s.add(c);
mb.add(f);
mb.add(s);
r.addItemListener(this);
b.addActionListener(this);
setMenuBar(mb);
setSize(400,400);
setVisible(true);
}
public static void main(String s[])
{
new MenuTest();
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource().equals(g))
setBackground(Color.green);
else
setBackground(Color.blue);
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource().equals(r))
{
if(r.getState())
setBackground(Color.red);
else
setBackground(Color.pink);
}
}
}
RESULT: