Question

In: Computer Science

The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with...

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 int getName()
      • public void setName(String name)
      • public Address getAddress()
      • public void setAddress(Address address)

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.
  1. Attributes
    • private int number
    • private String sports
    • private 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)
    • 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
  1. Attributes
    • private int goals
    • private int yellowCards
  1. 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
    • 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.
        • 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)

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
  1. Attributes
    • private int yards
    • private int minutesPlayed
  1. 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
    • 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.
        • 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)

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 SoccerPlayer object sp0 using the no-parameter constructor
  2. create a SoccerPlayer object sp1 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 - 7
    • sports - "Soccer"
    • gamesPlayed - 10
    • goals - 5
    • yellowCards - 1
  3. create a FootballPlayer object fp0 using the no-parameter constructor
  4. create a FootballPlayer object fp1 using the all-parameter constructor with the value
    • name "Saquon Barkley"
    • address
      • number - 10
      • name - Old Main
      • type - Street
      • zip
        • fiveDigit - 16802
        • plus4 - 0001
      • state - PA
    • number - 26
    • sports - "Football"
    • gamesPlayed - 10
    • yards - 80
    • minutesPlayed - 220
  5. display all the data from each object using the method toString()

Solutions

Expert Solution

SHORT SUMMARY:

  • Implemented the mentioned classes and functions as per the requirements.
  • Provided the source code and sample output of the code.

SOURCE CODE:

Person.java:

/*

** Person class

*/

public class Person {

    private String name;

    private Address address;

    // defaulut constructor

    public Person() {

        this.name = "John Doe";

        this.address = new Address();

    }

    // two argument construtor

    public Person(String name, Address address) {

        this.name = name;

        this.address = address;

    }

    @Override

    public String toString() {

        return "Name : " + name + "\nAddress : " + address;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public Address getAddress() {

        return address;

    }

    public void setAddress(Address address) {

        this.address = address;

    }

}

Player.java:

/**

*

* class Player

*/

abstract public class Player extends Person {

   

    private int number;

    private String sports;

    private int gamesPlayed;

    public Player() {

        number = 0;

        sports = "none";

        gamesPlayed = 0;

    }

    public Player( String name, Address address,int number,

            String sports, int gamesPlayed) {

        super(name, address);

        this.number = number;

        this.sports = sports;

        this.gamesPlayed = gamesPlayed;

    }

   

    

    @Override

    public String toString() {

        String content = super.toString();

        content += "\nNumber : " + number + "\nSports :" + sports +

                "\nGames Played : " + gamesPlayed ;

        return content;

    }

    public int getNumber() {

        return number;

    }

    public void setNumber(int number) {

        this.number = number;

    }

    public String getSports() {

        return sports;

    }

    public void setSports(String sports) {

        this.sports = sports;

    }

    public int getGamesPlayed() {

        return gamesPlayed;

    }

    public void setGamesPlayed(int gamesPlayed) {

        this.gamesPlayed = gamesPlayed;

    }

   

    public abstract double getRatings();

   

}

SoccerPlayer.java:

/**

*

* SoccerPlayer class

*/

public class SoccerPlayer extends Player{

    private int goals;

    private int yellowcards;

    public SoccerPlayer() {

        this.goals = 0;

        this.yellowcards = 0;

    }

    public SoccerPlayer(String name, Address address, int number,

            String sports, int gamesPlayed,int goals, int yellowcards) {

        super(name, address, number, sports, gamesPlayed);

        this.goals = goals;

        this.yellowcards = yellowcards;

    }

    @Override

    public String toString() {

        String content = super.toString();

        content += "\nGoals : " + goals + "\nYellow Cards : " + yellowcards;

        return content;

    }

   

    

    public int getGoals() {

        return goals;

    }

    public void setGoals(int goals) {

        this.goals = goals;

    }

    public int getYellowcards() {

        return yellowcards;

    }

    public void setYellowcards(int yellowcards) {

        this.yellowcards = yellowcards;

    }

   

    @Override

    public double getRatings() {

        return (double)(goals - yellowcards)/getGamesPlayed();

    }

   

}

FootBallPlayer.java:

/**

*

* FootBallPlayer class

*/

public class FootBallPlayer extends Player {

    private int yards;

    private int minutesPlayed;

    public FootBallPlayer() {

        yards = 0;

        minutesPlayed = 0;

    }

    public FootBallPlayer( String name, Address address, int number,

            String sports, int gamesPlayed,int yards, int minutesPlayed) {

        super(name, address, number, sports, gamesPlayed);

        this.yards = yards;

        this.minutesPlayed = minutesPlayed;

    }

