Question: Inheritance - Creating a Multilevel Hierarchy In this lab you will start use Inheritance to create a Creating a Multilevel Hierarchy. Deliverable A zipped NetBeans
Inheritance - Creating a Multilevel Hierarchy
In this lab you will start use Inheritance to create a Creating a Multilevel Hierarchy.
Deliverable
A zipped NetBeans project with 2 classes
- app
- ZipCode
- Address
- Person
- Player
- FootballPlayer
- SoccerPlayer
Classes
Suggestion:
- Use Netbeans to copy your last lab (Lab 06) to a new project called Lab07.
- Close Lab06.
- Work on the new Lab07 project then.
The Person Class
- Uses encapsulation
- Attributes
- private String name
- private Address address
- Constructors
- one constructor with no input parameters
- since it doesn't receive any input values, you need to use the default values below:
- name - "John Doe"
- address - use the default constructor of Address
- since it doesn't receive any input values, you need to use the default values below:
- one constructor with all (two) parameters
- one input parameter for each attribute
- one constructor with no input parameters
- Methods
- public String toString()
- returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as one String.
- toString() is a special method, you will learn more about it in the next lessons
- it needs to be public
- it needs to have @override notation (on the line above the method itself). Netbeans will suggest you do it.
- Get and Set methods
- public int getName()
- public void setName(String name)
- public Address getAddress()
- public void setAddress(Address address)
- public String toString()
The Player Class (with updates from the last lab)
- Player is a Person with some extra attributes
- Uses encapsulation
- Player now is an abstract class because it has an abstract method
- public double getRatings( );
- an abstract method is an incomplete method that has to be implemented by the subclasses.
- public double getRatings( );
- Attributes
- private int number
- private String sports
- private gamesPlayed
- Constructors
- one constructor with no input parameters
- since it doesn't receive any input values, you need to use the default values below:
- number - 0
- sports - "none"
- gamesPlayed - 0
- since it doesn't receive any input values, you need to use the default values below:
- one constructor with all parameters
- one input parameter for each attribute
- one constructor with no input parameters
- Methods
- public String toString()
- returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as one String.
- toString() is a special method, you will learn more about it in the next lessons
- it needs to be public
- it needs to have @override notation (on the line above the method itself). Netbeans will suggest you do it.
- Get and Set methods
- public int getNumber()
- public void setNumber(int number)
- public String getSports()
- public void setSports(String sports)
- public int getGamesPlayed()
- public void setGamesPlayed(int gamesPlayed)
- public abstract getRatings();
- public String toString()
The SoccerPlayer Class
- SoccerPlayer is a Player with some extra attributes
- Uses encapsulation
- SoccerPlayer will implement the method getRatings (an abstract method from the superclass Player)
- toString has to include the result of getRatings() too
- Attributes
- private int goals
- private int yellowCards
- Constructors
- one constructor with no input parameters
- since it doesn't receive any input values, you need to use the default values below:
- goals - 0
- yellowCards - 0
- since it doesn't receive any input values, you need to use the default values below:
- one constructor with all (two) parameters
- one input parameter for each attribute
- one constructor with no input parameters
- Methods
- public String toString()
- returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as one String.
- toString() is a special method, you will learn more about it in the next lessons
- it needs to be public
- it needs to have @override notation (on the line above the method itself). Netbeans will suggest you do it.
- should also include the value of getRatings() in the string
- Get and Set methods
- public int getGoals()
- public void setGoals(int goals)
- public int getYellowCards()
- public void setYellowCards(int yellowCards)
- public double getRatings()
- calculate and return the rates using this formula:
- (double) (goals - yellowCards)/gamesPlayed
- the (double) is called casting, forcing the expression that comes afterwards to become a double.
- it is necessary to avoid getting 0 as a result because of the precision loss in the division by integers
- if goals or gamesPlayed is 0, return 0 (you need to do this test to avoid the application crashing in case one of them is 0)
- (double) (goals - yellowCards)/gamesPlayed
- calculate and return the rates using this formula:
- public String toString()
The FootballPlayer Class
- FootballPlayer is a Player with some extra attributes
- Uses encapsulation
- FootballPlayer will implement the method getRatings (an abstract method from the superclass Player)
- toString has to include the result of getRatings() too
- Attributes
- private int yards
- private int minutesPlayed
- Constructors
- one constructor with no input parameters
- since it doesn't receive any input values, you need to use the default values below:
- yards - 0
- minutesPlayed - 0
- since it doesn't receive any input values, you need to use the default values below:
- one constructor with all (two) parameters
- one input parameter for each attribute
- one constructor with no input parameters
- Methods
- public String toString()
- returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as one String.
- toString() is a special method, you will learn more about it in the next lessons
- it needs to be public
- it needs to have @override notation (on the line above the method itself). Netbeans will suggest you do it.
- should also include the value of getRatings() in the string
- Get and Set methods
- public int getYards()
- public void getYards(int yards)
- public int getMinutesPlayed()
- public void setMinutesPlayed(int minutesPlayed)
- public double getRatings()
- calculate and return the rates using this formula:
- (double) ( (yards - minutesPlayed/10.0) ) /gamesPlayed
- be careful with the parenthesis to avoid getting 0 as a result
- the (double) is called casting, forcing the expression that comes afterwards to become a double.
- it is necessary to avoid getting 0 as a result because of the precision loss in the division by integers
- use 10.0 instead of 10 to force Java to use more precision in the calculation
- if yards or gamesPlayed is 0, return 0 (you need to do this test to avoid the application crashing in case one of them is 0)
- (double) ( (yards - minutesPlayed/10.0) ) /gamesPlayed
- calculate and return the rates using this formula:
- public String toString()
App Person Creates a SoccerPlayer object spo using the no-parameter constructor. Creates a Soccer Player object sp1 using the all-parameter constructor. Attributes private String name; private Address address; - Creates a Football Player object fpo using the no-parameter constructor. Creates a Football Player object fp1 using the all-parameter constructor. Constructors No-parameter All-parameter - Using the method toString(), display all the data from each of the 4 objects. Methods String toString() Get/Set methods ZipCode Address . Attributes private String five Digit; private String plus4; Attributes private int number; private String name; private String type; private Zip Code zip; private String state; extends abstract Player Attributes private int number; private String sports; Private int gamesPlayed; Constructors No-parameter One-parameter Two-parameter Constructors No-parameter All-paramet Constructors No-parameter All-parameter Methods String toString() Get/Set methods void display() void display(int p) Methods String toString() Get/Set methods Methods String toString() Get/Set methods abstract double getRatings(); extends Soccer Player Football Player Attributes private int goals; private int yellowCards; Attributes private int yards; private minutesPlayed; Constructors No-parameter All-parameter Constructors No-parameter All-parameter Methods String toString() Get/Set methods double getRatings() Methods String toString() Get/Set methods double getRatings()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
