0% found this document useful (0 votes)
93 views

Java Record

This Java code defines an applet calculator with the following functionality: 1) It initializes 24 buttons for numbers 0-9 and operators and a text field to display output. 2) It defines an action listener to handle button clicks and update the text field with number inputs or perform operations. 3) When an operator or equals button is clicked, it will calculate the operation on the current and previous number inputs and display the result.

Uploaded by

prabhanatarajan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Java Record

This Java code defines an applet calculator with the following functionality: 1) It initializes 24 buttons for numbers 0-9 and operators and a text field to display output. 2) It defines an action listener to handle button clicks and update the text field with number inputs or perform operations. 3) When an operator or equals button is clicked, it will calculate the operation on the current and previous number inputs and display the result.

Uploaded by

prabhanatarajan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 86

CALCULATOR

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="appletcalci" width=200 height=150>

</applet>

*/

public class appletcalci extends Applet implements ActionListener

Button b[]=new Button[24];

TextField tf;

// boolean firstop;

boolean found;

double mem,te,result;

String msg,temp;

public void init()

result=0.0;

msg=" ";

tf=new TextField(20);

setBackground(Color.white);

b[0]=new Button("0");
b[1]=new Button("1");

b[2]=new Button("2");

b[3]=new Button("3");

b[4]=new Button("4");

b[5]=new Button("5");

b[6]=new Button("6");

b[7]=new Button("7");

b[8]=new Button("8");

b[9]=new Button("9");

b[10]=new Button("ce");

b[11]=new Button("ac");

b[12]=new Button("m");

b[13]=new Button("mr");

b[14]=new Button("mc");

b[15]=new Button("mp");

b[16]=new Button("sqrt");

b[17]=new Button("bs");

b[18]=new Button("+");

b[19]=new Button("-");

b[20]=new Button("/");

b[21]=new Button("*");

b[22]=new Button("=");

for(int i=0;i<23;i++)

add(b[i]);
}

// firstop=true;

//clearText=true;

add(tf);

for(int j=0;j<23;j++)

b[j].addActionListener(this);

/*b[1].addActionListener(this);

b[2].addActionListener(this);

b[3].addActionListener(this);

b[4].addActionListener(this);

b[5].addActionListener(this);

b[6].addActionListener(this);

b[7].addActionListener(this);

b[8].addActionListener(this);

b[9].addActionListener(this);

b[10].addActionListener(this);

b[11].addActionListener(this);

b[12].addActionListener(this);

b[13].addActionListener(this);

b[14].addActionListener(this);

b[15].addActionListener(this);
b[16].addActionListener(this);

b[17].addActionListener(this);

b[18].addActionListener(this);

b[19].addActionListener(this);

b[20].addActionListener(this);

b[21].addActionListener(this);

b[22].addActionListener(this);*/

tf.addActionListener(this);

public void actionPerformed(ActionEvent ae)

String str=ae.getActionCommand();

found=false;

for(int k=0;k<=22&&!found;k++)

if(ae.getSource()==b[k])

found=true;

switch(k)

case 0:

msg=msg+0;

tf.setText(msg);

break;
}

case 1:

msg=msg+1;

tf.setText(msg);

break;

case 2:

msg=msg+2;

tf.setText(msg);

break;

case 3:

msg=msg+3;

tf.setText(msg);

break;

case 4:

msg=msg+4;

tf.setText(msg);

break;

}
case 5:

msg=msg+5;

tf.setText(msg);

break;

case 6:

msg=msg+6;

tf.setText(msg);

break;

case 7:

msg=msg+7;

tf.setText(msg);

break;

case 8:

msg=msg+8;

tf.setText(msg);

break;

case 9:
{

msg=msg+9;

tf.setText(msg);

break;

case 10:

msg=" ";

tf.setText(msg);

break;

case 11:

msg=" ";

mem=0.0;result=0.0;temp=" ";te=0;

tf.setText("0");

break;

case 12:

mem=Double.parseDouble(msg);

te=mem;

break;
}

case 13:

msg=String.valueOf(te);

tf.setText(msg);

msg=" ";

break;

case 14:

mem=0.0;

temp=" ";

te=0.0;

break;

case 15:

double t=te+Double.parseDouble(msg);

result=t;

msg=String.valueOf(result);

tf.setText(" "+result);

break;

