Question: Part 1 - Academic Class Let's create a program to model the people who attend and work at CSS. We will use inheritance to help

Part 1 - Academic Class

Let's create a program to model the people who attend and work at CSS. We will use inheritance to help us model this situation (and save us some coding).

Create an instantiable class called Academic. An Academic represents a person at CSS. The Academic class should have the following instance variables:

  • id (String) the person's university id number. Students have a student id; staff members have an employee id.
  • firstName (String) the person's first name.
  • lastName (String) the person's last name.
  • department (String) the department the person belongs to. For students, this is the department of his/her major; for staff, this is the department he/she works in.

Create a default constructor for this class (one that doesn't take in any parameters), and create get and set methods for each data member.

Next, let's create a main class to instantiate the Academic class. Create a class named College and write a main method. Create an Academic object to represent someone who works at CSS (not a student). Use the set methods to set the data members of the object.

Finally, print out the Academic object by putting it in a print statement. For example, if your object name is "staffMember", your print statement would look like this:System.out.println(staffMember);

Run you program. What output do you see?

You probably see output that look something like this: Academic@33909752. This is because Java doesn't know how to display an Academic object. By default, Java displays the class name and the memory address of the object. This isn't too helpful for us.

In order to display useful information about our Academic object, let's override the toString method. Java will call our toString method when trying to print out the Academic object. Write a toString method in the Academic class. Your toString method should look something like this:

@Override public String toString() { String text = USE INSTANCE VARIABLES HERE return text; } 

Now run your program again. You should now see useful information about your Academic object.

Part 2 - Student Class

The Academic class stores essential information about anyone here at the college, but it doesn't store student-specific information. Let's create a Student class we can use to model students at our college. A Student is an Academic, meaning students have everything academics have (id, first name, last name, department), but they also have other properties academics don't have (major, minor, and gpa, for instance). We need the id, first name, last name, and department data members in the Student class, but we don't want to rewrite them all (along with their get/set methods). Instead, we will inherit from the Academic class.

Write a Student class with the following instance variables:

  • major (String) the student's major area of study. Set to "Undeclared" initially.
  • minor (String) the student's minor area of study. Set to "Undeclared" initially.
  • gpa (double) the student's grade point average. Set to 0.0 initially.

Write a default constructor and get/set methods for the Student class. To inherit from the Academic class, write "extends Academic" at the end of your class declaration:

public class Student extends Academic // Student class code. } 

In the College class, create a Student object. Use the set methods to store information about the student. Print the Student object using a print statement. Run your program what information do you see about your student?

You'll notice that even though you didn't write a toString method for the Student class, Java has figured out how to display information about your student (you aren't seeing a memory address). How did Java know what to print?

The information displayed for the Student is useful, but it doesn't include all the information about the student. Let's fix that. Write a toString method in the Student class. Make sure your toString method includes all the information in the Academic data members and the Student data members. One way to do this is to call the Academic class's toString method to get a String with the Academic information. You can then append Student information onto that. To call the Academic class's toString method, call super.toString(); They keyword "super" refers to the class this class inherits from.

Run your program again. Do you see all the student's information being printed to the screen?

Part 3 - GraduateStudent Class

Our program is fairly comprehensive now. When we need to represent college personnel, we can create Academic objects. When we need to represent the college's students, we can create Student objects. We have used inheritance to make efficient use of our code. There is one more student population we need to represent: graduate students. Graduate students have all the same information as students, but they also have one additional piece of information: a thesis title.

Write a GraduateStudent class with a thesis title instance variable. Since graduate students are students, the GraduateStudent class should inherit from the Student class. Write a default constructor and get/set methods.

In the College class, create a GraduateStudent object. Set appropriate information in the GraduateStudent object. Print the object to the screen. You should see all the Academic and Student information about this object, but you will be missing the GraduateStudent information.

Write a toString method in the GraduateStudent class. Run your program again and verify that you are seeing the correct information for your GraduateStudent object.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!