Respuesta :
Answer:
Here is the Person class:
class Person { //class name
//below are the private data members/fields of Person class
private String name; //to hold name
private String address; //to hold address
private String phoneNo; // contains telephone number
public Person(String name, String address, String phoneNo){ //constructor of Person class with name, address and phoneNo as parameters
this.name = name;
this.address = address;
this.phoneNo = phoneNo; }
public String getName(){ //accessor method to get/access the name
return this.name; } //returns the name
public String getAddress(){ //accessor method to get/access the address
return this.address; } //returns the address
public String getPhoneNo(){ //accessor method to get/access the phone number
return this.phoneNo; } //returns the telephone number
//mutator methods
public void setName(String nam){ //mutator method to set the name
this.name = nam; }
public void setAddress(String addr){ //mutator method to set the address
this.address = addr; }
public void setPhoneNo(String phNo){ //mutator method to set the telephone number
this.phoneNo = phNo; } }
Here is the Customer class which extends the Person class:
class Customer extends Person{ //Customer class that is sub class of Person class
//below are the private data members/fields of Customer class
private String CustomerNo; //String type field to hold customer number
private boolean MailingList; //boolean field indicating whether the customer wishes to be on a mailing list
public Customer(String name, String address, String phoneNo, String custNum, boolean mail) { //constructor of Customer class that has parameters name, address and phoneNo (of Person class) and parameters custNum and mail ( of Customer class)
super(name, address, phoneNo); // super keyword is used to refer to Person objects and to access the Person class constructor
this.CustomerNo = custNum;
this.MailingList = mail; }
//accessor methods
public String getCustomerNo(){ //accessor method to get/access the customer number
return this.CustomerNo; }
public boolean getMailingList(){ //accessor method to get/access the boolean mailing list to check whether a person wants to be on mailing list
return this.MailingList; }
//mutator methods
public void setCustomerNo(String custNo){ //mutator method to set the customer number
this.CustomerNo = custNo; }
public void setMailingList(boolean mailing){ //mutator method to set the mailing list
this.MailingList = mailing; } }
Explanation:
Here is the demonstration class to demonstrate object of Customer class, calls Person and Customer class methods to display the Customer information such as name, address, phone number, customer number and if customer wants to receive mail.
import java.util.Scanner; //used to take input from user
class Demonstration{ //test class
public static void main(String args[]){ //start of main() function
Scanner input= new Scanner(System.in); //creates object of Scanner class
System.out.print("Enter Customer Name: "); //prompts user to enter name
String fname = input.nextLine();
System.out.print("Enter Customer Address: "); //prompts user to enter address
String address = input.nextLine();
System.out.print("Enter Customer Telephone Number: "); //prompts user to enter telephone number
String phoneNumber = input.nextLine();
System.out.print("Enter Customer Number: "); //prompts user to enter customer number
String customerNum = input.nextLine();
System.out.print("Does Customer wish to be on a mailing list? \nPress 1 for yes, 0 for no: "); //asks user if he wishes to receive mail
String choice = input.nextLine();
boolean mailList = (choice.equals("1")); //if user enters 1 indicating that he wishes to be on mailing list
Customer customer = new Customer(fname, address, phoneNumber, customerNum, mailList); //creates object of Customer class and calls Customer class constructor
System.out.println("\nCustomer Information: ");
System.out.println("Name: "+customer.getName()); //calls getName method of Person class using customer object to get the name and displays the name
System.out.println("Address: "+customer.getAddress()); //calls getAddress method of Person class using customer object to get the name and prints the address
System.out.println("Telephone Number: "+customer.getPhoneNo()); //calls getPhoneNo method of Person class using customer object to get the telephone number and prints phone number
System.out.println("Customer Number: "+customer.getCustomerNo()); //calls getCustomerNo method of Customer class using customer object to get the customer number and prints the customer number
System.out.println("Mailing List?: "+customer.getMailingList()); }} //calls getMailingList method of Customer class using customer object to get the mailing list and prints true of false on the basis of customers choice to be on mailing list.



