Design a class named PersonData with the following member variables: lastName fi
ID: 3782300 • Letter: D
Question
Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData, which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool. It will be set to true if the customer wishes to be on a mailing list, or false if the customer does not wish to be on a mailing list. Write appropriate accessor and mutator functions for these member variables. Demonstrate an object of the CustomerData class in a simple program.Explanation / Answer
// PersonData.java
public class PersonData {
public PersonData(String lastName, String firstName, String address,
String city, String state, String zip, String phone) {
this.lastName = lastName;
this.firstName = firstName;
this.address = address;
this.city = city;
this.state = state;
this.zip = zip;
this.phone = phone;
}
private String lastName;
private String firstName;
private String address;
private String city;
private String state;
private String zip;
private String phone;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
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 getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
// CustomerData
public class CustomerData extends PersonData{
int customerNumber;
boolean mailingList;
public CustomerData(String lastName, String firstName, String address,
String city, String state, String zip, String phone,
int customerNumber, boolean mailingList) {
super(lastName, firstName, address, city, state, zip, phone);
this.customerNumber = customerNumber;
this.mailingList = mailingList;
}
public int getCustomerNumber() {
return customerNumber;
}
public void setCustomerNumber(int customerNumber) {
this.customerNumber = customerNumber;
}
public boolean isMailingList() {
return mailingList;
}
public void setMailingList(boolean mailingList) {
this.mailingList = mailingList;
}
}
// CustomerDataTest
public class CustomerDataTest {
public static void main(String[] args)
{
String lastName = "Brynes";
String firstName = "Billly";
String address = "My local address";
String city = "Fairborn";
String state = "Ohio";
String zip = "45324";
String phone = "8978789878";
int customerNumber = 1;
boolean mailingList = false;
CustomerData customerData = new CustomerData(lastName, firstName, address,
city, state, zip, phone,
customerNumber, mailingList);
System.out.println("Hello " + customerData.getFirstName() + " " + customerData.getLastName());
System.out.println("You are from " + customerData.getCity() + " " + customerData.getAddress() + " " + customerData.getZip());
if (customerData.isMailingList())
System.out.println("You have subscribed to weekly newsletter");
else
System.out.println("You have not subscribed to weekly newsletter");
}
}
/*
Sample run
Hello Billly Brynes
You are from Fairborn My local address 45324
You have not subscribed to weekly newsletter
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.