Chapter 7: Inheritance – Student

Inheritance – Student Exercise
student
Use this implementation of Student and StudentAthlete ADTs to show inheritance concepts.

  1. Describe what the Student ADT looks like:
    A good description should include the name of the instance fields, whether the ADT has constructor(s), the methods’ purpose, and names.
  2. public class Student
    {
       private String name;
       private int numCourses;
    
       //-----------------------------------------------------------------
       //  Sets up a student with the specified name and number of
       //  courses.
       //-----------------------------------------------------------------
       public Student (String studentName, int courses)
       {
          name = studentName;
          numCourses = courses;
       }
    
       //-----------------------------------------------------------------
       //  Returns information about this student as a string.
       //-----------------------------------------------------------------
       public String toString()
       {
          String result = "Student name: " + name + "\n";
    
          result += "Number of courses: " + numCourses;
    
          return result;
       }
    }
    
  3. Describe what the StudentAthlete ADT looks like:
    A good description should include the name of the instance fields, whether the ADT has constructor(s), the methods’s purpose, and names.
    Mostly clearly identify attributes, any overridden methods, any methods that belong to the child class. As a final sentence describe the relationship between Student and StudentAthlete.
  4. //********************************************************************
    //  StudentAthlete.java       Author: Lewis/Loftus/Cocking
    //
    //  Represents a student athlete who plays a sports team for the school.
    //  Used to demonstrate inheritance.
    //********************************************************************
    
    public class StudentAthlete extends Student
    {
       private String sport;
    
       //-----------------------------------------------------------------
       //  Sets up the student athlete using the specified information.
       //-----------------------------------------------------------------
       public StudentAthlete (String studentName, int courses,
                           String sportName)
       {
          super (studentName, courses);
    
          sport = sportName;
       }
    
       //-----------------------------------------------------------------
       //  Returns a description of this graduate student as a string.
       //-----------------------------------------------------------------
       public String toString()
       {
          String result = super.toString();
    
          result += "\nSport: " + sport;
    
          return result;
       }
    }
    
    
    
  5. Describe the Academia driver class.
  6. //********************************************************************
    //  Academia.java       Author: Lewis/Loftus/Cocking
    //
    //  Demonstrates the use of methods inherited from the Object class.
    //********************************************************************
    
    public class Academia
    {
       //-----------------------------------------------------------------
       //  Creates objects of two student types, prints some information
       //  about them, then checks them for equality.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          Student Frank = new Student ("Frank", 5);
          StudentAthlete Suki = new StudentAthlete ("Suki", 4, "Soccer");
    
          System.out.println (Frank);
          System.out.println ();
    
          System.out.println (Suki);
          System.out.println ();
    
          if (! Frank.equals(Suki))
             System.out.println ("These are two different students.");
       }
    }
    
    
    
  7. Add accessor and mutator methods for the studentName and sportName. NOTE: do not change any visibility specifier.
  8. In the Academia driver class, include calls to the methods you just implemented.

All source: Java Software Solutions by Lewis, Loftus and Cocking