Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Inheritance - Creating a Multilevel Hierarchy In this lab, you will start to use Inheritance to create a Creating a Multilevel Hierarchy. Deliverable A zipped

Inheritance - Creating a Multilevel Hierarchy

In this lab, you will start to use Inheritance to create a Creating a Multilevel Hierarchy.

Deliverable

A zipped NetBeans project with 7 classes

App

Person

Height

Student

Player

FootballPlayer

SoccerPlayer

Classes

The Student class

A Student is a Person with 3 extra attributes, major, academic year and gpa.

Attributes

String major

String academicYear

double GPA

Constructorsone constructor with no input parameterssince it doesn't have input parameters, use the default data below

major - IST

academicYear - Sr.

GPA - 3.0

remember to call the constructor of the superclass with no parameters

one constructor with all the parameters

all three parameters from Student, one for each attribute

since this is a subclass, remember to include also the parameters for the superclass

Methods

Get and Set methods (a requirement from encapsulation)

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.

important: in the subclass toString, remember to make a call to the superclass toString so that the resulting String includes

data from the subclass Student

data from the superclass Person

The Person Class

Attributes

String firstName

String lastName

String hometown

String state

Height height

Constructorsone constructor with no input parameterssince it doesn't receive any input values, you need to use the default values below:

firstName - No

lastName - Name

hometown - N/A

state - N/A

height - use the Height class no parameter constructor

one constructor with three parameters

firstName using the input parameter

lastName using the input parameter

height using the input parameter

use the default values for

hometown - N/A

state - N/A

one constructor with all (five) parameters

one input parameter for each attribute

Methods

Get and Set methods (a requirement from encapsulation)

important:

You should start generating the default get and set methods using NetBeans automatic generator

Then you will change getFirstName and setLastName as specified. getFirstName

returns firstName with the first letter in upper case and the remaining of the String in lower case

getLastName

getHometown

getState

getHeight

setFirstName

setLastName

updates lastName to be all caps (all upper case)

remember to use setLastName in all constructors so the data (the updated lastName) is stored correctly

setHometown

setState

setHeight

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.

regarding state, the toString method will have a similar functionality as App had in the first lab.

if the state attribute is "PA", display the object's attribute name plus the message "is from Pennsylvania"

if the state attribute is not "PA", display the object's attribute name plus the message "is from out-of-state"

In short, the toString() method returns all the data from each object as a String

public void initials( )this method

gets firstName and lastName

extract the initials of each one of them

adds a "." period to each of them

and uses "System.out.println" to display them as one String

public void initials( int option)

this method overloads public void initials( ). This means, it has the same name, but a different number of parameters.

if the value of "option" is 1 gets firstName

extract its initials

adds a "." period to to a String

adds the lastName to this String

and uses "System.out.println" to display the String

if the value of "option" is 2

adds firsName to a String

gets lastName

extract its initials

adds a "." period to it

adds it to the String

and uses "System.out.println" to display the String

The Height class

uses encapsulation

private attributes

get and set methods for each attribute

Attributes

int feet

int inches

Constructorsone constructor with no input parameterssince it doesn't receive any input values, you need to use the default values below:

feet - 5

feet - 6

one constructor with two parameters

feet using the input parameter

inches using the input parameter

Methodspublic 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 Player Class (now an abstract class)

Player is a Student with some extra attributes

Uses encapsulation

Player now is an abstract class because it has an abstract methodpublic double getRatings( );

an abstract method is an incomplete method that has to be implemented by the subclasses.

Attributes

private int number

private String sports

private gamesPlayed

Constructorsone constructor with no input parameterssince it doesn't receive any input values, you need to use the default values below:

number - 0

sports - "none"

gamesPlayed - 0

one constructor with all parameters

one input parameter for each attribute

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();

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

Constructorsone constructor with no input parameterssince it doesn't receive any input values, you need to use the default values below:

goals - 0

yellowCards - 0

one constructor with all (two) parameters

one input parameter for each attribute

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 ratings 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)

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

Constructorsone constructor with no input parameterssince it doesn't receive any input values, you need to use the default values below:

yards - 0

minutesPlayed - 0

one constructor with all (two) parameters

one input parameter for each attribute

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 ratings 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 afterward 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)

The App class

create a SoccerPlayer object called sp0 using the no-parameter constructor

create a SoccerPlayer object called sp1 using the all-parameter constructor with the values

goals - 5

yellowCards - 2

number - 7

sports - Soccer

gamesPlayed - 10

major - Cyber

academicYear - Sr.

GPA - 3.5

firstName - jillian (see the different capitalization used to test the get/set methods)

lastName - Jennings

height 5 7

hometown - Montclair

state - NJ

create a FootballPlayer object called fp0 using the no-parameter

create a FootballPlayer object called fp1 using the all-parameter constructor with the values

yards - 60

minutesPlayed -30

number - 26

sports - Football

gamesPlayed - 10

major - IST

academicYear - Jr.

GPA - 3.5

firstName - KEATON (see the different capitalization used to test the get/set methods)

lastName - Ellis

height 5 11

hometown - State College

state - PA

display all the data from each object

Output

Important

you need to display each class toString( ) in a separate line.

You can do this by adding a " " at the end of the string.

In the last toString you will also add a "==============================================================" String to close the line.

The output should be similar to

Person{firstName=No, lastName=NAME, hometown=N/A, state=N/A, height=Height{feet=5, inches=6}} Student{major=IST, academicYear=Sr., GPA=3.0} Player{number=0, sports=none, gamesPlayed=0} SoccerPlayer{goals=0, yellowCards=0, ratings= 0.0} ============================================================== Person{firstName=Jillian, lastName=JENNINGS, hometown=Montclair, state=out-of-state, height=Height{feet=5, inches=7}} Student{major=Cyber, academicYear=Sr., GPA=3.5} Player{number=7, sports=Soccer, gamesPlayed=10} SoccerPlayer{goals=5, yellowCards=2, ratings= 0.3} ============================================================== Person{firstName=No, lastName=NAME, hometown=N/A, state=N/A, height=Height{feet=5, inches=6}} Student{major=IST, academicYear=Sr., GPA=3.0} Player{number=0, sports=none, gamesPlayed=0} FootballPlayer{yards=0, minutesPlayed=0, ratings=0.0} ============================================================== Person{firstName=Keaton, lastName=ELLIS, hometown=State College, state=Pennsylvania, height=Height{feet=5, inches=11}} Student{major=IST, academicYear=Jr., GPA=3.5} Player{number=26, sports=Football, gamesPlayed=10} FootballPlayer{yards=60, minutesPlayed=30, ratings=5.7} ==============================================================

I need code for each class described to produce the listed output alongside any explanations of concepts, thank you for your assistance!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Samsung Galaxy S23 Ultra Comprehensive User Manual

Authors: Leo Scott

1st Edition

B0BVPBJK5Q, 979-8377286455

More Books

Students also viewed these Databases questions