case 16:
{

double s=Double.parseDouble(msg);

s=Math.sqrt(s);

msg=String.valueOf(s);

tf.setText(msg);

break;

case 17:

if(msg.length()>0)

msg=msg.substring(0,msg.length()-1);

tf.setText(msg);

break;

case 18:

temp=str;

result=Double.parseDouble(msg);

msg=" ";

tf.setText(msg);

break;

case 19:

temp=str;
result=Double.parseDouble(msg);

msg=" ";

tf.setText(msg);

break;

case 20:

temp=str;

result=Double.parseDouble(msg);

msg=" ";

tf.setText(msg);

break;

case 21:

temp=str;

result=Double.parseDouble(msg);

msg=" ";

tf.setText(msg);

break;

case 22:

if(str.equals("="))

{
if(temp.equals("+"))

result=(result+Double.parseDouble(msg));

msg=String.valueOf(result);

tf.setText(msg);

else if(temp.equals("-"))

result=result-Double.parseDouble(msg);

msg=String.valueOf(result);

tf.setText(msg);

else if(temp.equals("*"))

result=result*Double.parseDouble(msg);

msg=String.valueOf(result);

tf.setText(msg);

else if(temp.equals("/"))

result=(result/Double.parseDouble(msg));

msg=String.valueOf(result);
tf.setText(msg);

break;

BorderInsetEx

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="BorderInsetsEx" width=250 height=200>

</applet>

*/

public class BorderInsetsEx extends Applet implements ActionListener

String msg="";
Button c1,c2,c3,c4;

TextArea c5;

public void init()

BorderLayout f = new BorderLayout();

setLayout(f);

c1 = new Button("Chummaaaaa");

c2 = new Button("Vetttteeeeeeee");

c3 = new Button("Borrrrreeeeeee");

c4 = new Button("Mudiyala..");

c5 = new TextArea("Wasteeeeeee Layoutttttttttt..");

add(c1,BorderLayout.NORTH);

add(c2,BorderLayout.EAST);

add(c3,BorderLayout.SOUTH);

add(c4,BorderLayout.WEST);

add(c5,BorderLayout.CENTER);

c1.addActionListener(this);

c2.addActionListener(this);
c3.addActionListener(this);

c4.addActionListener(this);

//c5.addActionListener(this);

public void actionPerformed(ActionEvent ie)

repaint();

public Insets getInsets()

return new Insets(10,10,10,10);

public void paint(Graphics g)

BorderLayoutEx

import java.awt.*;

import java.awt.event.*;

import java.applet.*;
/*

<applet code="BorderLayoutEx" width=250 height=200>

</applet>

*/

public class BorderLayoutEx extends Applet implements ItemListener

String msg="";

Checkbox c1,c2,c3,c4,c5;

public void init()

BorderLayout f = new BorderLayout(50,50);

setLayout(f);

c1 = new Checkbox("Chummaaaaa",null,true);

c2 = new Checkbox("Vetttteeeeeeee");

c3 = new Checkbox("Borrrrreeeeeee");

c4 = new Checkbox("Mudiyala..");

c5 = new Checkbox("Wasteeeeeee Layoutttttttttt..");


add(c1,BorderLayout.NORTH);

add(c2,BorderLayout.EAST);

add(c3,BorderLayout.SOUTH);

add(c4,BorderLayout.WEST);

add(c5,BorderLayout.CENTER);

c1.addItemListener(this);

c2.addItemListener(this);

c3.addItemListener(this);

c4.addItemListener(this);

c5.addItemListener(this);

public void itemStateChanged(ItemEvent ie)

repaint();

public void paint(Graphics g)

msg = c1.getLabel()+" : " +c1.getState();

g.drawString(msg,6,80);

msg = c2.getLabel()+" : " +c2.getState();

g.drawString(msg,6,100);

msg = c3.getLabel()+" : " +c3.getState();


g.drawString(msg,6,120);

msg = c4.getLabel()+" : " +c4.getState();

g.drawString(msg,6,140);

public Insets getInsets()

return new Insets(10,10,10,10);

Editor

import java.awt.*;

import java.awt.event.*;

import java.io.*;

class findnreplace extends Frame implements ActionListener

TextField t1, t2;

Button btn;

Panel p;

editor e;

findnreplace(String title, editor e)

super(title);
this.e = e;

p = new Panel();

t1 = new TextField(10);

t2 = new TextField(10);

btn = new Button("Replace");

p.setLayout(new GridLayout(3, 2, 6, 6));

p.add(new Label("Find"));

p.add(t1);

p.add(new Label("Replace"));

p.add(t2);

p.add(btn);

add(p);

btn.addActionListener(this);

addWindowListener(new MyWindowAdapter(this));

public void actionPerformed(ActionEvent ae)

if(ae.getSource() == btn)

{
String msg = e.ta.getText();

if(msg.indexOf(t1.getText()) != -1)

msg = msg.replaceAll(t1.getText(), t2.getText());

e.ta.setText(msg);

this.setVisible(false);

class find extends Frame implements ActionListener

TextField t1;

Button btn;

Panel p;

editor e;

find(String title, editor e)

super(title);

this.e = e;

p = new Panel();

t1 = new TextField(10);

btn = new Button("Find");


p.add(t1);

p.add(btn);

add(p);

btn.addActionListener(this);

addWindowListener(new MyWindowAdapter(this));

public void actionPerformed(ActionEvent ae)

if(ae.getSource() == btn)

String msg = e.ta.getText();

int strt;

if((strt = msg.indexOf(t1.getText())) != -1)

e.ta.select(strt, strt+t1.getText().length());

this.setVisible(false);

public class editor extends Frame implements ActionListener


{

String msg = "", clpbrd;

TextArea ta;

editor(String title)

super(title);

MenuBar mb = new MenuBar();

ta = new TextArea();

setMenuBar(mb);

Menu file = new Menu("File");

Menu edit = new Menu("Edit");

MenuItem mi[] = new MenuItem[9];

file.add(mi[0] = new MenuItem("New", new MenuShortcut(KeyEvent.VK_N, false)));

file.add(mi[1] = new MenuItem("Open", new MenuShortcut(KeyEvent.VK_O, false)));

file.add(mi[2] = new MenuItem("Save", new MenuShortcut(KeyEvent.VK_S, false)));

file.add(mi[3] = new MenuItem("Exit", new MenuShortcut(KeyEvent.VK_E, false)));

edit.add(mi[4] = new MenuItem("Cut", new MenuShortcut(KeyEvent.VK_X, false)));

edit.add(mi[5] = new MenuItem("Copy", new MenuShortcut(KeyEvent.VK_C, false)));


edit.add(mi[6] = new MenuItem("Paste", new MenuShortcut(KeyEvent.VK_V, false)));

edit.add(mi[7] = new MenuItem("Replace", new MenuShortcut(KeyEvent.VK_H, false)));

edit.add(mi[8] = new MenuItem("Find", new MenuShortcut(KeyEvent.VK_F, false)));

mb.add(file);

mb.add(edit);

add(ta);

for(int i = 0; i < 9; i++)

mi[i].addActionListener(this);

addWindowListener(new MyWindowAdapter(this));

public void actionPerformed(ActionEvent ae)

String s = ae.getActionCommand();

if(s.equals("New"))

ta.setText("");

else if(s.equals("Open"))

{
FileDialog fd = new FileDialog(this, "Open...", FileDialog.LOAD);

fd.setVisible(true);

if(fd.getFile() != null)

/*

try

FileInputStream fi = new FileInputStream(fd.getFile());

byte b;

while((b = (byte)fi.read()) != -1)

String h = String.valueOf((char)b);

ta.append(h);

catch(Exception e) { }*/

try

FileReader fr = new FileReader(fd.getDirectory()+fd.getFile());

BufferedReader br = new BufferedReader(fr);

String temp;

ta.setText("");

while((temp = br.readLine()) != null)

ta.append(temp+"\n");

fr.close();
}

catch(Exception e) { }

else if(s.equals("Save"))

FileDialog fd = new FileDialog(this, "Save...", FileDialog.SAVE);

fd.setVisible(true);

if(fd.getFile() != null)

/*

try

FileOutputStream fo = new FileOutputStream(fd.getFile());

fo.write(ta.getText().getBytes());

fo.close();

catch(Exception e) { }

*/

try

String source = ta.getText();

char buffer[] = new char [source.length()];

source.getChars(0, source.length(), buffer, 0);

FileWriter fw = new FileWriter(fd.getDirectory()+fd.getFile());


fw.write(buffer);

fw.close();

catch(Exception e) { }

else if(s.equals("Exit"))

dispose();

System.exit(0);

else if(s.equals("Cut"))

clpbrd = ta.getSelectedText();

int strt = ta.getSelectionStart();

StringBuffer sb = new StringBuffer(ta.getText());

sb.delete(strt, ta.getSelectionEnd());

ta.setText(sb.substring(0));

ta.setCaretPosition(strt);

else if(s.equals("Copy"))

clpbrd = ta.getSelectedText();

else if(s.equals("Paste"))
{

int strt = ta.getSelectionStart();

ta.replaceRange("", strt, ta.getSelectionEnd());

ta.insert(clpbrd, strt);

else if(s.equals("Replace"))

findnreplace fnr = new findnreplace("Find and Replace", this);

fnr.setSize(200, 100);

fnr.setVisible(true);

else if(s.equals("Find"))

find f = new find("Find", this);

f.setSize(200, 100);

f.setVisible(true);

public static void main(String var[])

editor mf = new editor("menu demo");

mf.setSize(600, 500);

mf.setVisible(true);

}
}

class MyWindowAdapter extends WindowAdapter

editor e;

findnreplace fnr;

find f;

public MyWindowAdapter(editor e) { this.e = e; }

public MyWindowAdapter(findnreplace fnr) { this.fnr = fnr; }

public MyWindowAdapter(find f) { this.f = f; }

public void windowClosing(WindowEvent we)

if(we.getSource() == e)

e.setVisible(false);

System.exit(0);

} else if(we.getSource() == fnr)

fnr.setVisible(false);

else if(we.getSource() == f)

f.setVisible(false);

Ex1

import java.io.*;
import java.util.*;

class Mobile

private String name;

private String model;

private double cost;

private String color;

private String memory;

Mobile()

name = null;

model = null;

cost = 0;

color = null;

memory = null;

Mobile(String name, String model)

this(15000,"BLUE","2GB");

this.name = name;

this.model = model;
}

Mobile(double cost, String color, String memory)

this.cost = cost;

this.color = color;

this.memory = memory;

Mobile(String name, String model, double cost, String color, String memory)

this.name = name;

this.model = model;

this.cost = cost;

this.color = color;

this.memory = memory;

void display()

System.out.println("\nMobile Details: ");

System.out.println("Name: " + name);

System.out.println("Model: " + model);

System.out.println("Cost: Rs." + cost);


System.out.println("Color: " + color);

System.out.println("Memory: " + memory);

class Ex1

public static void main(String[] arg)

System.out.println("\nDefault Constructor: ");

Mobile a = new Mobile();

a.display();

System.out.println("\nConstructor with TWO Parameters: ");

Mobile b = new Mobile("SonyEricson","K810i");

b.display();

System.out.println("\nConstructor with FIVE Parameters: ");

Mobile c = new Mobile("Nokia","1100",2500,"Brown","64KB");

c.display();

FlowLayoutEx

import java.awt.*;
import java.awt.event.*;

import java.applet.*;

/*

<applet code="FlowLayoutEx" width=250 height=200>

</applet>

*/

public class FlowLayoutEx extends Applet implements ItemListener

String msg="";

Checkbox c1,c2,c3,c4;

public void init()

FlowLayout f = new FlowLayout(FlowLayout.RIGHT,50,10);

setLayout(f);

c1 = new Checkbox("Chummaaaaa",null,true);

c2 = new Checkbox("Vetttteeeeeeee");

c3 = new Checkbox("Borrrrreeeeeee");

c4 = new Checkbox("Mudiyala..");
add(c1);

add(c2);

add(c3);

add(c4);

c1.addItemListener(this);

c2.addItemListener(this);

c3.addItemListener(this);

c4.addItemListener(this);

public void itemStateChanged(ItemEvent ie)

repaint();

public void paint(Graphics g)

msg = c1.getLabel()+" : " +c1.getState();

g.drawString(msg,6,80);

msg = c2.getLabel()+" : " +c2.getState();

g.drawString(msg,6,100);

msg = c3.getLabel()+" : " +c3.getState();

g.drawString(msg,6,120);
msg = c4.getLabel()+" : " +c4.getState();

g.drawString(msg,6,140);

Font

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

/* <applet code="Font" width=300 height=180>

</applet>

*/

public class Font extends Applet implements ItemListener

Choice typesoffont;

String msg,msg1,msg2;

int msg3,msg4,msg5,msg6;

int ascent,descent;

public void init()

typesoffont=new Choice();

String FontList[];

GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();

FontList=ge.getAvailableFontFamilyNames();

for(int i=0;i<FontList.length;i++)

{
msg=FontList[i];

typesoffont.add(msg);

add(typesoffont);

typesoffont.addItemListener(this);

public void itemStateChanged(ItemEvent ie)

String s=typesoffont.getSelectedItem();

Graphics g=this.getGraphics();

Font f=new Font(s,Font.PLAIN,12);

this.setFont(f);

msg1=f.getName();

msg2=f.getFamily();

msg3=f.getSize();

FontMetrics fm=g.getFontMetrics();

msg4=fm.getAscent();

msg5=fm.getDescent();

msg6=fm.getHeight();

repaint();

public void paint(Graphics g)

g.drawString("The Name is :"+msg1,100,140);

g.drawString("The Frame name is :"+msg2,100,150);


g.drawString("The size is :"+Integer.toString(msg3),100,160);

g.drawString("The ascent value is :"+msg4,100,190);

g.drawString("The descent value is :"+msg5,100,200);

g.drawString("The height is :"+msg6,100,210);

STUDENTFORM

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

/*<applet code="StudentForm" height=700 width=700>

</applet>*/

public class StudentForm extends Applet implements ItemListener,ActionListener

Label name;

Label reg;

Label sex;

Label deg;

Label br;

Label yrst;

Label dob;

Label address;

Label email;

Label hec;
TextField name1;

TextField reg1;

TextField email1;

CheckboxGroup sex1;

Checkbox male;

Checkbox female;

Choice yrst1;

Choice date;

Choice month,bt,d;

Choice year;

TextArea address1;

TextArea hec1;

TextArea details;

Button submit;

public void init()

name=new Label("Name:");

reg=new Label("Register no.:");

sex=new Label("Sex:");

deg=new Label("Degree:");

br=new Label("Branch:");

yrst=new Label("Year of Study:");

dob=new Label("Date of Birth:(dd/mm/yy)");

address=new Label("Address:");

email=new Label("email id:");


hec=new Label("Hobbies&ExtraCurricular:");

name1=new TextField(20);

reg1=new TextField(20);

d=new Choice();

d.add("B.Tech");

d.add("M.Tech");

bt=new Choice();

bt.add("ICT");

bt.add("CSE");

bt.add("IT");

bt.add("IBT");

bt.add("MECH");

bt.add("CIVIL");

email1=new TextField(20);

sex1=new CheckboxGroup();

male=new Checkbox("Male",sex1,true);

female=new Checkbox("Female",sex1,false);

yrst1=new Choice();

date=new Choice();

month=new Choice();

year=new Choice();

yrst1.add("2005");

yrst1.add("2006");

yrst1.add("2007");

yrst1.add("2008");
yrst1.add("2009");

yrst1.add("2010");

yrst1.add("2011");

yrst1.add("2012");

yrst1.add("2013");

date.add("1");

date.add("2");

date.add("3");

date.add("4");

date.add("5");

date.add("6");

date.add("7");

date.add("8");

date.add("9");

date.add("10");

date.add("11");

date.add("12");

date.add("13");

date.add("14");

date.add("15");

date.add("16");

date.add("17");

date.add("18");

date.add("19");

date.add("20");
date.add("21");

date.add("22");

date.add("23");

date.add("24");

date.add("25");

date.add("26");

date.add("27");

date.add("28");

date.add("29");

date.add("30");

date.add("31");

month.add("1");

month.add("2");

month.add("3");

month.add("4");

month.add("5");

month.add("6");

month.add("7");

month.add("8");

month.add("9");

month.add("10");

month.add("11");

month.add("12");

year.add("1988");

year.add("1989");
year.add("1990");

year.add("1991");

year.add("1992");

year.add("1993");

year.add("1994");

year.add("1995");

year.add("1996");

address1=new TextArea();

hec1=new TextArea();

details=new TextArea();

submit=new Button("Submit");

add(name);add(name1);

add(reg);add(reg1);

add(sex);add(male);add(female);

add(deg);add(d);

add(br);add(bt);

add(yrst);add(yrst1);

add(dob);add(date);add(month);add(year);

add(address);add(address1);

add(email);add(email1);

add(hec);add(hec1);

add(submit);

add(details);

name1.addActionListener(this);

reg1.addActionListener(this);
email1.addActionListener(this);

submit.addActionListener(this);

yrst1.addItemListener(this);

date.addItemListener(this);

month.addItemListener(this);

year.addItemListener(this);

male.addItemListener(this);

female.addItemListener(this);

d.addItemListener(this);

bt.addItemListener(this);

public void itemStateChanged(ItemEvent e)

{repaint();}

public void actionPerformed(ActionEvent ae)

{repaint();}

public void paint(Graphics g)

{String msg1,msg2,msg3,msg4,msg5;

msg1="Name of the Student is:"+name1.getText()+"\n"+" Sex:"+sex1.getSelectedCheckbox().getLabel()


+"\n";

msg2=" Reg.no:"+reg1.getText()+" Degree:"+d.getSelectedItem()+" Branch:"+bt.getSelectedItem()


+"Year of Study:"+yrst1.getSelectedItem()+"\n";

msg3=" Date of Birth:"+date.getSelectedItem()+"/"+month.getSelectedItem()


+"/"+year.getSelectedItem()+"\n";

msg4=" Address:"+address1.getText()+"Email id:"+email1.getText()+"\n";

msg5=" Hobby&Extra curricular activities:"+hec1.getText()+"\n";

details.setText(msg1+msg2+msg3+msg4+msg5);

}
}

Gpaint

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

/* <applet code="gpaint" width=300 height=400>

</applet> */

class brush extends Frame implements ActionListener,ItemListener

String mg=" ";

CheckboxMenuItem red,blue;

brush(String title)

super(title);

MenuBar mbar=new MenuBar();

setMenuBar(mbar);

Menu draw=new Menu("DRAW");

MenuItem d1,d2,d3,d4,d5,d6;

draw.add(d1=new MenuItem("LINE"));

draw.add(d2=new MenuItem("CIRCLE"));

draw.add(d3=new MenuItem("SQUARE"));

draw.add(d4=new MenuItem("-"));

Menu sub=new Menu("Fill",true);

MenuItem s1,s2,s3;

sub.add(s1=new MenuItem("Circle"));
sub.add(s2=new MenuItem("Square"));

draw.add(sub);

draw.add(d5=new MenuItem("-"));

draw.add(d6=new MenuItem("Close"));

mbar.add(draw);

Menu col= new Menu("Color");

col.add(red= new CheckboxMenuItem("red"));

col.add(blue= new CheckboxMenuItem("blue"));

mbar.add(col);

d1.addActionListener(this);

d2.addActionListener(this);

d3.addActionListener(this);

d4.addActionListener(this);

d5.addActionListener(this);

d6.addActionListener(this);

s1.addActionListener(this);

s2.addActionListener(this);

red.addItemListener(this);

blue.addItemListener(this);

blue.setState(true);

public void actionPerformed(ActionEvent ae)

String arg=ae.getActionCommand();

if(arg.equals("LINE"))
mg="Line";

else if(arg.equals("CIRCLE"))

mg="Circle";

else if(arg.equals("SQUARE"))

mg="Square";

else if(arg.equals("Circle"))

mg="Fillc";

else if(arg.equals("Square"))

mg="Fills";

else

mg="Close";

repaint();

public void itemStateChanged(ItemEvent ie) {}

public void paint(Graphics g)

if(red.getState())

g.setColor(Color.red);

blue.setState(false);

else if(blue.getState())

g.setColor(Color.blue);

red.setState(false);
}

