Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Inheritance - Extending a class In this lab you will start using Inheritance to create a new class from an existing one. Deliverable A zipped
Inheritance - Extending a class
In this lab you will start using Inheritance to create a new class from an existing one.
Deliverable
A zipped NetBeans project with 2 classes
- app
- ZipCode
- Address
- Person
- Player
Classes
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 String getName()
- public void setName(String name)
- public Address getAddress()
- public void setAddress(Address address)
- public String toString()
The Player Class
- Player is a Person with some extra attributes
- Uses encapsulation
- Attributes
- private int number
- private String sports
- private int 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 (three) 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 String toString()
The Address Class
- Uses encapsulation
- Attributes
- private int number
- private String name
- private String type
- private ZipCode zip
- private String state
- 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
- name - "N/A"
- type - "Unknown"
- zip - use the default constructor of ZipCode
- state - " " (two spaces)
- since it doesn't receive any input values, you need to use the default values below:
- one constructor with all (five) 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 getName()
- public void setName(String name)
- this method will receive an input parameter name and will correct if necessary to make its first letter upper case and the remaining part of the word lower case (correcting MAIN to Main, for instance)
- it will work for at least 2 words (correcting north atherton to North Atherton, for instance)
- the attribute name will be updated with the corrected value
- public String getType()
- this method will return a corrected value depending on the value of the attribute type.
- it will return "Dr." if the attribute type is "Drive"
- it will return "Ave." if the attribute type is "Avenue"
- it will return "St." if the attribute type is "Street"
- it will return the value of the attribute type for any other cases
- public void setType(String type)
- public ZipCode getZip()
- public void setZip(ZipCode zip)
- public String getState()
- public void setState(String state)
- public String toString()
The ZipCode Class (updated to include encapsulation)
- Uses encapsulation
- Attributes
- private String fiveDigit
- private String plus4
- Constructors
- one constructor with no input parameters
- since it doesn't receive any input values, you need to use the default values below:
- private fiveDigit - "00000"
- private plus4 - "0000"
- since it doesn't receive any input values, you need to use the default values below:
- one constructor with one parameter
- one input parameter for fiveDigit
- use the default value from the no-parameter constructor to initialize plus4
- one constructor with all (two) parameters
- one input parameter for each attribute
- one constructor with no input parameters
- Methods (updated to include Get and Set 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.
- the toString method will have a similar functionality as App had in the first lab.
- it returns all the data from each object as a String
- if the second attribute, plus4, is blank, display only the first attribute fivedigit, for instance, 16801
- if the second attribute, plus4, is not blank, display only the first attribute fivedigit followed by a "-" and then the plus4 attribute, for instance, 16802-1503
- it returns all the data from each object as a String
- Get and Set methods for each of the two attributes
- display()
- this method gets the "toString()" value (whatever is returned by toString() ) and uses "System.out.println" to display it.
- you need to decide what is the type of this method
- displayPrefix(int p)
- this method receives an input parameter, an int number p
- based on p's value
- if p's value is 1
- uses "System.out.println" to display the zipcode's prefix, i.e., its 3 first digits.
- if the fiveDigit is "10022", displays "100"
- if p's value is2
- uses "System.out.println" to display the zipcode's area, i.e., its fourth and fifth digits.
- if the fiveDigit is "10022", displays "22"
- for any other value of p, it should not display anything
- if p's value is 1
- public String toString()
The App class
- create a Player object p0 using the no-parameter constructor
- create a Player object p1 using the all-parameter constructor with the value
- name "Julia Dohle"
- address
- number - 10
- name - Old Main
- type - Street
- zip
- fiveDigit - 16802
- plus4 - 0001
- state - PA
- number - 1
- sports - "Soccer"
- gamesPlayed - 5
- display all the data from each object using the method toString()
Output
The output should be similar to
Person{name=John Doe, address=Address{number=0, name=N/A, type=Unknown, zip=00000, state= }} Player{number=0, sports=none, gamesPlayed=0} Person{name=Julia Dohle, address=Address{number=10, name=Old Main, type=St., zip=16802-0001, state=PA}} Player{number=1, sports=Soccer, gamesPlayed=5}App Person - Creates a Player object po using the no-parameter constructor. - Creates a Player object p1 using the all-parameter constructor. Attributes private String name; private Address address; - Using the method toString(), display all the data from each of the 2 objects. Constructors No-parameter All-parameter ZipCode Address Attributes private String fiveDigit; private String plus4; Attributes private int number; private String name; private String type; private Zip Code zip; private String state; Methods String toString() Get/Set methods extends Constructors No-parameter One-parameter Two-parameter Constructors No-parameter All-parameter Player Attributes private int number; private String sports; Private int gamesPlayed; . Methods String toString() Get/Set methods void display() void displayPrefix(int p) Methods String toString() Get/Set methods Constructors No-parameter All-parameter Methods String toString() Get/Set methods
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started