I need help to finish my java code assignment below. I have most of it set up, I
ID: 3563372 • Letter: I
Question
I need help to finish my java code assignment below. I have most of it set up, I just need help to finalize it, can you please help me with this.
I have 4 classes (PersonApp (main method), Person, BoyScout, and Address). Address is finalized ---please do not change anything in it!
Please see my assignment below, and then my code that I need help with to finish --per these instructions:
Package person:
Inside package inheritancePolymorphism there are four classes: Person, Address, BoyScout, and PersonApp.
A person has an Address and a BoyScount is a Person.
Class Address is already finished. Do not change it please.
It is your job to implement Person and BoyScout as described in the UML diagram.
Recommendation: Use the code generator from your IDE to help you be productive.
The toString method of class Person should do the following:
It should list the type of this instance followed by first name, last name and Address (see output)
BoyScount does NOT override the toString method.
However, it needs to implement a constructor to initialize the fields of the superclass and that initialized the field badges with an empty ArrayList
PersonApp should do the following:
Add 3 badges
Print the boy scout
Print the badges by accessing the field
Use a foreach loop to print all persons in the list and in case the person happens to be a boy scout print all the badges in the same line as well
PLEASE HELP ME TO FINISH THIS CODE --- SEE BELOW:
public class PersonApp {
public static void main(String[] args)
{
Person myPerson = new Person(null, null, null);
System.out.println(myPerson);
BoyScout myBoyScout = new BoyScout();
System.out.println(myBoyScout);
}
}
_______
public class Person
{
private String firstName;
private String lastName;
private Address address;
public Person(String fName, String lName, Address a)
{
this.firstName = fName;
this.lastName = lName;
this.address = a;
}
?
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
?
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
?
@Override
public String toString() {
return "Person [firstName=" + firstName + ", lastName=" + lastName
+ ", address=" + address + "]";
}
}
________
import java.util.ArrayList;
public class BoyScout
{
ArrayList<String> badges = new ArrayList<String>();
public void addBadge()
{
}
public void setBadges(ArrayList<String> badges) {
this.badges = badges;
}
public ArrayList<String> getBadges() {
return badges;
}
}
_______
THE ADDRESS CLASS SHOULD NOT BE CHANGED!! ONLY SHOWING FOR REFERENCE:
public class Address {
private final String street;
private final int zip;
private final String city;
private final String state;
public Address(String street, int zip, String city, String state) {
this.street = street;
this.zip = zip;
this.city = city;
this.state = state;
}
public String getStreet() {
return street;
}
public int getZip() {
return zip;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
@Override
public String toString() {
return street + " " + city + ", " + state + " " + zip;
}
}
Explanation / Answer
import java.util.ArrayList;
public class PersonApp {
public static void main(String[] args) {
Person myPerson = new Person("Jack","Marks", new Address("Teak road", 33498, "Stelore", "FL"));
System.out.println(myPerson);
myPerson.setAddress(new Address(myPerson.getAddress().getStreet(),33179,myPerson.getAddress().getCity(),myPerson.getAddress().getState()));
System.out.println(myPerson);
BoyScout myBoyScout = new BoyScout("John","Strew",new Address("Elm Street",32991,"Cantorie","CA"));
myBoyScout.addBadge("badge1");
myBoyScout.addBadge("badge2");
myBoyScout.addBadge("badge3");
for(String badge: myBoyScout.getBadges())
{
System.out.println("Badge: "+ badge);
}
ArrayList<Person> personlist= new ArrayList<Person>();
personlist.add(myPerson);
personlist.add(myBoyScout);
for(Person p: personlist)
{
System.out.println("Person Details: "+ p.toString());
if( p instanceof BoyScout)
{
System.out.println("Badges for "+ p.getFirstName());
for(String badge: ((BoyScout) p).getBadges())
System.out.println(badge);
}
}
//System.out.println(myBoyScout);
}
}
public static class Person {
private String firstName;
private String lastName;
private Address address;
public Person(String fName, String lName, Address a) {
this.firstName = fName;
this.lastName = lName;
this.address = a;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Person [firstName=" + firstName + ", lastName=" + lastName
+ ", address=" + address + "]";
}
}
public class BoyScout extends Person {
ArrayList<String> badges = new ArrayList<String>();
boolean isboyscout=false;
public void addBadge(String badge) {
badges.add(badge);
}
public BoyScout(String fname,String lname,Address a)
{
super(fname,lname,a);
isboyscout=true;
}
public void setBadges(ArrayList<String> badges) {
this.badges = badges;
}
public boolean isBoyScout()
{
return isboyscout;
}
public ArrayList<String> getBadges() {
return badges;
}
}
//_______
//THE ADDRESS CLASS SHOULD NOT BE CHANGED!! ONLY SHOWING FOR REFERENCE:
public class Address {
private final String street;
private final int zip;
private final String city;
private final String state;
public Address(String street, int zip, String city, String state) {
this.street = street;
this.zip = zip;
this.city = city;
this.state = state;
}
public String getStreet() {
return street;
}
public int getZip() {
return zip;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
@Override
public String toString() {
return street + " " + city + ", " + state + " " + zip;
}
}
OUTPUT:
Person [firstName=Jack, lastName=Marks, address=Teak road Stelore, FL 33498]
Person [firstName=Jack, lastName=Marks, address=Teak road Stelore, FL 33179]
Badge: badge1
Badge: badge2
Badge: badge3
Person Details: Person [firstName=Jack, lastName=Marks, address=Teak road Stelore, FL 33179]
Person Details: Person [firstName=John, lastName=Strew, address=Elm Street Cantorie, CA 32991]
Badges for John
badge1
badge2
badge3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.