else

red.setState(false);

blue.setState(false);

g.setColor(Color.black);

if(mg=="Line")

g.drawLine(150,150,200,200);

else if(mg=="Circle")

g.drawOval(0,0,200,200);

else if(mg=="Square")

g.drawRect(100,100,200,200);

if(mg=="Fillc")

g.fillOval(150,150,200,200);

else if(mg=="Fills")

g.fillRect(100,100,200,200);

if(mg=="Close")

setVisible(false);

class Myadapter extends WindowAdapter

brush br;

public Myadapter(brush bru)


{

this.br=bru;

public void WindowClosing(WindowEvent we)

br.setVisible(false);

public class gpaint extends Applet

Frame f;

public void init()

f=new brush("Paint and Brush");

f.setSize(300,300);

f.setVisible(true);

public void start()

f.setVisible(true);

public void stop()

f.setVisible(false);

}
}

GridLayoutEx

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="GridLayoutEx" width=550 height=600>

</applet>

*/

public class GridLayoutEx extends Applet implements ActionListener

String msg="";

Button c1,c2,c3,c4;

Button[] b;

TextArea c5;

public void init()

GridLayout f = new GridLayout(3,6);

setLayout(f);
b = new Button[10];

c1 = new Button("Chummaaaaa");

c2 = new Button("Vetttteeeeeeee");

