179k views
3 votes
Design a class named Person with fields for holding a person's name , address, and telephone number (all as Strings). Write a constructor that initializes all of these values , and mutator and accessor methods for every field. Next, design a class named Customer, which inherits from the Person class . The Customer class should have a String field for the customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write a constructor that initializes these values and the appropriate mutator and accessor methods for the class's fields.

User Robin
by
7.3k points

1 Answer

1 vote

Final answer:

The question involves creating a Person class with fields for a person's details and a Customer class that inherits from Person, adding extra fields and methods.

Step-by-step explanation:

To answer your question, you would start by designing a Person class with private fields for the name, address, and telephone number, all of which are strings. A constructor would be used to initialize these fields, and you would provide accessor (getter) and mutator (setter) methods for each. Here is an example in 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;
}

// Accessor and Mutator Methods
public String getName() { return name; }
public void setName(String name) { this.name = name; }
// ... Additional getters and setters for other fields
}

The Customer class would extend the Person class and include additional fields: a customer number and a boolean for the mailing list preference. Similarly, it would have a constructor, accessors, and mutators:

public class Customer extends Person {
private String customerNumber;
private boolean wantsMailingList;

public Customer(String name, String address, String phoneNumber,
String customerNumber, boolean wantsMailingList) {
super(name, address, phoneNumber);
this.customerNumber = customerNumber;
this.wantsMailingList = wantsMailingList;
}

// Accessor and Mutator Methods for new fields
// ...
}
User Manosim
by
8.1k points