241,913 views
38 votes
38 votes
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. Put all of your classes in the same file. To do this, do not declare them public.

User Windhoek
by
2.8k points

1 Answer

18 votes
18 votes

Answer:

Step-by-step explanation:

The following code is written in Java. It creates the three classes as requested with the correct constructors, and getter/setter methods. Then the test class asks the user for all the information and creates the customer object, finally printing out that information from the object getter methods.

import java.util.Scanner;

class Person {

String name, address, telephone;

private void Person (String name, String address, String telephone) {

this.name = name;

this.address = address;

this.telephone = telephone;

}

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;

}

}

class Customer extends Person {

String customerNumber;

Boolean mailingList;

public Customer(String s, Boolean mail) {

super();

this.customerNumber = customerNumber;

this.mailingList = mailingList;

}

public String getCustomerNumber() {

return customerNumber;

}

public void setCustomerNumber(String customerNumber) {

this.customerNumber = customerNumber;

}

public Boolean getMailingList() {

return mailingList;

}

public void setMailingList(Boolean mailingList) {

this.mailingList = mailingList;

}

}

class Test {

public static void main(final String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter Customer Name: ");

String name = in.nextLine();

System.out.println("Enter Customer Address: ");

String address = in.nextLine();

System.out.println("Enter Customer Telephone: ");

String telephone = in.nextLine();

System.out.println("Would you like to receive mail? y/n ");

Boolean mail;

if(in.nextLine() == "y") {

mail = true;

} else {

mail = false;

}

Customer customer = new Customer("1", mail);

customer.setName(name);

customer.setAddress(address);

customer.setTelephone(telephone);

System.out.println("Name: " + customer.getName());

System.out.println("Address: " + customer.getAddress());

System.out.println("Telephone: " + customer.getTelephone());

System.out.println("Wants to receive mail: " + String.valueOf(customer.getMailingList()));

}

}

Design a class named Person with fields for holding a person's name, address, and-example-1
User Mr Mcwolf
by
3.0k points