c3 = new Button("Borrrrreeeeeee");

c4 = new Button("Mudiyala..");

add(c1);

add(c2);

add(c3);

add(c4);

for(int i=0;i<10;i++)

b[i] = new Button(""+i);

add(b[i]);

c1.addActionListener(this);

c2.addActionListener(this);

c3.addActionListener(this);

c4.addActionListener(this);
}

public void actionPerformed(ActionEvent ie)

repaint();

public Insets getInsets()

return new Insets(10,10,10,10);

public void paint(Graphics g)

DEPARTMENT

import java.io.*;

import java.util.*;

class Dept

String deptname;
void dept(String s)

deptname=s;

void show()

System.out.println("dept name is "+deptname);

class Student extends Dept

String name,address;

int regno,yr;

void getstudentdetails(String a,String b,int c,int d)throws IOException

name=a;

address=b;

regno=c;

yr=d;

void sdisplay()

System.out.println("student name is"+name);

System.out.println("Address is"+address);

System.out.println("reg no"+regno);
System.out.println("year of joining"+yr);

class Score extends Student

int m1,m2,m3;

void getmarkdetails(int a,int b,int c)throws IOException

m1=a;

m2=b;

m3=c;

class Result extends Score

int tot,avg;

String grade;

void computegrade()throws IOException

tot=m1+m2+m3;

avg=(tot)/3;

if(avg>=85)

grade="S";

}
else if(avg>85&&avg<=75)

grade="A";

else if(avg>75&&avg<=65)

grade="B";

else if(avg>65&&avg<=55)

grade="D";

else

grade="F";

void display()

System.out.println("m1=\t"+m1);

System.out.println("m2=\t"+m2);

System.out.println("m3=\t"+m3);

System.out.println("total=\t"+tot);

System.out.println("grade=\t"+grade);

}
}

class Staff extends Dept

String staff_name;

int id,dateofjoinin;

void getdata(String x,int a,int b,String y)throws IOException

staff_name=x;

id=a;

dateofjoinin=b;

class Teaching extends Staff

String qual,skills,area_of_intrest;

void getpersonaldetails(String a,String b,String c,String d)throws IOException

qual=a;

skills=b;

area_of_intrest=c;

void display()throws IOException

System.out.println("staff name"+staff_name);
System.out.println("Id "+id);

System.out.println("date of joining:"+dateofjoinin);

System.out.println("qual "+qual);

System.out.println("skills "+skills);

System.out.println("area of interest "+area_of_intrest);

class Nonteaching extends Staff

String certification,expertise;

int yrs_of_exp;

void inputdata(String m,String n,int p)throws IOException

