Question

In: Computer Science

Assignment Examine the Main and Address classes. You are going to add two classes derived from...

Assignment

Examine the Main and Address classes. You are going to add two classes derived from Address: BusinessAddress and PersonAddress.

Create BusinessAddress class

  1. Select package home and create a new Java class called BusinessAddress
  2. Make the class extend the Address class
  3. Add two private String fields businessName and address2
  4. Generate constructor and all getters and setters
  5. Add a printLabel() method

The printLabel method should print (using System.out.println())

First line – the businessName field

Second line – the address2 field if it is not null or empty

Third line – the StreetAddress field if it is not null or empty

Fourth line – city field followed by a comma and space, the state field followed by two spaces, and the zip field

Create PersonAddress class

  1. Select package home and create a new Java class called PersonAddress
  2. Make the class extend the Address class
  3. Add a private String field personName
  4. Generate constructor and all getters and setters
  5. Add a printLabel() method

The printLabel method should print (using System.out.println())

First line – the personName field

Second line – the StreetAddress field

Third line – city field followed by a comma and space, the state field followed by two spaces, and the zip field

Modify Main class

Add the following three BusinessAddress objects to the list.

BusinessName

Address2

StreetAddress

City

State

Zip

Columbus State

Eibling 302B

550 East Spring St.

Columbus

OH

43215

AEP

P.O. Box 2075

null

Columbus

OH

43201

Bill’s Coffee

null

2079 N. Main St.

Columbus

OH

43227

Add the following three PersonAddress objects to the list.

PersonName

StreetAddress

City

State

Zip

Saul Goodman

1200 N. Fourth St.

Worthington

OH

43217

Mike Ehrmentraut

207 Main St.

Reynoldsburg

OH

43211

Gustavo Fring

2091 Elm St.

Pickerington

OH

43191

Example Output

Columbus State

Eibling 302B

550 East Spring St.

Columbus, OH 43215

====================

AEP

P.O. Box 2075

Columbus, OH 43201

====================

Bill's Coffee

2079 N. Main St.

Columbus, OH 43227

====================

Saul Goodman

1200 N. Fourth St.

Worthington, OH 43217

====================

Mike Ehrmentraut

207 Main St.

Reynoldsburg, OH 43211

====================

Gustavo Fring

2091 Elm St.

Pickerington, OH 43191

====================

My code

package home;

public class Main {

    public static void main(String[] args) {
       Address[] addressList = new Address[6];

       // TODO Add 3 person addresses to list
        addressList[3] = new PersonAddress("1200 N. Fourth St.","Worthington","OH","43217","Saul Goodman");
        addressList[4] = new PersonAddress("207 Main St.","Reynoldsburg","OH","43217","Mike Ehrmentraut");
        addressList[5] = new PersonAddress("2091 Elm St.","Pickerington","OH","43191","Gustavo Fring");

        // TODO Add 3 business address to list
        addressList[0] = new BusinessAddress("550 East Spring St.","Columbus","OH","43215","Columbus State","Eibling 302B");
        addressList[1] = new BusinessAddress(null,"Columbus","OH","43201","AEP","P.O. Box 2075");
        addressList[2] = new BusinessAddress("2079 N. Main St.","Columbus","OH","43227","Bill’s Coffee",null);

       for (Address address : addressList) {
           address.printLabel();
            System.out.println("====================");
        }
    }
}
package home;

public class BusinessAddress extends Address {
    // two private String fields businessName and address2
    private String businessName;
    private String address2;
    //Constructor

    public BusinessAddress(String streetAddress, String city, String state, String zip, String businessName, String address2) {
        super(streetAddress, city, state, zip);
        this.businessName = businessName;
        this.address2 = address2;
    }

    //getters and setters

    public String getBusinessName() {
        return businessName;
    }

    public void setBusinessName(String businessName) {
        this.businessName = businessName;
    }

    public String getAddress2() {
        return address2;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    @Override
    public void printLabel() {
        String result ="";
        if(address2==null)
            result = businessName+"\n"+super.toString();
        else
            result = businessName+"\n"+address2+"\n"+super.toString();
        System.out.println(result);
    }
}

}
package home;

public class PersonAddress extends Address {
    private String  personName;

    //Constructor

