Question

Implement a superclass Person. Make two classes, Student and Instructor, that inherit from Person. A person...

Implement a superclass Person. Make two classes, Student and Instructor, that inherit from Person. A person has a name and a year of birth. A student has a major, and an instructor has a salary. Writhe the class definitions, the constructors, and the methods toString for all classes. For Person the constructors should include a no-arg constructor and one that accepts in the name of the person. For student there should be a no-arg constructor and a constructor that accepts in both name and major. And, for Instructor there should be a no-arg constructor as well as one that accepts in name and salary.

Supply a test program that exercises all of the capabilities of each class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Person.java


public class Person {
   public String name;
   public int year;
   // no-arg constructor
   Person(){
       name = "DEFAULT";
       year = 0;
   }
   Person(String name){
       this.name = name;
       year = 0;
   }
   public String toString(){
       return String.format("Name: %s\nYear: %d", name, year);
   }
  
}

_______________________________________________________________________________________

\underline{Student.java}


public class Student extends Person{
   private String major;
   Student(){
       super();
       major = "DEFAULT";
   }
   Student(String name, String major){
       super(name);
       this.major = major;
   }
   public String toString(){
       return super.toString() + String.format("\nMajor: %s", major);
   }
}

__________________________________________________________________________________________

\underline{Instructor.java}


public class Instructor extends Person{
   private double salary;
   Instructor(){
       super();
       salary = 0;
   }
   Instructor(String name, double salary){
       super(name);
       this.salary = salary;
   }
   public String toString(){
       return super.toString() + String.format("\nSalary: %.2f", salary);
   }
}

__________________________________________________________________________________________

\underline{PersonTest.java}


public class PersonTest {
   public static void main(String args[]){
       Student s1 = new Student();
       Student s2 = new Student("Student2", "Major2");
       Instructor i1 = new Instructor();
       Instructor i2 = new Instructor("Instructor2", 20);
       Person p1 = new Person();
       Person p2 = new Person("Person2");
       System.out.println("Student 1: \n" + s1);
       System.out.println("\nStudent 2: \n" + s2);
       System.out.println("\nInstrucotr 1: \n" + i1);
       System.out.println("\nInstrucotr 2: \n" + i2);
       System.out.println("\nPerson 1: \n" + p1);
       System.out.println("\nPerson 2: \n" + p2);
   }
}

_____________________________________________________________________________________________

\underline{Output:}

Add a comment
Answer #2
class Person {
  private String name;
  private int year;
  Person(String name, int year) {
    this.name = name;
    if (year <= 2012) this.year = year;
    else this.year = -1;
  }
  public String toString() {
    return "My name is "+ this.name + this.year + "This is a string";
  }
}

class Student extends Person {
  String major;
  Student(String name, int year, String major) {
    super(name, year);
    this.major = major;
  }
  public String toString() {
    return "" + super.toString() + this.major;
  }
}

class Instructor extends Person {
  double salary;
  Instructor(String name, int year, double salary) {
    super(name, year);
    this.salary = salary;
  }
  public String toString() {
    return "Instructor: " + super.toString() + " with salary " + this.salary;
  }
}