certification=m;

expertise=n;

yrs_of_exp=p;

void showdata()throws IOException

System.out.println("date of joining"+dateofjoinin);

System.out.println("certification"+certification);

System.out.println("years of experience"+yrs_of_exp);

System.out.println("Expertise "+expertise);

}
class College

public static void main(String args[])throws IOException

int ch,a;

int x,y,m1,m2,m3,e,f,g;

String s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16;

String st,st1;

DataInputStream din=new DataInputStream(System.in);

System.out.println("enter choice:\n1.Student \n2.Staff");

st=din.readLine();

ch=Integer.parseInt(st);

System.out.println("Enter dept name:");

st1=din.readLine();

if(ch==1)

System.out.println("student");

Result r=new Result();

System.out.println("Enter dept name:");

st1=din.readLine();

System.out.println("Enter Name:");

s1=din.readLine();

System.out.println("Enter Address");

s2=din.readLine();
System.out.println("Enter Reg.no");

s3=din.readLine();

x=Integer.parseInt(s3);

System.out.println("Enter Year");

s4=din.readLine();

y=Integer.parseInt(s4);

r.getstudentdetails(s1,s2,x,y);

r.sdisplay();

System.out.println("Enter mark 1");

s5=din.readLine();

m1=Integer.parseInt(s5);

System.out.println("Enter mark 2");

s6=din.readLine();

m2=Integer.parseInt(s6);

System.out.println("Enter mark 3");

s7=din.readLine();

m3=Integer.parseInt(s7);

r.getmarkdetails(m1,m2,m3);

r.computegrade();

r.display();

else if(ch==2)

System.out.println("Staff");

DataInputStream d= new DataInputStream(System.in);


System.out.println("Enter choice: 1.Teaching staff\n 2.Non-teaching staff\n");

st=d.readLine();

a=Integer.parseInt(st);

if(a==1)

System.out.println("teaching staff");

Teaching t=new Teaching();

System.out.println("Enter Staff name:");

s11=d.readLine();

System.out.println("Enter id:");

s12=d.readLine();

e=Integer.parseInt(s12);

System.out.println("Enter Date of joining:");

s13=d.readLine();

f=Integer.parseInt(s13);

t.getdata(s11,e,f,st1);

System.out.println("Enter Qualification");

s8=d.readLine();

System.out.println("Enter Skills");

s9=d.readLine();

System.out.println("Enter Area of Intrest");

s10=d.readLine();

t.getpersonaldetails(s8,s9,s10,s11);

t.display();

}
if(a==2)

System.out.println("non-teaching staff");

Nonteaching n=new Nonteaching();

System.out.println("Enter Staff name:");

s11=d.readLine();

System.out.println("Enter id:");

s12=d.readLine();

e=Integer.parseInt(s12);

System.out.println("Enter Date of joining:");

s13=d.readLine();

f=Integer.parseInt(s13);

n.getdata(s11,e,f,st1);

System.out.println("Enter certification:");

s14=d.readLine();

System.out.println("Enter Expertise:");

s15=d.readLine();

System.out.println("Enter yrs of experiance:");

s16=d.readLine();

g=Integer.parseInt(s16);

n.inputdata(s14,s15,g);

n.showdata();

}
}

vehicle

import java.io.*;

class Vehicle

String name,service_station_name;

int model_no,price;

void get(String a,String b,int c,int d)

name=a;

service_station_name=b;

model_no=c;

price=d;

void display()

System.out.print("Name is:"+name);

System.out.print("\nService Station name is:"+service_station_name);

System.out.print("\n Model no is:"+model_no);

System.out.print("\n price is:"+price);

class Bike extends Vehicle

