1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Finger [1]
3 years ago
12

Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructo

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

Computers and Technology
1 answer:
Fittoniya [83]3 years ago
8 0

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.

You might be interested in
Max magnitude Write a method maxMagnitude() with two integer input parameters that returns the largest magnitude value. Use the
Ira Lisetskai [31]

Answer:

The java program is as follows.

import java.lang.*;

public class Numbers

{

   //variables to hold two numbers

   static int num1=-8, num2=-2;

   //method to return integer with higher magnitude

   public static int maxMagnitude(int userVal1, int userVal2)

   {

       if(Math.abs(userVal1) > Math.abs(userVal2))

           return userVal1;

       else    

           return userVal2;

   }

public static void main(String[] args) {

    int max = maxMagnitude(num1, num2);

 System.out.println("The number with higher magnitude among "+num1+ " and "+num2+ " is "+max);

}

}

OUTPUT

The number with higher magnitude among -8 and -2 is -8

Explanation:

1. Two integer variables are declared to hold the two numbers. The variables are declared at class level (outside main()) and declared static.

2. The variables are initialized with any random value.

3. The method, maxMagnitude(), takes the two integer parameters and returns the number with higher magnitude. Hence, return type of the method is int.

public static int maxMagnitude(int userVal1, int userVal2)

4. This method finds the number with higher magnitude using the Math.abs() method inside if-else statement.

               if(Math.abs(userVal1) > Math.abs(userVal2))

                   return userVal1;

        else    

                   return userVal2;

5. Inside main(), the method, maxMagnitude(), is called. This method takes the two variables as input parameters.

6. The integer returned by the method, maxMagnitude() is assigned to a local integer variable, max, as shown.

      int max = maxMagnitude(num1, num2);

7. The result is displayed to the user as shown.

System.out.println("The number with higher magnitude among "+num1+ " and "+num2+ " is "+max);

8. The class variables and the method, maxMagnitude(), are declared static so that they can be used and called inside main().

9. The variable, max, is called local variable since it can be used only within the main() where it is declared. It is not declared static.

10. The class is declared public since it has the main() method and execution begins with main().

11. The program is saved with the same name as the name of the public class having main(). In this case, the program is saved as Numbers.java.

12. No user input is taken for the two input parameter values. The static variables can be assigned any integer value and the program can be tested accordingly.

8 0
4 years ago
Convert 128 GB into KB​
goblinko [34]

Answer:

1,073,741,274  KB

Explanation:

Use an unit convertor or an calculator.

8 0
4 years ago
Read 2 more answers
Which of the following domestic appliances does not use a magnet?
alekssr [168]
Pressing iron isn’t domestic appliances
7 0
3 years ago
Read 2 more answers
Software piracy is acceptable as it helps us obtain software cheaper or sometimes even for free.
mr Goodwill [35]

Answer:

1: false because it's just scams

2: true

Explanation:

3 0
3 years ago
The picture that graphically represents the items you use in Windows is called a/an
alex41 [277]
<span>The answer is letter C which is GUI. GUI means Graphic User Interface. GUI are elements such as icons, windows, menus, buttons, and other graphical elements. These GUI carry out commands that instead of reading words or phrases, graphics are used to be more convenient. </span>
6 0
4 years ago
Read 2 more answers
Other questions:
  • Stan is looking at cloud computing options for his business needs. He wants a model that helps his business customers and client
    12·2 answers
  • What is one important feature of an AUP?
    14·1 answer
  • Reputable firms often ask recent graduates to pay an up-front fee for a job.
    10·2 answers
  • What is the term for a web site and network used only by employees within a company?
    12·1 answer
  • With respect to the five components of social media information systems (SMIS), which of the following statements is true of SM
    11·1 answer
  • Write a C++ program that takes two numbers from the command line and perform and arithmetic operations with them. Additionally y
    11·1 answer
  • What is modularity? Help asap
    9·1 answer
  • Cycle for science and technology<br>​
    14·1 answer
  • Question # 2
    6·1 answer
  • Discuss the factors which cause imbalance in the environment.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!