|
import java.applet.Applet; import java.awt.*; //import Peoplec2;
public class C1 extends Applet { private Studentc2 s[]; private Teacherc2 t[];
Label nameLab, ageLab, gpaLab; TextField name,age,gpa; Button enterStudentc2,enterTeacherc2,print;
int tcount = 0; int scount=0;
public void init() { s = new Studentc2[80]; t = new Teacherc2[20]; nameLab = new Label("Enter Name"); add(nameLab); name = new TextField(10); add(name); ageLab = new Label("Enter Age"); add(ageLab); age = new TextField(10); add(age); gpaLab = new Label("Enter GPA"); add(gpaLab); gpa = new TextField(10); add(gpa);
enterStudentc2 = new Button("Studentc2"); add(enterStudentc2); enterTeacherc2 = new Button("Teacherc2"); add(enterTeacherc2); print = new Button("Print"); add(print); }
public void paint(Graphics g) { for (int x=0;x<scount;x++) g.drawString(s[x].toString(),10,120+x*15);
for (int x=0;x<tcount;x++) g.drawString(t[x].toString(),10,220+x*15);
}
public boolean action(Event e,Object o) { if (e.target == enterStudentc2) { s[scount] = new Studentc2(name.getText(),Integer.parseInt(age.getText()),gpa. getText()); name.setText(""); age.setText(""); gpa.setText(""); scount++; }
if (e.target == enterTeacherc2) { t[tcount] = new Teacherc2(name.getText(),Integer.parseInt(age.getText()),Inte ger.parseInt(gpa.getText())); name.setText(""); age.setText(""); gpa.setText(""); tcount++; }
if (e.target == print) repaint();
return true;
} }
abstract class Peoplec2 { String name; int age;
public Peoplec2(String n, int a) { name = n; age = a; }
public String toString() { return ("Name-" + name + " Age-" + String.valueOf(age)); } }
class Studentc2 extends Peoplec2 { String gpa;
public Studentc2 (String n, int a, String g) { super(n,a); gpa = g; }
public String toString() { return ("Student:" + super.toString() + " GPA-" + gpa); } }
class Teacherc2 extends Peoplec2 { int salary;
public Teacherc2 (String n, int a, int s) { super(n,a); salary = s; }
public String toString() { return ("Teacher:" + super.toString() + " $" + String.valueOf(salary)); } }
Another way to do the same thing, using polymorphism
import java.applet.Applet; import java.awt.*; import Peoplec2;
public class Schoolc2 extends Applet { private Peoplec2 p[]; private Studentc2 s[]; private Teacherc2 t[]; Label nameLab, ageLab, gpaLab; TextField name,age,gpa; Button enterStudentc2,enterTeacherc2,print; int pcount = 0;
public void init() { p = new Peoplec2[100]; s = new Studentc2[80]; t = new Teacherc2[20]; nameLab = new Label("Enter Name"); add(nameLab); name = new TextField(10); add(name); ageLab = new Label("Enter Age"); add(ageLab); age = new TextField(10); add(age); gpaLab = new Label("Enter GPA"); add(gpaLab); gpa = new TextField(10); add(gpa); enterStudentc2 = new Button("Studentc2"); add(enterStudentc2); enterTeacherc2 = new Button("Teacherc2"); add(enterTeacherc2); print = new Button("Print"); add(print); }
public void paint(Graphics g) { showStatus(" in paint " + p[0].toString()); for (int x=0;x<pcount;x++) g.drawString(p[x].toString(),10,120+x*15);
}
public boolean action(Event e,Object o) { if (e.target == enterStudentc2) { s[pcount] = new Studentc2(name.getText(),Integer.parseInt(age.getText()),gpa.getText()); p[pcount] = s[pcount]; name.setText(""); age.setText(""); gpa.setText(""); pcount++; }
if (e.target == enterTeacherc2) { t[pcount] = new Teacherc2(name.getText(),Integer.parseInt(age.getText()),Integer.parseInt(gp a.getText())); p[pcount] = t[pcount]; name.setText(""); age.setText(""); gpa.setText(""); pcount++; }
if (e.target == print) repaint(); return true; } }
public abstract class Peoplec2 { String name; int age;
public Peoplec2(String n, int a) { name = n; age = a; }
public String toString() { return ("Name-" + name + " Age-" + String.valueOf(age)); } }
import Peoplec2;
class Studentc2 extends Peoplec2 { String gpa; public Studentc2 (String n, int a, String g) { super(n,a); gpa = g; }
public String toString() { return ("Student:" + super.toString() + " GPA-" + gpa); } }
import Peoplec2;
class Teacherc2 extends Peoplec2 { int salary;
public Teacherc2 (String n, int a, int s) { super(n,a); salary = s; }
public String toString() { return ("Teacher:" + super.toString() + " $" + String.valueOf(salary)); }
}
|