{
int x,discount=5;

void calculate()

x=(price*discount)/100;

void disp()

System.out.print("\ndiscount is:\t"+discount);

System.out.print("\nReduced price is:\t"+x);

public static void main(String args[])throws IOException

DataInputStream din=new DataInputStream(System.in);

Bike ob=new Bike();

String s1,s2,s3,s4;

int a,b;

System.out.print("\nEnter name:");

s1=din.readLine();

System.out.print("\nEnter Service station name:");

s2=din.readLine();

System.out.print("\nEnter model_no:");

s3=din.readLine();

a=Integer.parseInt(s3);

System.out.print("\nEnter price:");

s4=din.readLine();
b=Integer.parseInt(s4);

ob.get(s1,s2,a,b);

ob.display();

ob.calculate();

ob.disp();

INTERFACEMAINLAB

import java.io.*;

interface plumber

void unplugdrain();

interface electrician

void changebulb();

class Human

String name;

void HumanName(String s)

name=s;

System.out.println("The Human name is:" +name);


}

class Handyperson extends Human implements plumber,electrician

void name()

System.out.println("This human is a handyperson");

public void changebulb()

System.out.println("this human can change bulb");

public void unplugdrain()

System.out.println("This human can unplug drain");

class Nobodyspecial extends Human

void name()

System.out.println(" This Human is Nodody spl");

public void unplugdrain()


{

System.out.println("This human cannot unplug drain");

public void changebulb()

System.out.println("This human cannot change bulb");

class Industrialplumber extends Human implements plumber

void name()

System.out.println("This human is an Industrialplumber");

public void unplugdrain()

System.out.println("This human can unplug drain");

class Methods

public static void main(String args[])throws IOException

int ch;

String st,st1;
DataInputStream din=new DataInputStream(System.in);

System.out.println("Enter the choice:\n 1.Handy Person \n 2.Nobody Special \n 3.Industrial plumber");

st1=din.readLine();

ch=Integer.parseInt(st1);

System.out.println("Enter the Human Name:");

st=din.readLine();

if(ch==1)

Handyperson un=new Handyperson();

un.HumanName(st);

un.name();

un.unplugdrain();

un.changebulb();

else if(ch==2)

Nobodyspecial no=new Nobodyspecial();

no.HumanName(st);

no.name();

no.unplugdrain();

no.changebulb();

else

Industrialplumber in=new Industrialplumber();


in.HumanName(st);

in.name();

in.unplugdrain();

interfaceprelab

import java.io.*;

interface inter1

public void meth1();

interface inter2

public void meth2();

class test implements inter1,inter2

int a=20,b=30,c=40,d;

public void meth1()

System.out.println("marks in a:"+a);

System.out.println("marks in b:"+b);

System.out.println("marks in c:"+c);

}
public void meth2()

d=a+b+c;

System.out.println("total:"+d);

void grade()

System.out.println("grade is:A");

class inter

public static void main(String args[])

test t= new test();

t.meth1();

t.meth2();

t.grade();

KEYEVE

import java.applet.*;

import java.awt.*;

import java.awt.event.*;
/*<applet code="keyeve" height=600 width=500>

</applet>*/

public class keyeve extends Applet implements KeyListener

String msg=" ";

int mx=10,my=10;

public void init()

addKeyListener(this);

requestFocus();

public void keyPressed(KeyEvent ke)

setBackground(Color.red);

showStatus("Key down");

public void keyReleased(KeyEvent ke)

setBackground(Color.blue);

showStatus("Key up");
}

public void keyTyped(KeyEvent ke)

setBackground(Color.green);

msg+=ke.getKeyChar();

repaint();

public void paint(Graphics g)

g.drawString(msg,mx,my);

LOGIN

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

/*

<applet code="Login"

height=300

width=300 >

</applet>*/

public class Login extends Applet implements ActionListener

{
String v1,v2,st1;

Label l1=new Label("username");

Label l2=new Label("password");

TextField t1=new TextField(12);

TextField t2=new TextField(7);

Button b1= new Button("okay");

Button b2=new Button("cancel");

public void init()

add(l1);

add(t1);

add(l2);

add(t2);

add(b1);

add(b2);

b1.addActionListener(this);

b2.addActionListener(this);

public void actionPerformed(ActionEvent ae)

st1=ae.getActionCommand();

repaint();

public void paint(Graphics g)

{
v1=t1.getText();

v2=t2.getText();

t2.setEchoChar('*');

if(st1.equals("okay"))

if(v2.equals("sastra"))

g.drawString("successfully logined in",100,100);

else

g.drawString("failed",100,100);

if( st1.equals("cancel"))

t1.setText("");

t2.setText("");

THREADS

import java.io.*;

import java.lang.*;
class Fact implements Runnable

Thread t1;

int n=10;

public void run()

int i,fact;

try

for(i=1;i<=n;i++)

fact=1;

for(int j=1;j<=i;j++)

fact=fact*j;

System.out.println(i+" Factorial = "+fact);

Thread.sleep(1000);

catch( InterruptedException e )

System.out.println("An error occured in "+t1);

e.printStackTrace();

}
class SumofNos implements Runnable

Thread t2;

int n=10;

public void run()

int i,sum;

try

for(i=1;i<=n;i++)

sum=0;

for(int j=1;j<=i;j++)

sum+=j;

System.out.println(i+" Sum = "+sum);

Thread.sleep(1000);

catch( InterruptedException e )

System.out.println("An error occured in "+t2);

e.printStackTrace();

}
class FiveTable implements Runnable

Thread t3;

int n=10;

public void run()

int i,pdt;

try

for(i=1;i<=n;i++)

pdt=5*i;

System.out.println("5x"+i+"="+pdt);

Thread.sleep(1000);

catch( InterruptedException e )

System.out.println("An error occured in "+t3);

e.printStackTrace();

