36.0k views
4 votes
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.

User SBad
by
6.6k points

1 Answer

6 votes

Answer:

class Person{

String name;

String address;

int phoneNumber;

public Person(){}

public Person(String name, String address, int phoneNumber){

this.name = name;

this.address = address;

this.phoneNumber = phoneNumber;

}

public String getName(){

return name;

}

public String getAddress(){

return address;

}

public int getPhoneNumber(){

return phoneNumber;

}

public void setName(String name){

this.name = name;

}

public void setAddress(String address){

this.address = address;

}

public void setPhoneNumber(int phoneNumber){

this.phoneNumber = phoneNumber;

}

}

class Customer extends Person{

int customerNumber;

boolean addToMailingList;

Customer(String name, String address, int phoneNumber, int customerNumber, boolean addToMailingList){

super(name, address, phoneNumber);

this.customerNumber = customerNumber;

this.addToMailingList = addToMailingList;

}

@Override

public String toString(){

return "\\\\The customer name is: " + name + ".\t The customer address is: " + address + ".\t The phone number is also: " + phoneNumber + ".\\The customer number is: " + customerNumber + ".\tThe option to add to mailing list is: " + addToMailingList;

}

}

public class Main{

public static void main(String[] args){

Customer customer1 = new Customer("John Doe", "Silicon Valley", 12345, 1100, true);

Customer customer2 = new Customer("James Smith", "Epic Towers", 56788, 1100, false);

Customer customer3 = new Customer("Grace Hooper", "Naval base", 11223, 1110, true);

System.out.println(customer1);

System.out.println(customer2);

System.out.println(customer3);

}

}

Step-by-step explanation:

First, the Person class was defined with the name, address and telephone number field. The Person class also contain constructor for the class.

Next, a customer class was created that extended the Person class. The customer class also has new field of customerNumber and 'addToMailingList'. the toString method was also override in the customer class so as to create a readable output.

The Main class was then created where three instance of the Customer class was created and printed.

User Alexwenzel
by
6.7k points