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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!