***JAVA Program*** Design a class named Person with fields for holding a person\
ID: 3790615 • Letter: #
Question
***JAVA Program***
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.
Demonstrate the Customer class in a program that prompts the user to enter values
for the customer's name , address, phone number, and customer number, and then
asks the user whether or not the customer wants to recieve mail. Use this information
to create a customer object and then print its information.
Put all of your classes in the same file. To do this, do not declare them public.
Instead, simply write:
class Person { ... }
class Customer { ... }
SAMPLE RUN #1: java Driver
****Expected Results****
Enter·name·of·customer:Enter·address·of·customer:Enter·phone·number·of·customer:Enter·customer·number:Enter·yes/no·--·does·the·customer·want·to·recieve·mail?:
Customer:·
Name:·Julia·Stevens
Address:·77·Massachusetts·Ave·Cambridge,·MA·02139
Phone·Number:·617-777-7777
Customer·Number:·928734502
Recieve·Mail?:·false
Explanation / Answer
//*******************************************************************
// NOTE: please read the 'More Info' tab to the right for shortcuts.
//*******************************************************************
import java.util.Scanner;
class Person{
String name;
String address;
String telephoneNo;
Person(String name, String address, String telephoneno)
{
this.name = name;
this.address = address;
this.telephoneNo = telephoneno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTelephoneNo() {
return telephoneNo;
}
public void setTelephoneNo(String telephoneNo) {
this.telephoneNo = telephoneNo;
}
}
class Customer extends Person{
int CustNo=233442;
boolean receiveMails;
public int getCustNo() {
return CustNo;
}
public void generateCustNo() {
CustNo = CustNo+1;
}
public boolean isReceiveMails() {
return receiveMails;
}
public void setReceiveMails(boolean receiveMails) {
this.receiveMails = receiveMails;
}
Customer(String name, String address, String telephoneno,boolean receivemail){
super(name,address ,telephoneno );
this.receiveMails = receivemail;
}
void printCustomerInfo(){
System.out.println("Customer Id: "+getCustNo());
System.out.println("Customer Name: "+getName());
System.out.println("Customer Address: "+getAddress());
System.out.println("Customer Telephone No: "+getTelephoneNo());
System.out.println("Want to receive mail : "+isReceiveMails());
}
}
public class PrintCustomerInfo{
public static void main(String args[]){
boolean wanttoreceivemail = false;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
String name = sc.nextLine();
System.out.println("Enter your address");
String address = sc.nextLine();
System.out.println("Enter your telephoneno");
String telephoneno = sc.nextLine();
System.out.println("does the customer wants to receive mail (Y/N)");
String mail = sc.nextLine();
if("Y".equals(mail) || "y".equals(mail))
wanttoreceivemail = true;
Customer cust = new Customer(name ,address,telephoneno,wanttoreceivemail);
cust.generateCustNo();
cust.printCustomerInfo();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.