
Brandon W. answered 12/19/22
Advanced Java
Hello, this is a great question! What a great use of encapsulation.
To begin, let's look at our first Class: Person. We will come back to the Main or Driver class in a moment. First, we need to declare 3 String variables and set the access modifier too protected for all of them. We can list all 3 variables in a row since we are only declaring them and not initializing them...
class Person{
protected String name, address, phone;
}
Next, let's create the constructor, mutator, and accessor methods for this superclass. The constructor will need to take 3 arguments; name, address, and phone, and assign them to the protected variables we declared previously. We will also need to create the mutator and accessor methods to access and modify the protected data since protected data can only be accessed and modified through the Class it is stored in. I, personally, like to group all my mutator methods together and all my accessor methods together; however, you can organize them however you would like...
class Person{
protected String name, address, phone;
public Person(String name, String address, String phone){
this.name = name;
this.address = address;
this.phone = phone;
}
//mutator methods
public void setName(String name){
this.name = name;
}
public void setAddress(String address){
this.address = address;
}
public void setPhone(String phone){
this.phone = phone;
}
//accessor methods
public String getName(){
return this.name;
}
public String getAddress(){
return this.address;
}
public String getPhone(){
return this.phone;
}
}
Continuing, we need to create a second class called Customer that extends the first class. The Customer class is considered the subclass and the Person will be considered the superclass in which Customer inherits its data. To do this we simple add "extends Person" when declaring the new class. The Customer class also will include two new protected variables: customer number and mailing preference...
class Customer extends Person(){
protected String custom;
protected Boolean mail;
}
Now, we must create a constructor for this class that includes assigning the variables inherited from the class Person as well as the variables we just added to the class Customer. To do this, we must use the super method to assign the variables that we inherited and this to assign the variables we just added to the class. Once again, we need to create the mutator and accessor methods for the two new variables. You will notice that because the mail preference is a boolean we need to convert the boolean to a String for the mutator and accessor methods. Otherwise, the output will say "true" or "false" when we want it to say "yes" or "no." To do that, we can use a simple if else statement: if the String input is "yes" the boolean returns as true, and if it's "no" it returns false. You may use the long form or short form if else statement. I will include the short form below...
class Customer extends Person(){
protected String customerNum;
protected Boolean mail;
public Customer(String name, String address, String phone, String customerNum, Boolean mail){
super(name, address, phone)
this.customerNum = customerNum;
this.mail = mail;
}
//mutator methods
public void setCustomerNum(String customerNum){
this.customerNum = customerNum;
}
public void setMail(String mailOpt){
this.mail = mailOpt.equals("yes");
}
//accessor methods
public String getCustomerNum(){
return this.customerNum;
}
public Boolean getMial(){
return mail == true ? "yes" : "no";
}
}
There we go! The superclass and subclass are all set! Now all we need to do is set up the Driver or Main class to run the program. Here, we want to include a scanner to ask for input from the user, input the data from the user to create a Customer object, and print the data. All of this should feel pretty routine at this point so I will short hand a bit of the repeated code below...
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
//create scanner object
Scanner scanner = new Scanner(System.in);
//declare variables
String name, address, phone, customerNum, mail;
Boolean mailOpt;
//get user input
System.out.print("Enter Customers Name: ");
name = scanner.nextLine();
System.out.print("Enter Customers Address: ");
address = scanner.nextLine();
.
.
.
.
mail = scanner.nextLine();
mailOpt = mail.equals("yes"); //converts String to boolean
//Create customer object
Customer cust1 = new Customer(name, address, phone, customerNum, mailOpt);
//print data
System.out.print("Name: " + cust1.getName + "\nAddress: " + cust1.getAddress + ...);
}
}
When you organize your file since this is all in one file, make sure the Main class is first, then the Person class, and lastly the Customer class. As described in the instructions make sure the Person and Customer classes do not have a modifier (I.e. public or private) since they are all sharing a single file.
There are multiple ways to achieve the same outcome when it comes to coding, so there may be another way that makes more sense to you when it comes to some aspects of this question. Thats okay! Depending on what your professor requires there may be a little variation. Hopefully this helps you understand the process and how it should look. If you have any questions please feel free to let me know! :)