Allan Didier

Inheritance Intro

Goals

To write classes that can inherit properties from another class.

Skills

  • Inheritance

Program Details

Write a program which can instantiate, modify, and output statistics for a few sports players. You can choose your own statistics or the use ones provided below. Your program will need the following:

  1. A Player Class with
    1. 3 private variables for the player’s name, salary, and years played.
    2. A private static variable for a minimum salary, initially set to $50,000. 
    3. A constructor method that instantiates the name, salary, and years played by passing in values. You don’t need to ask input from the console.
    4. Public setter and getter methods to change and return the 4 private variables.
      1. You can use the Eclipse Source-Generate Setters and Getters to accomplish this.
      2. The setSalary method should make sure the salary is not set below the minimum salary.  
    5. A toString() method to output the player information to a string.
  2. Three Child Classes that inherit the Player class, such as a Quarterback, Running Back, and Field Goal Kicker classes.
    1. The child classes should each have
      1. 3 private variables specific to the class. Examples could be:
        1. Quarterback: touch downs thrown, pass percentage, and yards per game.
        2. Running Back: touch downs made, yards per carry, and longest run.
        3. Field Goal Kicker: field goals made, field goal percentage, and longest field goal made.
      2. A constructor method that instantiates the 3 variables above by passing them directly in. It will also need to pass in the three Player variables and run the Player constructor method to instantiate the Player variables. You don’t need a constructor that asks the user for input.
      3. Public setter and getter methods for each of these variables. Setters should report that a change has been made.
      4. A toString method to output the player information to a string. This method should call the parent toString method to output the Player information.
  3. A Main Program that
    1. Creates an Array or ArrayList of players (the parent class).
    2. Instantiates 3 different Players into the list, one from each child class.
      1. The player data can be instantiated directly in the code. You do not need to get input from the console.
      2. Instantiate a player with a salary below the minimum to make sure their salary is not below the minimum.
    3. Modifies each player twice. Use a parent setter and a child setter for each player modification.
    4. Modifies the minimum salary.
    5. Change a player’s salary to below the minimum to make sure the salary cannot go below the new minimum. 
    6. Outputs the initial and final player stats to the console.

Resources

Java Textbook

Chapter 9: Inheritance

CS Awesome Website

Bill Barnum's AP Computer Science A videos

AP College Board test prep videos

AP College Board:  Overriding Methods