class ThreadTest

{
public static void main(String args[])

//try

//{

Fact f=new Fact();

Thread T1=new Thread(f);

T1.start();

SumofNos s=new SumofNos();

Thread T2=new Thread(s);

T1.suspend();

T2.start();

T1.resume();

FiveTable ft=new FiveTable();

Thread T3=new Thread(ft);

//T1.stop();

//T1.join();

System.out.println("Thread T1 is "+T1.isAlive());

T3.start();

//}

/*catch(InterruptedException e)

}*/

}
MYCAR

import java.awt.*;

import java.applet.*;

/*

<applet code="MyCar" width=500 height=400>

</applet>

*/

public class MyCar extends Applet implements Runnable

Thread t;

Color maroon;

int x=0,stx=20,sty=150;

boolean test=true;

public void init()

maroon =new Color(0x80,50,0);

setForeground(Color.blue);

setBackground(maroon);

public void paint(Graphics g)

g.drawRoundRect(stx+x,sty+20,200,30,10,10);

g.fillOval(stx+20+x,sty+40,20,20);

g.fillOval(stx+160+x,sty+40,20,20);
g.drawLine(stx+50+x,sty,stx+150+x,sty);

g.drawLine(stx+50+x,sty,stx+20+x,sty+20);

g.drawLine(stx+150+x,sty,stx+180+x,sty+20);

public void start()

t=new Thread(this);

t.start();

public void run()

while(test)

try

repaint();

Thread.sleep(200);

x = x+10;

if(x>400)

x=5;

catch(Exception e)

{}

}
}

PRETHREAD

class thread implements Runnable

int i;

Thread t=Thread.currentThread();

thread()

t=new Thread(this,"My first thread");

System.out.println("First thread:"+t);

t.start();

public void run()

try

for(i=0;i<5;i++)

System.out.println("First thread:"+i);

Thread.sleep(2000);

catch(InterruptedException e)
{

System.out.println("Interrupt");

System.out.println("Exit1");

class prethread

public static void main(String a[])

int j;

new thread();

try

for(j=0;j<5;j++)

System.out.println("Main thread:"+j);

Thread.sleep(2000);

catch(InterruptedException e)

System.out.println("Interrupted");
}

System.out.println("ExitM");

SHAPES

import java.lang.*;

import java.io.*;

class shape

double x;

double y;

void get() throws IOException

String st;

DataInputStream din=new DataInputStream(System.in);

System.out.println("enter the value of a");

st=din.readLine();

x=Integer.parseInt(st);

System.out.println("enter the value of y");

st=din.readLine();

y=Integer.parseInt(st);

void area(){}
}

class rectangle extends shape

void area()

double a;

a=x*y;

System.out.println("area of rectangle="+a);

class triangle extends shape

void area()

double a;

a=(x*y)/2;

System.out.println("area of triangle="+a);

class dnd

public static void main(String args[]) throws IOException

int choice;

String k;
System.out.println("enter choice\n1.rectangle\n2.triangle");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

k=br.readLine();

choice=Integer.parseInt(k);

shape s;

rectangle r=new rectangle();

triangle t=new triangle();

if(choice==1)

s=r;

s.get();

s.area();

else

s=t;

s.get();

s.area();

TEXT EDIT

import java.awt.*;

import java.awt.event.*;
import java.applet.*;

class NotePad extends Frame implements ActionListener

MenuBar mb;

Menu file,edit;

MenuItem ne,save,open,exit;

MenuItem copy,cut,paste,find,replace;

TextArea ta;

String cpText="";

NotePad()

mb = new MenuBar();

setMenuBar(mb);

file = new Menu("File");

edit = new Menu("Edit");

ne = new MenuItem("New");

save = new MenuItem("Save");

open = new MenuItem("Open");

exit = new MenuItem("Exit");

cut = new MenuItem("Cut");


copy = new MenuItem("Copy");

paste = new MenuItem("Paste");

find = new MenuItem("Find");

replace = new MenuItem("Replace");

file.add(ne);

file.add(open);

file.add(save);

file.add(exit);

edit.add(cut);

edit.add(copy);

edit.add(paste);

edit.add(find);

edit.add(replace);

mb.add(file);

mb.add(edit);

ne.addActionListener(this);

open.addActionListener(this);

save.addActionListener(this);

exit.addActionListener(this);

cut.addActionListener(this);

copy.addActionListener(this);
paste.addActionListener(this);

find.addActionListener(this);

replace.addActionListener(this);

ta = new TextArea(10,100);

add(ta);

addWindowListener(new MyWinAda(this));

setSize(300,300);

setVisible(true);

public void actionPerformed(ActionEvent ae)

Object src = ae.getSource();

if(src == exit)

System.exit(1);

else if(src == copy)

cpText = ta.getSelectedText();

else if(src == paste)

{
ta.insertText(cpText,ta.getSelectionStart());

// ta.insert(cpText,ta.getCaretPosition());

else if(src == replace)

System.out.println("\n"+ta.getSelectionStart());

System.out.println(ta.getSelectionEnd());

System.out.println(ta.getCaretPosition());

System.out.println(ta.getRows());

System.out.println(ta.getColumns());

else if(src == cut)

cpText = ta.getSelectedText();

ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());

class MyWinAda extends WindowAdapter

NotePad txt;

MyWinAda(NotePad t)

{
txt = t;

public void windowClosing(WindowEvent we)

txt.setVisible(false);

System.exit(1);

public class TextEdit

public static void main(String[] arg)

new NotePad();

You might also like