Question
Using the final version of EncapsulationExample.java (below), modify it with the following: 1) Add an instance variable (remember encapsulation) of type String that is called
Using the final version of EncapsulationExample.java (below), modify it with the following:
1) Add an instance variable (remember encapsulation) of type String that is called
gender
2) Add the instance variable you created to both of the constructors
a) For the constructor that takes arguments, you can assume the user will instantiate with valid arguments
3) Add a setter method for your new instance variable called setGender
4) Your setter method should set the argument to all lower case, so if the user
enters Male or FEmaLE, they would be stored as male or female respectively
a) Hint, use the Java API for the String class if you are having trouble with this
5) Add a getter for your new instance variable called getGender
public class EncapsulationExample {
//this class doesn't have a main method
//these are instance variables
//each instance of an Object will have it's own instance of these
variables
private String name;
private int age;
//these are constructors
//these are how we will instanstiate a new EncapsulationExample
public EncapsulationExample(){
name = "John Doe";
age = 25;
}//this constructor takes no arguments
//this constructor will take arguments
public EncapsulationExample(String name, int age){
this.name = name;
this.age = age;
}//this contstructor took a String and an int as an argument
public void setAge(int newAge) {
if (newAge <=0);
else age = newAge;
}
public void setName(String newName) {
name = newName;
}
public int getAge() {
return age;
}
public String getName() {
}
}//end of EncapsulationExample class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started