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;*/
}
}
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;*/
}
}