    @Override

    public String toString() {

        String content = super.toString();

        content += "\nYards : " + yards + ", "+ "\nMinutes Played :" + minutesPlayed ;

        return content;

    }

    public int getYards() {

        return yards;

    }

    public void setYards(int yards) {

        this.yards = yards;

    }

    public int getMinutesPlayed() {

        return minutesPlayed;

    }

    public void setMinutesPlayed(int minutesPlayed) {

        this.minutesPlayed = minutesPlayed;

    }

    @Override

    public double getRatings() {

        return (double) ((yards - minutesPlayed / 10.0)) / getGamesPlayed();

    }

}

Address.java:

/**

*

* Address class

*/

class Address {

//attributes of Address class

    private int number;

    private String name;

    private String type;

    private ZipCode zip;

    private String state;

    //no argument constructor

    public Address() {

        number = 0;

        name = "N/A";

        type = "Unknown";

        zip = new ZipCode();

        state = " ";

    }

    //constructor with 5 parameters

    public Address(int number, String name, String type, ZipCode zip, String state) {

        setNumber(number);

        setName(name);

        setType(type);

        setZip(zip);

        setState(state);

    }

    @Override

    public String toString() {

        return number + ", " + name + ", " + type + " " + zip + ", " + state;

    }

    //getter of number

    public int getNumber() {

        return number;

   }

    //setter of number

    public void setNumber(int number) {

        this.number = number;

    }

    //getter of name

    public String getName() {

        return name;

    }

    // setter of name

    public void setName(String name) {

        if (name == null || name.isEmpty()) {

            this.name = name;

        } else {

            this.name = "";

            //convert first character of the name to UPPER case

           

            // Take the first and last name from name

            String[] names = name.split("\\s+");

            for(String inname: names){

                // Convert the first char to upper

                // and rest all to lower

                if(this.name != ""){

                    this.name += " ";

                }

                this.name += inname.substring(0, 1).toUpperCase() +

                        inname.substring(1).toLowerCase();

            }

        }

    }

    //getter of type

    public String getType() {

        if (type.equalsIgnoreCase("Drive")) {

            return "Dr.";

        } else if (type.equalsIgnoreCase("Avenue")) {

            return "Ave.";

        } else if (type.equalsIgnoreCase("Street")) {

            return "St.";

        } else {

            return type;

        }

    }

    // setter of type

    public void setType(String type) {

        this.type = type;

    }

    // getter of zipcode

    public ZipCode getZip() {

        return zip;

    }

    // setter of zipcode

    public void setZip(ZipCode zip) {

        this.zip = zip;

    }

    // getter of state

    public String getState() {

        return state;

    }

    //setter of state

    public void setState(String state) {

        this.state = state;

    }

}

ZipCode.java:

/*

** ZipCode class

*/

class ZipCode {

    //attributes of zipcode class

    private String fiveDigit;

    private String plus4;

    //no argument constructor

    public ZipCode() {

        fiveDigit = "00000";

        plus4 = "0000";

    }

    //one argument constructor

   public ZipCode(String fiveDigit) {

        // use default constructor to initialize plus4

        this();

        this.fiveDigit = fiveDigit;

    }

    //two argument constructor

    public ZipCode(String fiveDigit, String plus4) {

        this.fiveDigit = fiveDigit;

        this.plus4 = plus4;

    }

    @Override

    public String toString() {

        return fiveDigit + "-" + plus4;

    }

    // getters and setters of each of the attribute

    public String getFiveDigit() {

        return fiveDigit;

    }

    public void setFiveDigit(String fiveDigit) {

        this.fiveDigit = fiveDigit;

    }

    public String getPlus4() {

        return plus4;

    }

    public void setPlus4(String plus4) {

        this.plus4 = plus4;

    }

    public void display() {

        System.out.println(toString());

    }

    public void displayPrefix(int p) {

        if (p == 1) {

            System.out.println("Zipcode Prefix: "+fiveDigit.substring(0,3));

        } else if(p == 2) {

            System.out.println("Zipcode area: "+fiveDigit.substring(4,5));

        }

        else{

            System.out.println(" ");

        }

    }

}

App.java:

/**

*

* App class

*/

public class App {

