Design a class named Person with fields for holding a person\'s name, address, a
ID: 3555587 • Letter: D
Question
Design a class named Person with fields for holding a person's name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class's fields.
Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator and accessor methods for the class's fields. Demonstrate an object of the Customer class in a simple program.
(Instructor request) For the demo, write a main that creates 3 Customers and prints out their information. Make up some names, addresses, phone numbers, etc. so no user input is required.
Explanation / Answer
// Person.java
public class Person {
private String name;
private String address;
private String phoneNumber;
public Person(String name, String address, String phoneNumber) {
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
// Customer.java
public class Customer extends Person {
private int customerNumber;
private boolean onList;
public Customer(String name, String address, String phoneNum, int number, boolean onList) {
super(name, address, phoneNum);
this.customerNumber = number;
this.onList = onList;
}
public int getCustomerNumber() {
return customerNumber;
}
public void setCustomerNumber(int customerNumber) {
this.customerNumber = customerNumber;
}
public boolean isOnList() {
return onList;
}
public void setOnList(boolean onList) {
this.onList = onList;
}
}
// Driver.java
public class Driver {
public static void main(String[] args) {
Customer customer1 = new Customer("Judy Smith", "123 Maple Lane", "555-305-9876", 1, true);
Customer customer2 = new Customer("Bob White", "456 Oak Road", "555-895-4931", 2, true);
Customer customer3 = new Customer("Garry Goodman", "789 Acorn Avenue", "555-587-2983", 3, false);
System.out.println("Customer 1:");
System.out.println(" Name: " + customer1.getName());
System.out.println(" Address: " + customer1.getAddress());
System.out.println(" Phone Number: " + customer1.getPhoneNumber());
System.out.println(" Customer Number: " + customer1.getCustomerNumber());
System.out.println(" On Mailing List: " + customer1.isOnList());
System.out.println("Customer 2:");
System.out.println(" Name: " + customer2.getName());
System.out.println(" Address: " + customer2.getAddress());
System.out.println(" Phone Number: " + customer2.getPhoneNumber());
System.out.println(" Customer Number: " + customer2.getCustomerNumber());
System.out.println(" On Mailing List: " + customer2.isOnList());
System.out.println("Customer 3:");
System.out.println(" Name: " + customer3.getName());
System.out.println(" Address: " + customer3.getAddress());
System.out.println(" Phone Number: " + customer3.getPhoneNumber());
System.out.println(" Customer Number: " + customer3.getCustomerNumber());
System.out.println(" On Mailing List: " + customer3.isOnList());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.