BuiltWithNOF
Classes

Now we have all the tools to make any program we would like, but we have not really used the power of Object Oriented Languages to this point. Now we start on this topic. In the Array Applet Program, you kept an array of names and scores as 2 separate arrays. This was needed because we cannot group different types of data together in an array. You can probably see that it would be confusing to have 5 different arrays if we wanted to keep the name, age, address, phone, and gpa for different people. Is there a way to group data together as 1 unit? YES.. we make our own class. Check out the applet below. Enter values and then press the enter button. Change the date and press enter again. We will look at the code for this applet below:

 

import java.applet.Applet;
import java.awt.*;
//import Studentc1;

public class Schoolc1 extends Applet
{
Studentc1 s;
Label nameLab, ageLab, gpaLab;
TextField name,age,gpa;
Button enterInfo;

public void init()
{
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);
enterInfo = new Button("Enter Info"); add(enterInfo);
}

public void paint(Graphics g)
{
g.drawString("Name is " + s.name,10,120);
g.drawString("Age is " + String.valueOf(s.age),10,140);
g.drawString("GPA is " + s.gpa,10,160);
}

public boolean action(Event e,Object o)
{
if (e.target == enterInfo)
{
s = new Studentc1(name.getText(),Integer.parseInt(age.getText()),gpa.getText());
repaint();
}
return true;
}
 

class Studentc1
{
String name;
int age;
String gpa;

public Studentc1(String n,int a,String g)
{
name = n;
age = a;
gpa = g;
}

}

Now look at the applet below:

 

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));
}


}

 

 

 

[Home] [Syllabus] [Sessions]