class Story {
  public static void main(String[] args) {
    Instructor a = new Instructor("Laura", 1980, 12345.67);
    Student b = new Student("Janice", 1965, "History");
    Person c = new Instructor("Bob", 1979, 10000.01);
    Person d = new Student("Joe", 1995, "Accounting");
    Person e = new Person("Johnny Bravo", 1991);
    System.out.println( a );
    System.out.println( b );
    System.out.println( c );
    System.out.println( d );
    System.out.println( e );


  }
}
Add a comment
Know the answer?
Add Answer to:
Implement a superclass Person. Make two classes, Student and Instructor, that inherit from Person. A person...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement a superclass Adult. Make two classes, Worker and Manager, that inherit from Adult. Each adult...

    Implement a superclass Adult. Make two classes, Worker and Manager, that inherit from Adult. Each adult has a name and a year of birth. Each worker has a job title of “engineer” and each manager has a job title of “supervisor”. Write the class declarations, the constructors, and the methods toString for all classes. Supply a driver class that creates an array of Worker objects and sort them by the birth years. Moreover, create an array of Manager objects but...

  • Please send me this answer using netbeans in java programming with sample output in caption shown...

    Please send me this answer using netbeans in java programming with sample output in caption shown in picture Implement a super class Person. Make two classes, Student and instructor, inherit from Person. A person has a name and a year of birth. A student has a major and an instructor has a salary. Write the class definitions, the constructors, and the methods toString for all classes. Supply a test program that test these classes and methods C: WINNT System321cmd.exe Student...

  • For task 2, implement the three classes (Person, Student, and Staff) that are described above. Note...

    For task 2, implement the three classes (Person, Student, and Staff) that are described above. Note that classes Staff and Student are derived from the superclass Person -- in other words Staff and Student inherit from Person. Make sure that the subclasses Student and Staff invoke the superclass' constructors and correctly inherit the variables and methods from the superclass Person. Also note that Person is an abstract class. <abstract> Person -name:String -address:String = “String” +getName():String +getAddress():String +setAddress(address:String):void +setName(name:String):void Staff -school:String...

  • Computer Programming II (CS141) Q.1. Why would you use an inner class instead of regular class?...

    Computer Programming II (CS141) Q.1. Why would you use an inner class instead of regular class? Q.2. What does this code print? Why is it polymorphic? DataSet data = new DataSet(); data.add(new BankAccount(1000)); data.add(new Coin(0.1, "dime")); System.out.println(data.getAverage()); Q.3. Can you convert a superclass reference into a subclass reference? A subclass reference into a superclass reference? If so, give examples. If not, explain why not. Q.4. Implement a superclass Person. Make two classes, Student and Instructor that inherit from Person. A...

  • Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman...

    Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman, sophomore, junior, or senior). Define the possible status values using an enum. Staff has an office, salaray, and date-hired. Implement the above classes in Java. Provide Constructors for classes to initialize private variables. Getters and setters should only be provided if needed. Override the toString() method...

  • Java: student directory GUI You need to implement three classes: Person Student StudentDirectory StudentMain Start by...

    Java: student directory GUI You need to implement three classes: Person Student StudentDirectory StudentMain Start by implementing Person and Student classes. Once you are sure you can serialize and deserialize and ArrayList of Students to and from a file, move on to building the GUI application. Person: The Person class should implement serializable interface. It contains the following: Person's first name (String) Person's last name (String) Person's id number Person's date of birth (Date) public String toString(): This method method...

  • a c++ program (The Person, Student, Employee, Faculty, and Staff classes) Design a class named Person...

    a c++ program (The Person, Student, Employee, Faculty, and Staff classes) 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, salary, and date hired. Use the MyDate classto create an object for date hired. A faculty...

  • Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person...

    Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...

  • Design a class named Person and its two derived classes named Student and Employee. MakeFaculty and Staff derived classes of Employee

    In Java(The Person, Student, Employee, Faculty, and Staff classes)Design a class named Person and its two derived classes named Student and Employee. MakeFaculty and Staff derived classes of Employee. A person has a name, address, phone number, and e-mail address. A student has a class status (freshman, sophomore, junior, or senior). An employee has an office, salary, and date hired. Define a class namedMyDate that contains the fields year, month, and day. A faculty member has office hours and a rank....

  • In this assignment, you will implement Address and Residence classes. Create a new java project. Part...

    In this assignment, you will implement Address and Residence classes. Create a new java project. Part A Implementation details of Address class: Add and implement a class named Address according to specifications in the UML class diagram. Data fields: street, city, province and zipCode. Constructors: A no-arg constructor that creates a default Address. A constructor that creates an address with the specified street, city, state, and zipCode Getters and setters for all the class fields. toString() to print out all...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT