Results 1 to 9 of 9
  1. #1
    njcountrygirl is offline Member
    Join Date
    Feb 2015
    Posts
    5
    Rep Power
    0

    Default inheritance & Polymorphism

    Hey ya'll. im currently fighting a 102 fever so I am going to need all the help I can get. I luckily did part of the program ahead of time.

    this is what the problem states:

    Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office and salary. A faculty member has office hours and a rank. A staff member has a title. Override the toString method in each class to display the class name and the person's name. Implement the classes.

    I then need to write a test program that creates a Person, Student, Employee, Faculty, and Staff, and invokes their toString() methods. This is the part that I need help with. I just can't physically figure it out even though i know its suppose to be the easiest part of the program to write.

    here is my code:
    Java Code:
    import java.util.*;
    public class Exer112 {
    	public static void main(String[] args) {
    	
    		Person person = new Person("John Doe", "123 This Street", "908-234-1212", "johndoe@gmail.com");
    		Person student = new Student("Mary Jane", "44 That Street", "234-123-1234", "maryjane@gmail.com", "Senior");
    		Person employee = new Employee("Gary Rug", "654 Over There Street", "765-987-0987", "garyrug@gmail.com");
    		Person faculty = new Faculty("Jose Bee", "876 Jay Street", "345-345-3456", "josebee@gmail.com");
    		Person staff = new Staff("Sue Joe", "6 Here Street", "101-101-1234", "sj@gmail.com");
    
    		System.out.println(person.toString() + "\n");
    		System.out.println(student.toString() + "\n");
    		System.out.println(employee.toString() + "\n");
    		System.out.println(faculty.toString() + "\n");
    		System.out.println(staff.toString() + "\n");
    
    		
    	}
    }		
    
    
    class Person {
    	private String name;
    	private String address;
    	private String phone;
    	private String email;
    
    	public Person() {
    		name = "Stephan Lewis"; 
    		address = "1329 Strawberry Lane";
    		phone = "615-555-0128";
    		email = "sl@gmail.com";
    	}
    
    	public Person(String name, String address, String phone, String email){
    		this.name = name;
    		this.address = address;
    		this.phone = phone;
    		this.email= email;
    	}
    
    	public void setName(String name){
    		this.name = name;
    	}
    
    	public String getName(){
    		return name;
    	}
    
    	public void setAddress(String address){
    		this.address = address;
    	}
    
    	public String getAddress(){
    		return address;
    	}
    
    	public void setPhone(String phone){
    		this.phone = phone;
    	}
    
    	public String getPhone(){
    		return phone;
    	}
    
    	public void setEmail(String email){
    		this.email = email;
    	}
    
    	public String getEmail(){
    		return email;
    	}
    
    	public String toString() {
    		return "Person";
    	}
    }
    
    class Student extends Person {
    	private int classStatus;
    	public static in freshman = 1;
    	public static int sophomore =2;
    	public static int junior =3;
    	public static int senior =4;
    	
    	public Student(String classStatus){
    		this.classStatus = classStatus;
    	}
    	
    	public String getClassStatus(){
    		return "Student";
    	}
    }
    
    class Employee extends Person {
    	private String office;
    	private String salary;
    	private MyDate dateHired;
    
    	public Employee(){
    		office = "101";
    		salary = "85,000";
    	}
    
    	public Employee(String office, String salary, MyDate dateHired){
    		this.office = office;
    		this.salary = salary;
    		this.dateHired = dateHired;
    	}
    
    	public void setOffice(String office){
    		this.office = office;
    	}
    
    	public String getOffice(){
    		return office;
    	}
    
    	public String getSalary(){
    		return salary;
    	}
    
    	public void setSalary(String salary){
    		this.salary = salary;
    	}
    
    	public MyDate getMyDate(){
    		return dateHired;
    	}
    
    	public String toString(){
    		return “Employee";
    	}
    }
    
    class MyDate{
    	private int month;
    	private int day;
    	private int year;
    
    	public MyDate(int month, int day, int year){
    		this.day = day;
    		this.month  = month;
    		this.year = year;
    	}
    }
    
    class Faculty extends Employee { 
    	public static int lecturer =1;
    	public static int assistantProfessor =2;
    	public static int associateProfessor =3;
    	public static int professor =4;
    	
    	private String officeHours;
    	private String rank;
    
    	public Faculty(){
    		officeHours = "9am-5pm”;
    		rank = "Professor";
    	}
    
    	public Faculty(String officeHours, String rank){
    		this.officeHours = officeHours;
    		this.rank = rank;
    	}
    
    	public String getOfficeHours(){
    		return officeHours;
    	}
    
    	public void setOfficeHours(String officeHours){
    		this.officeHours = officeHours;
    	}
    
    	public String getRank(){
    		return rank;
    	}
    
    	public void setRank(String rank){
    		this.rank = rank;
    	}
    	
    	public String toString(){
    		return "Faculty";
    	}
    }
    
    class Staff extends Employee {
    	private String title;
    
    	public Staff(){
    		title = "Janitor";
    	}
    
    	public Staff(String title){
    		this.title = title;
    	}
    
    	public String getTitle(){
    		return title;
    	}
    
    	public void setTitle(String title){
    		this.title = title;
    	}
    
    	public String toString(){
    		return "Staff";
    	}
    }
    I'm sure this is all wrong. Any help would be very appreciated!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: inheritance & Polymorphism

    But you've written that test class.
    So what's the problem?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: inheritance & Polymorphism

    Quote Originally Posted by njcountrygirl View Post
    I'm sure this is all wrong. Any help would be very appreciated!
    That's not how it works, you are required to think about it yourself first. If you are so sure it is all wrong, then point out some things which are wrong. Try to resolve them yourself, one at a time.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    njcountrygirl is offline Member
    Join Date
    Feb 2015
    Posts
    5
    Rep Power
    0

    Default Re: inheritance & Polymorphism

    I am made some major changes. Here is my code now using super:

    Java Code:
    import java.util.*;
    public class Exer112 {
    	public static void main(String[] args) {
    
    		Person person = new Person("John Doe", "123 This Street", "123-123-1234", "jd@gmail.com");
    		Person student = new Student("Mary Jane", "44 That Street", "123-123-1235", "mj@gmail.com", "Senior");
    		Person employee = new Employee("Gary Rug", "500 Over There Street", "123-123-1236", "gr@gmail.com", "111", "85,000");
    		Person faculty = new Faculty("Jose Bee", "867 Hay Street", "123-123-1237", "jb@gmail.com","9am-3pm", "Professor");
    		Person staff = new Staff("Sue Joe", "6 Here Street", "123-123-1238", "sj@gmail.com", "Janitor" );
    		
    	
    	
    		System.out.println(Person.toString() + "\n");
    		System.out.println(Student.toString() + "\n");
    		System.out.println(Employee.toString() + "\n");
    		System.out.println(Faculty.toString() + "\n");
    		System.out.println(Staff.toString() + "\n");
    
    		
    	}
    }		
    
    
    class Person {
    	private String name;
    	private String address;
    	private String phone;
    	private String email;
    
    	public Person() {
    		
    	}
    
    	public Person(String name, String address, String phone, String email){
    		this.name = name;
    		this.address = address;
    		this.phone = phone;
    		this.email= email;
    	}
    
    	public void setName(String name){
    		this.name = name;
    	}
    
    	public String getName(){
    		return name;
    	}
    
    	public void setAddress(String address){
    		this.address = address;
    	}
    
    	public String getAddress(){
    		return address;
    	}
    
    	public void setPhone(String phone){
    		this.phone = phone;
    	}
    
    	public String getPhone(){
    		return phone;
    	}
    
    	public void setEmail(String email){
    		this.email = email;
    	}
    
    	public String getEmail(){
    		return email;
    	}
    
    	public String toString() {
    		return "Person";
    	}
    }
    
    class Student extends Person {
    	private int classStatus;
    	public static int freshman = 1;
    	public static int sophomore =2;
    	public static int junior =3;
    	public static int senior =4;
    
    	public Student(){
    
    	}
    	
    	public Student(int classStatus){
    		this.classStatus = classStatus;
    	}
    
    	public Student(String name, String address, String phone, String email, int classStatus){
    		super(name, address, phone, email);
    		this.classStatus = classStatus;
    	}
    	
    	public String toString(){
    		return super.toString + "Student";
    	}
    }
    
    class Employee extends Person {
    	private String office;
    	private String salary;
    	private MyDate dateHired;
    
    	public Employee(){
    	}
    		
    	public Employee(String name, String address, String phone, String email){
    		super(name, address, phone, email);
    	}
    
    	public Employee(String office, String salary){
    		this.office = office;
    		this.salary = salary;
    	}
    
    	public Employee(String name, String address, String phone, String email, String office, String salary, MyDate dateHired){
    		super(name, address, phone,email);
    		this.office = office;
    		this.salary = salary;
    		this.dateHired = dateHired;
    	}
    
    	public void setOffice(String office){
    		this.office = office;
    	}
    
    	public String getOffice(){
    		return office;
    	}
    
    	public String getSalary(){
    		return salary;
    	}
    
    	public void setSalary(String salary){
    		this.salary = salary;
    	}
    
    	public MyDate getMyDate(){
    		return dateHired;
    	}
    
    	public String toString(){
    		return super.toString + "Employee";
    	}
    }
    
    class MyDate{
    	private int month;
    	private int day;
    	private int year;
    
    	public MyDate(int month, int day, int year){
    		this.day = day;
    		this.month  = month;
    		this.year = year;
    	}
    }
    
    class Faculty extends Employee { 
    	public static int lecturer =1;
    	public static int assistantProfessor =2;
    	public static int associateProfessor =3;
    	public static int professor =4;
    	
    	private String officeHours;
    	private String rank;
    
    	public Faculty(){
    	
    	}
    
    	public Faculty(String officeHours, String rank){
    		this.officeHours = officeHours;
    		this.rank = rank;
    	}
    
    	public Faculty(String name, String address, String phone, String email, String officeHours, String rank){
    		super(name, address, phone,email);
    		this.officeHours = officeHours;
    		this.rank = rank;
    	}
    
    	public String getOfficeHours(){
    		return officeHours;
    	}
    
    	public void setOfficeHours(String officeHours){
    		this.officeHours = officeHours;
    	}
    
    	public String getRank(){
    		return rank;
    	}
    
    	public void setRank(String rank){
    		this.rank = rank;
    	}
    	
    	public String toString(){
    		return super.toString + "Faculty";
    	}
    }
    
    class Staff extends Employee {
    	private String title;
    
    	public Staff(){
    
    	}
    
    	public Staff(String title){
    		this.title = title;
    	}
    
    	public Staff(String name, String address, String phone, String email, String title){
    		super(name, address, phone, email);
    		this.title = title;
    	}
    
    	public String getTitle(){
    		return title;
    	}
    
    	public void setTitle(String title){
    		this.title = title;
    	}
    
    	public String toString(){
    		return super.toString + "Staff";
    	}
    }
    here are the errors that I am getting

    Exer112.java:42: cannot find symbol
    symbol : constructor Student(java.lang.String,java.lang.String,java.lan g.String,java.lang.String,java.lang.String)
    location: class Student
    Person student = new Student("Mary Jane", "44 That Street", "123-123-1235", "mj@gmail.com", "Senior");
    ^
    Exer112.java:43: cannot find symbol
    symbol : constructor Employee(java.lang.String,java.lang.String,java.la ng.String,java.lang.String,java.lang.String,java.l ang.String)
    location: class Employee
    Person employee = new Employee("Gary Rug", "500 Over There Street", "123-123-1236", "gr@gmail.com", "111", "85,000");
    ^
    Exer112.java:49: non-static method toString() cannot be referenced from a static context
    System.out.println(Person.toString() + "\n");
    ^
    Exer112.java:50: non-static method toString() cannot be referenced from a static context
    System.out.println(Student.toString() + "\n");
    ^
    Exer112.java:51: non-static method toString() cannot be referenced from a static context
    System.out.println(Employee.toString() + "\n");
    ^
    Exer112.java:52: non-static method toString() cannot be referenced from a static context
    System.out.println(Faculty.toString() + "\n");
    ^
    Exer112.java:53: non-static method toString() cannot be referenced from a static context
    System.out.println(Staff.toString() + "\n");
    ^
    Exer112.java:135: cannot find symbol
    symbol : variable toString
    location: class Person
    return super.toString + "Student";
    ^
    Exer112.java:184: cannot find symbol
    symbol : variable toString
    location: class Person
    return super.toString + "Employee";
    ^
    Exer112.java:241: cannot find symbol
    symbol : variable toString
    location: class Employee
    return super.toString + "Faculty";
    ^
    Exer112.java:270: cannot find symbol
    symbol : variable toString
    location: class Employee
    return super.toString + "Staff";
    ^
    11 errors

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: inheritance & Polymorphism

    Let's take it from the top.

    Quote Originally Posted by njcountrygirl View Post
    here are the errors that I am getting

    Exer112.java:42: cannot find symbol
    symbol : constructor Student(java.lang.String,java.lang.String,java.lan g.String,java.lang.String,java.lang.String)
    location: class Student
    Person student = new Student("Mary Jane", "44 That Street", "123-123-1235", "mj@gmail.com", "Senior");
    ^
    That compiler error is rather clearly telling you that Student doesn't have a constructor that takes 5 Strings. Looking at the Student class code, it's clearly true; there are two constructors, one of which takes no parameters and the other takes 4 Strings and one int.

    Once you sort that out, the rest should be fairly easy. But do note that instance methods are invoked on an instance of the class, and method invocations always have parentheses, empty if no parameters are to be passed.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: inheritance & Polymorphism

    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: inheritance & Polymorphism

    1. You are calling your constructor with the wrong argument types.
    2. You cannot reference toString using the class name. You need to use the instance.
    3. In line 233, toString should be referenced as a method.

    Regards,
    Jim
    Last edited by jim829; 04-27-2015 at 05:13 PM. Reason: slow as usual
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: inheritance & Polymorphism

    Quote Originally Posted by KevinWorkman View Post
    How rude!

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    njcountrygirl is offline Member
    Join Date
    Feb 2015
    Posts
    5
    Rep Power
    0

    Default Re: inheritance & Polymorphism

    thanks everyone for all the help, I have successfully figured it out and it runs perfectly!

Similar Threads

  1. Compile-time Polymorphism or Run-time Polymorphism ?
    By dejulial in forum New To Java
    Replies: 1
    Last Post: 03-06-2012, 07:14 PM
  2. A question about inheritance / polymorphism
    By gabriels in forum New To Java
    Replies: 2
    Last Post: 02-26-2011, 11:53 AM
  3. Still trying to grasp Polymorphism and Inheritance
    By tashimoto in forum New To Java
    Replies: 15
    Last Post: 12-06-2010, 08:35 PM
  4. inheritance and polymorphism
    By tester in forum Eclipse
    Replies: 1
    Last Post: 12-21-2008, 04:58 AM
  5. Relation between Polymorphism and Inheritance
    By janakiram.attuluri in forum Advanced Java
    Replies: 1
    Last Post: 12-26-2007, 11:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •