Java: Design a class named Person with fields for holding a person\'s name, addr
ID: 3857245 • Letter: J
Question
Java:
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 { ... }
Run like the following:
Enter·name·of·customer:Julia·Stevens
Enter·address·of·customer:77·Massachusetts·Ave·Cambridge,·MA·02139
Enter·phone·number·of·customer:617-777-7777
Enter·customer·number:928734502
Enter·yes/no·--·does·the·customer·want·to·recieve·mail?:no
Customer:·
Name:·Julia·Stevens
Address:·77·Massachusetts·Ave·Cambridge,·MA·02139
Phone·Number:·617-777-7777
Customer·Number:·928734502
Recieve·Mail?:·false
Explanation / Answer
Person class -----
class Person {
private String name;
private String address;
private String telephoneNo;
public Person(String name, String address, String telephoneNo) {
super();
this.name = name;
this.address = address;
this.telephoneNo = telephoneNo;
}
@Override
public String toString() {
return "[name=" + name + ", address=" + address + ", 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;
}
}
Customer class ---------------------------
public class Customer extends Person
{
private String customerNo;
private boolean custWish;
public Customer(String name, String address, String telephoneNo,String customerNo,boolean custWish) {
super(name, address, telephoneNo);
this.customerNo=customerNo;
this.custWish=custWish;
}
public String getCustomerNo() {
return customerNo;
}
@Override
public String toString() {
return "Customer [customerNo=" + customerNo + ", custWish=" + custWish + "]" + super.toString();
}
public void setCustomerNo(String customerNo) {
this.customerNo = customerNo;
}
public boolean isCustWish() {
return custWish;
}
public void setCustWish(boolean custWish) {
this.custWish = custWish;
}
}
Test class ----------------------------
import java.util.Scanner;
import java.io.*;
public class test {
public static void main(String args[]) throws IOException
{
Scanner s = new Scanner(System.in);
System.out.println(" enter customer name : ");
String name = s.nextLine();
System.out.println(" enter customer address : ");
String address = s.nextLine();
System.out.println(" enter customer phone number : ");
String phoneNo = s.nextLine();
System.out.println(" enter customer number : ");
String custNo = s.nextLine();
System.out.println(" enter yes/no -- customer wants to receive mail ? : ");
String flag = s.nextLine();
boolean wish;
if(flag.contains("y"))
wish =true;
else wish=false;
Customer customer = new Customer(name,address,phoneNo,custNo,wish);
System.out.println(customer.toString());
}
}
OUTPUT ------------------------
enter customer name :
san
enter customer address :
USA
enter customer phone number :
12345
enter customer number :
1
enter yes/no -- customer wants to receive mail ? :
yes
Customer [customerNo=1, custWish=true][name=san, address=USA, telephoneNo=12345]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.