Question

Design a class named Person with fields for holding a person's name, address, and telephone number...

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 receive mail. Use this information to create a customer object and then print its information.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any problem with the code feel free to comment.

Program

import java.util.Scanner;

/*Person Class*/
class Person {
   private String name, address, telephone;

   //constructor
   public Person(String name, String address, String telephone) {
       super();
       this.name = name;
       this.address = address;
       this.telephone = telephone;
   }

   //all getters an setters
   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 getTelephone() {
       return telephone;
   }

   public void setTelephone(String telephone) {
       this.telephone = telephone;
   }

}

/*Customer class*/

class Customer extends Person {
   private String cnum;
   private boolean mail;

   public Customer(String name, String address, String telephone, String cnum, boolean mail) {
       super(name, address, telephone);
       this.cnum = cnum;
       this.mail = mail;
   }

   public String getCnum() {
       return cnum;
   }

   public void setCnum(String cnum) {
       this.cnum = cnum;
   }

   public boolean isMail() {
       return mail;
   }

   public void setMail(boolean mail) {
       this.mail = mail;
   }
}

/*Driver clas for testing*/
public class Test{
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
      
       String name, address, telephone, cnum, m;
       boolean mail;
      
       //take user input
       System.out.print("Enter Name: ");
       name = sc.nextLine();
       System.out.print("Enter Address: ");
       address = sc.nextLine();
       System.out.print("Enter Telephone number: ");
       telephone = sc.nextLine();
       System.out.print("Enter Customer number: ");
       cnum = sc.nextLine();
       System.out.print("Do you want to receive mails? (Yes/No): ");
       m = sc.nextLine();
       sc.close();
      
       mail = (m.equalsIgnoreCase("Yes"))? true : false;
      
       //creating customer object
       Customer obj = new Customer(name, address, telephone, cnum, mail);
      
       //print customer details
       System.out.println();
       System.out.println("----------Your Details----------");
       System.out.println("Name: "+obj.getName());
       System.out.println("Address: "+obj.getAddress());
       System.out.println("Telephone: "+obj.getTelephone());
       System.out.println("Customer number: "+obj.getCnum());
       System.out.println("Receive mails? "+obj.isMail());
   }
}

Output

Add a comment
Know the answer?
Add Answer to:
Design a class named Person with fields for holding a person's name, address, and telephone number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using JAVA* Design a class named Person with fields for holding a person’s name, address, and...

    Using 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...

  • this is java m. please use netbeans if you can. 7. Person and Customer Classes Design...

    this is java m. please use netbeans if you can. 7. Person and Customer Classes 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 cus- tomer wishes to...

  • Assignment 6 Due: Apr 12, 2019 at 11:59 PM A6 OOP 3 "PreferredCustomer Class" (need to...

    Assignment 6 Due: Apr 12, 2019 at 11:59 PM A6 OOP 3 "PreferredCustomer Class" (need to create Person, Customer classes described in the previous problem) Access A6 from pdf assignment file. Turn in the following files: a6main.java Customer.java Person.java PreferredCustomer.java program compile and run screenshots design document (including UML) A6 7. Person and Customer Classes esign 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...

  • Write a class named PhoneBookEntry that has fields for a person's name and phone number.The class...

    Write a class named PhoneBookEntry that has fields for a person's name and phone number.The class should have a constructor and appropriate accessor and mutator methods. Thenwrite a program that creates at least five PhoneBookEntry objects and stores them in anArrayLitt. Use a loop to display the contents of each object in the ArrayList.

  • JAVA HELP Design a class named Employee. The class should keep the following information in fields:...

    JAVA HELP Design a class named Employee. The class should keep the following information in fields: Employee name Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. Hire date then, Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that extends the Employee class. The ProductionWorker class should have fields to...

  • 7. PersonData and CustomerData classes Design a class named PersonData with the following member variables: lastName...

    7. PersonData and CustomerData classes Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData, which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailinglist The customer Number variable will be used to hold a unique integer for each customer. The mailing List variable should be a bool....

  • Problem 2 (36 pts): You are going to design a class named Computer. The class contains:...

    Problem 2 (36 pts): You are going to design a class named Computer. The class contains: A string field named manufacturer. A string field named serialNumber. A double field named speed (measured in Gigahertz). A constructor that has a parameter for each data field and sets the data fields with these values. A no-arg constructor that sets string fieldsto and numeric fields to 0. Mutator and Accessor methods for each data field. A method toString) that returns each field value...

  • Design a class named EmployeeRecord that holds an employee’s ID number, name, and payrate. Include mutator...

    Design a class named EmployeeRecord that holds an employee’s ID number, name, and payrate. Include mutator methods to set the values for each data field and output the values for each data field. Create the class diagram and write the code that defines the class and implements the mutator methods. need help please with this question

  • Phone Book ArrayList Write a class named PhoneBookEntry that has fields for a person’s name and...

    Phone Book ArrayList Write a class named PhoneBookEntry that has fields for a person’s name and phone number. The class should have a constructor and appropriate accessor and mutator methods. Then write a program that creates at least five PhoneBookEntry objects and stores them in an ArrayList. Use a loop to display the contents of each object in the ArrayList Copyright | Addison-Wesley | Starting Out with Java | [email protected] | Printed from Chegg iOS App

  • Java Programming Design a class named Person and its two subclasses named Student and Employee. A...

    Java Programming Design a class named Person and its two subclasses named Student and Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, date hired. Define a class named MyDate that contains the fields year, month, and day. Override the toString method in each class to display the class name and the person's name....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT