Click to See Complete Forum and Search --> : Persons Program


The Little Guy
10-09-2006, 02:16 PM
I don't understand Java too well, so could someone please help me out with this? Here is what I have to do:

Implement a superclass Person. Make two classes, Student and Employee who inherit from Person. A person has a name and a year of birth. A student also has a major (we only care about Computer Science majors), a student id (randomly generated), and a status (also a randomly generated number from 1 to 4, representing the year in school). The birth year for each student should be a randomly generated number from 1970 to 1989. The employee also has a salary.

Write the class definitions, the constructors, and the methods, toString, for all classes. You only need one person and one employee but the number of students will be determined from a command line argument (their names can be "Name0," "Name1," ... "Name9"). Display the person and employee and only the juniors in some sort of meaningful format.

Here is what I have:
public class Hw4{
public static void main(String[]args){
Person[] s = new Person[20];
for(int i=0;i<s.length;i++){
s[i] = new Person ("Name"+i, (int)(Math.random()*10000)%20+1970);
System.out.println(s[i].toString());
}
}
}
class Person{
public String name;
public int birthYear;
public Person(String n, int bY){
name = n;
birthYear = bY;
}
public String toString(){
String msg = "Student name="+name+", birthYear="+birthYear+", Major=CCIS, Year=";
return msg;
}
}

//Create Students
class Student extends Person{
public int schoolYear;
public int idNum;
String msg;
public Student(int sY, int iD){
super(sY,iD);
schoolYear = sY;
idNum = iD;
}
public String toString(){
return msg+schoolYear+", ID number="+idNum;
}
}

//Create Employees
class Employee extends Person{
public Employee(String n, int bY){
super(n,bY);
name = n;
birthYear = bY;
/*schoolYear = sY;
idNum = iD;*/
}
}

chazzy
10-09-2006, 03:20 PM
your Student class needs to have a constructor that takes 5 parameters, as I can see it, and the class itself needs to have 3 parameters (the other 2 are inherited).

you need to first create this student constructor with 5 parameters - the 2 from Person and the 3 additional for students. your super() call should include 2 parameters: the birth year and name, since those are the only 2 things inherited.

your toString() could call a super method as well, since the super first has name and birth year.

do employee and student share the three additional fields that student brings in? if so, employee might be better off extending student rather than person.

PS- I feel like university homework assignments have gotten very difficult since I completed.

The Little Guy
10-09-2006, 04:04 PM
Ok, there are no errors, I think I fixed what you suggested, the problem that I am having is displaying each student (I haven't done anything with employee yet).
it is getting this and printing it out:
public String toString(){
String msg = "Student name="+name+", birthYear="+birthYear;
return msg;
}
That is what I want, now how do I get the extra string that comes from Student and concatenate it to the end of that string?

Current Code:

public class Hw4{
public static void main(String[]args){
Person[] s = new Person[20];
for(int i=0;i<s.length;i++){
s[i] = new Person ("Name"+i, (int)(Math.random()*10000)%20+1970, (int)(Math.random()*10000)%4, (int)(Math.random()*10000)%7);
System.out.println(s[i].toString());
}
}
}
class Person{
public String name;
public int birthYear;
public Person(String n, int bY,int sY, int iD){
name = n;
birthYear = bY;
}
public String toString(){
String msg = "Student name="+name+", birthYear="+birthYear;
return msg;
}
}

//Create Students
class Student extends Person{
public int schoolYear;
public int idNum;
String msg;
public Student(String n, int bY,int sY, int iD){
super(n,bY,sY,iD);
schoolYear = sY;
idNum = iD;
}
public String toString(){
return msg+", Major=CCIS, Year="+schoolYear+", ID number="+idNum;
}
}

//Create Employees
class Employee extends Person{
public Employee(String n, int bY,int sY, int iD){
super(n,bY,sY,iD);
name = n;
birthYear = bY;
/*schoolYear = sY;
idNum = iD;*/
}
}

The Little Guy
10-11-2006, 12:24 PM
Anyone know how to take a string from my Person class, and add it to the string in my Student class, then print it out in my Hw4 class. This would be the same for my Employee class. I don't really understand this, so much help would be appreciated.

chazzy
10-11-2006, 08:23 PM
to get the parent class, you call super. To get a method in the parent's class, you call super.methodName() IE super.toString();

so String parentMsg = super.toString();
return parentMsg + "some other message.";