Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed

The Person Class

  • Uses encapsulation
  1. Attributes
    • private String name
    • private Address address
  1. 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
    • one constructor with all (two) parameters
      • one input parameter for each attribute
  2. 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)

The Player Class

  • Player is a Person with some extra attributes
  • Uses encapsulation
  1. Attributes
    • private int number
    • private String sports
    • private int gamesPlayed
  1. 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
    • one constructor with all (three) parameters
      • one input parameter for each attribute
  2. 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)

The Address Class

  • Uses encapsulation
  1. Attributes
    • private int number
    • private String name
    • private String type
    • private ZipCode zip
    • private String state
  1. 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)
    • one constructor with all (five) parameters
      • one input parameter for each attribute
  2. 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)

The ZipCode Class (updated to include encapsulation)

  • Uses encapsulation
  1. Attributes
    • private String fiveDigit
    • private String plus4
  2. 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"
    • 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
  3. 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
    • 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

The App class

  1. create a Player object p0 using the no-parameter constructor
  2. 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
  3. 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

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

Current Trends In Database Technology Edbt 2004 Workshops Edbt 2004 Workshops Phd Datax Pim P2panddb And Clustweb Heraklion Crete Greece March 2004 Revised Selected Papers Lncs 3268

Authors: Wolfgang Lindner ,Marco Mesiti ,Can Turker ,Yannis Tzitzikas ,Athena Vakali

2005th Edition

3540233059, 978-3540233053

More Books

Students also viewed these Databases questions