In: Computer Science
(Java)
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.
import java.util.Scanner;
class Person {
private String name;
private String address;
private String phoneNumber;
public Person() {
}
public Person(String aName, String aAddress, String
aPhoneNumber) {
super();
name = aName;
address = aAddress;
phoneNumber = aPhoneNumber;
}
public String getName() {
return name;
}
public void setName(String aName) {
name = aName;
}
public String getAddress() {
return address;
}
public void setAddress(String aAddress) {
address = aAddress;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String aPhoneNumber)
{
phoneNumber = aPhoneNumber;
}
@Override
public String toString() {
return "name : " + name + ",
address : " + address + ", phoneNumber : " + phoneNumber;
}
}
class Customer extends Person {
private String customerNumber;
private boolean isInMailingList;
public Customer(String aName, String aAddress,
String aPhoneNumber, String aCustomerNumber,
boolean
aIsInMailingList) {
super(aName, aAddress,
aPhoneNumber);
customerNumber =
aCustomerNumber;
isInMailingList =
aIsInMailingList;
}
public String getCustomerNumber() {
return customerNumber;
}
public void setCustomerNumber(String
aCustomerNumber) {
customerNumber =
aCustomerNumber;
}
public boolean isInMailingList() {
return isInMailingList;
}
public void setInMailingList(boolean
aIsInMailingList) {
isInMailingList =
aIsInMailingList;
}
@Override
public String toString() {
return super.toString() + "
CustomerNumber : " + customerNumber + ", customer wishes to be on a
mailing list : "
+ isInMailingList;
}
}
public class TestPerson {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter customer
name");
String name = sc.nextLine();
System.out.println("Enter customer
address");
String address =
sc.nextLine();
System.out.println("Enter customer
Phone ");
String phoneNumber =
sc.nextLine();
System.out.println("Enter customer
number");
String customerNumber =
sc.nextLine();
System.out.println("do you wishe to
be on a mailing list? ");
boolean s = sc.nextBoolean();
Customer c = new Customer(name,
address, phoneNumber, customerNumber, s);
System.out.println(c);
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me