Question: Person and Customer Class Design a class named person with fields for
ID: 3617104 • Letter: Q
Question
Question: Person and Customer Class
Design a class named person with fields for holding aperson’s name, address, and telephone number. Write on ormore constructors and the appropriate mutator and accessor methodsfor the class’s fields.
Next, design a class named Customer, which inherits from theperson class. The Customer class should have a field for a customernumber and a boolean field indicating whether the customer wishingto be on a mailing list. Write one or more constructors and theappropriate mutator and accessor methods for the class’sfields. Demonstrate an object of the Customer class in a simpleprogram.
Explanation / Answer
publicclass Person { // instancefields privateString name; privateString address; privateint telephone; //constructors publicPerson(String name, String address, inttelephone) { setName(name); setAddress(address); setTelephone(telephone); } // accessors publicString getName() { return name; } publicString getAddress() { return address; } publicint getTelephone() { return telephone; } // modifiers publicvoid setName(String n) { name = n; } publicvoid setAddress(String a) { address = a; } publicvoid setTelephone(int t) { telephone = t; } } public class Customerextends Person { // instancefields privateint number; privateboolean mailingList; //constructors publicCustomer(String name, String address, inttelephone, int number, boolean mailingList) { super(name, address, telephone); setNumber(number); setMailingList(mailingList); } // accessors publicint getNumber() { return number; } publicboolean getMailingList() { return mailingList; } // modifiers publicvoid setNumber(int n) { number = n; } publicvoid setMailingList(boolean m) { mailingList = m; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.