Question: Java: Create a class called PersonAddress that includes fields for street Address, City, State, and zipcode. Add a private data item to the Person class
Java: Create a class called PersonAddress that includes fields for street Address, City, State, and zipcode. Add a private data item to the Person class (defined above) that is of type PersonAddress as well as get methods to retrieve data from this object. (This is an example of aggregation). Instantiate an object using the OlympicAthlete class and pass in all data using the constructor including the address data. Display all data.
person class is based off this
abstract class Person { //private fields private String lastname; private String firstname; private int age;
//methods for retrieving
public String getLastname() { return lastname; }
public String getFirstname() { return firstname; }
public int getAge() { return age; }
//constructor
public Person(String lastname, String firstname, int age) { this.lastname = lastname; this.firstname = firstname; this.age = age; } abstract void displayInformation(); }
public class OlympicAthlete extends Person{
private int numberOfGoldMedals;
public OlympicAthlete(String lastname, String firstname, int age, int numberOfGoldMedals) { super(lastname, firstname, age); this.numberOfGoldMedals = numberOfGoldMedals; }
public int getNumberOfGoldMedals() { return numberOfGoldMedals; } void displayInformation(){ System.out.println("First name:"+getFirstname()); System.out.println("Last name:"+getLastname()); System.out.println("Age:"+getAge()); System.out.println("Number of Gold Medals:"+getNumberOfGoldMedals()); }
//creating objects public static void main(String[] args) { OlympicAthlete o1=new OlympicAthlete("Doe","John",26,4); //display o1.displayInformation(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