    public static void main(String[] args) {

        // SoccerPlayer object sp0 using the no-parameter constructor

        SoccerPlayer sp0 = new SoccerPlayer();

        // SoccerPlayer object sp1 using the all-parameter constructor with the value

        SoccerPlayer sp1 = new SoccerPlayer("Julia Dohle",

                new Address(10, "Old Main", "Street",

                        new ZipCode("16802", "0001"), "PA"),

                7, "Soccer", 10, 5, 1);

        // FootballPlayer object fp0 using the no-parameter constructor

        FootBallPlayer fb0 = new FootBallPlayer();

        // FootballPlayer object fp1 using the all-parameter constructor with the value

        FootBallPlayer fb1 = new FootBallPlayer("Saquon Barkley",

                new Address(10, "Old Main", "Street",

                        new ZipCode("16802", "0001"), "PA"),

                26, "Football", 10, 80, 220);

        // toString() to display the results

        System.out.println(sp0.toString());

        System.out.println("-----------------------------------");

        System.out.println(sp1.toString());

        System.out.println("-----------------------------------");

        System.out.println(fb0.toString());

        System.out.println("-----------------------------------");

        System.out.println(fb1.toString());

    }

}

SAMPLE OUTPUT:

******************************************************************************

Feel free to rate the answer and comment your questions, if you have any.

Please upvote the answer and appreciate our time.

Happy Studying!!!

******************************************************************************


Related Solutions

public class Person { private String name; public Person() { name = "No name yet"; }...
public class Person { private String name; public Person() { name = "No name yet"; } public Person(String initialName) { name = initialName; } public void setName(String newName) { name = newName; } public String getName() { return name; } public void writeOutput() { System.out.println("Name: " + name); } public boolean hasSameName(Person otherPerson) { return this.name.equalsIgnoreCase(otherPerson.name); } } 2- Write a Program Patient. Java Class that extends Person to include  Social security Gender  Appropriate construtors, accessors, and mutators....
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
Code a simple Editor class with 2 constructors (one default, one with a string for a...
Code a simple Editor class with 2 constructors (one default, one with a string for a file name) The Editor class in private has an instance of a LinkedList (of strings) and the position of the user (use our Point class) create a short file - at least 5 lines - could be a program main: string fileName("input.txt"); Editor miniVi (fileName); miniVi.displayLines(); C++ explain in detail plz Thank you
Create an ApartmentException class whose constructor receives a string that holds a street address, an apartment...
Create an ApartmentException class whose constructor receives a string that holds a street address, an apartment number, a number of bedrooms, and a rent value for an apartment. Upon construction, throw an ApartmentException if any of the following occur: • The apartment number does not consist of 3 digits • The number of bedrooms is less than 1 or more than 4 • The rent is less than $500.00 or over $2500 Write a driver class that demonstrates creating valid...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of the account holder. a method, getBalance, that returns a double corresponding to the account balance. a method withdraw that accepts a double, and deducts the amount from the account balance. Write a class definition for a subclass, CheckingAccount, that contains: a boolean instance variable, overdraft. (Having overdraft for a checking account allows one to write checks larger than the current balance). a constructor that...
Project 1 Fractional Number Class Part-1 for Five groups of member functions: Constructors (inc. string constructor)...
Project 1 Fractional Number Class Part-1 for Five groups of member functions: Constructors (inc. string constructor) Getter/setters Math Type cast Friend << and >> Part-2 for the explanation for the behavior observed by running the provided test pattern: six (6) operations in one statement vs. six (6) operations in six (6) statements. PROJECT 1 Frac Class The following information, located on Github m03, are provided as a starter. Frac.h, a complete Frac Class Declaration ( the definition is to be...
Define the exception class called TornadoException. The class should have two constructors including one default constructor....
Define the exception class called TornadoException. The class should have two constructors including one default constructor. If the exception is thrown with the default constructor, the method getMessage should return "Tornado: Take cover immediately!" The other constructor has a single parameter, m, of int type. If the exception is thrown with this constructor, the getMessage should return "Tornado: m miles away; and approaching!" Write a Java program to test the class TornadoException.
Consider the following class: class Person {         String name;         int age;        ...
Consider the following class: class Person {         String name;         int age;         Person(String name, int age){                this.name = name;                this.age = age;         } } Write a java program with two classes “Teacher” and “Student” that inherit the above class “Person”. Each class has three components: extra variable, constructor, and a method to print the student or the teacher info. The output may look like the following (Hint: you may need to use “super”...
Write a class named Person with data attributes for a person’s first name, last name, and...
Write a class named Person with data attributes for a person’s first name, last name, and telephone number. Next, write a class named Customer that is a subclass of the Person class. The Customer class should have a data attribute for a customer number and a Boolean data attribute indicating whether the customer wishes to be on a calling list. Demonstrate an instance of the Customer class in a simple program. Using python
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT