In: Computer Science
The Person Class
The Player Class (with updates from the last lab)
The SoccerPlayer Class
The FootballPlayer Class
The Address Class
The ZipCode Class (updated to include encapsulation)
The App class
SHORT SUMMARY:
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!!!
******************************************************************************