    public PersonAddress(String streetAddress, String city, String state, String zip, String personName) {
        super(streetAddress, city, state, zip);
        this.personName = personName;
    }

    //getter and setter

    public String getPersonName() {
        return personName;
    }

    public void setPersonName(String personName) {
        this.personName = personName;
    }

    @Override
    public void printLabel() {
        System.out.println(personName+"\n"+super.toString());
    }
}
package home;

public abstract class Address {
    private String streetAddress;
    private String city;
    private String state;
    private String zip;

    public Address(String streetAddress, String city, String state, String zip) {
        this.streetAddress = streetAddress;
        this.city = city;
        this.state = state;
        this.zip = zip;
    }

    public String getStreetAddress() {
        return streetAddress;
    }

    public void setStreetAddress(String streetAddress) {
        this.streetAddress = streetAddress;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public String toString() {
        return streetAddress + "\n" +
                city + ", " + state + "  " + zip + "\n";
    }

    public abstract void printLabel();
}

Solutions

Expert Solution

Address.java :

package home;

public abstract class Address {
private String streetAddress;
private String city;
private String state;
private String zip;

public Address(String streetAddress, String city, String state, String zip) {
this.streetAddress = streetAddress;
this.city = city;
this.state = state;
this.zip = zip;
}

public String getStreetAddress() {
return streetAddress;
}

public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getZip() {
return zip;
}

public void setZip(String zip) {
this.zip = zip;
}

public String toString() {
return streetAddress + "\n" +
city + ", " + state + " " + zip + "\n";
}

public abstract void printLabel();
}
*****************************

PersonAddress.java :

package home;
//Java class
public class PersonAddress extends Address {
private String personName;

//Constructor

public PersonAddress(String streetAddress, String city, String state, String zip, String personName) {
super(streetAddress, city, state, zip);
this.personName = personName;
}

//getter and setter

public String getPersonName() {
return personName;
}

public void setPersonName(String personName) {
this.personName = personName;
}

@Override
public void printLabel() {
System.out.println(personName+"\n"+super.toString());
}
}

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

BusinessAddress.java :

package home;
//Java class
public class BusinessAddress extends Address {
// two private String fields businessName and address2
private String businessName;
private String address2;
//Constructor

public BusinessAddress(String streetAddress, String city, String state, String zip, String businessName, String address2) {
super(streetAddress, city, state, zip);
this.businessName = businessName;
this.address2 = address2;
}

//getters and setters

public String getBusinessName() {
return businessName;
}

public void setBusinessName(String businessName) {
this.businessName = businessName;
}

public String getAddress2() {
return address2;
}

public void setAddress2(String address2) {
this.address2 = address2;
}

@Override
public void printLabel() {
String result ="";
if(address2==null && super.getStreetAddress()==null)
   //if address2 and StreetAddres null then
result = businessName+"\n" +
super.getCity() + ", " + super.getState() + " " + super.getZip() + "\n";
else if(address2==null && super.getStreetAddress()!=null) {
   //only if address2 is null and StreetAddres is not null then
   result = businessName+"\n"+super.toString();
}
else if(address2!=null && super.getStreetAddress()==null)
{
   //only if StreetAddres is null and address2 not null then
   result = businessName+"\n"+address2+"\n"+super.getCity() + ", " + super.getState() + " " + super.getZip() + "\n";
}
else {
result = businessName+"\n"+address2+"\n"+super.toString();
}
System.out.println(result);
}
}
***********************************

Main.java :

package home;
//Java class
public class Main {
   //entry point main method
public static void main(String[] args) {
Address[] addressList = new Address[6];

// TODO Add 3 person addresses to list
addressList[3] = new PersonAddress("1200 N. Fourth St.","Worthington","OH","43217","Saul Goodman");
addressList[4] = new PersonAddress("207 Main St.","Reynoldsburg","OH","43217","Mike Ehrmentraut");
addressList[5] = new PersonAddress("2091 Elm St.","Pickerington","OH","43191","Gustavo Fring");

// TODO Add 3 business address to list
addressList[0] = new BusinessAddress("550 East Spring St.","Columbus","OH","43215","Columbus State","Eibling 302B");
addressList[1] = new BusinessAddress(null,"Columbus","OH","43201","AEP","P.O. Box 2075");
addressList[2] = new BusinessAddress("2079 N. Main St.","Columbus","OH","43227","Bill’s Coffee",null);

for (Address address : addressList) {
address.printLabel();
System.out.println("====================");
}
}
}

=========================

Output 1 :


Related Solutions

You will write classes from the last in-class assignment, R&DLibrary. R&DLibrary is the main file in...
You will write classes from the last in-class assignment, R&DLibrary. R&DLibrary is the main file in which a user can choose to enroll themselves, check out an item, return an item, pay a fine and display their account (what is currently checked out, accumulated fines). Write a material class that calculates the fine based off of the type of item, the length it’s been checked out and whether or not it’s even been checked out. Lastly, write a employee Class...
C++ Assignment 1: Make two classes practice For each of the two classes you will create...
C++ Assignment 1: Make two classes practice For each of the two classes you will create for this assignment, create an *.h and *.cpp file that contains the class definition and the member functions. You will also have a main file 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them. Class #1 Ideas for class one are WebSite, Shoes, Beer, Book, Song, Movie, TVShow, Computer, Bike, VideoGame, Car, etc Take your chosen class from...
You are to use your code from part A of this assignment and add to it....
You are to use your code from part A of this assignment and add to it. In this part of the project, you are to do the following: 1. In the count_voters function you are to add code to: • Count the number of votes for Trump • Count the number of votes for Biden • Count the number of females • Count the number of males • Count the number of Democrats • Count the number of Republicans •...
LAUNGEG: C# Objective: For this assignment you will be creating two classes, an interface, and a...
LAUNGEG: C# Objective: For this assignment you will be creating two classes, an interface, and a driver program: Class Calculator will implement the interface CalcOps o As such it will implement hexToDec() - a method to convert from Hexadecimal to Decimal. Class HexCalc will inherit from Calculator. Interface CalcOps will have abstract methods for add( ), subtract( ), multiply( ) and divide( ). Both classes will implement the CalcOps interface. o Class Calculator’s add, subtract, multiply and divide will take...
*JAVA* For this assignment you have been given two classes, a Main.java and a Coin.java. The...
*JAVA* For this assignment you have been given two classes, a Main.java and a Coin.java. The coin class represents a coin. Any object made from it will have a 1) name, 2) weight and 3) value. As of now, the instance variables in Coin.java are all public, and the main function is calling these variables directly for the one coin made in it. Your goal is to enforce information hiding principles in this project. Take Coin.java, make all instance variables...
In this assignment you are going to use the menu you created in Assignment 1 to...
In this assignment you are going to use the menu you created in Assignment 1 to test both your Double and Integer classes. You should add functionality to the menu to allow you test the add, sub, mul, div functions for instances of both classes You are going to have make a modification to your menu class. Currently it uses an array to hold a fixed amount of menu items. While this may be OK for most applications we want...
Assignment For this discussion, you are asked to examine the structure of DNA from your textbook....
Assignment For this discussion, you are asked to examine the structure of DNA from your textbook. Using your knowledge or the material covered in Weeks 1 – 12, your general knowledge of organic chemistry I, your textbooks and the Internet to answer the following questions regarding the DNA molecule. Include the structure of DNA in your discussion. Label and give the name for the the major functional groups in the DNA molecule? Please be sure to include all functional groups...
Requirements: In this assignment, you are going to create two switch functions. The first function is...
Requirements: In this assignment, you are going to create two switch functions. The first function is going to be called switchVal and the second is going to be called switchRef. The goal of this assignment is to demonstrate the understanding of what pass-by-reference (Links to an external site.) and pass-by-value (Links to an external site.) do when working with functions that you create. The two functions that you will modify need to accept two parameters and inside them they need...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed ) 1. Add New private String name (customer name) for both, add a New int taxID for Savings only. 2. Add equals() METHOD TO CHECK any 2 accounts Demonstrate with AccountDemo.java in which you do the following: 3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same....
Refactor the following classes so they are both derived from a base class called Person. Write...
Refactor the following classes so they are both derived from a base class called Person. Write the code for the new base class as well. Try to avoid repeating code as much as possible. Write the classes so that any common methods can be invoked through a pointer or reference to the base class. #include <string> #include <cmath> using namespace std; class Student { private: string name;    int age;    int studyYear; public:    Student(string,int,int); void study();    void...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT