2. String is an example of an immutable class. The Person class that is started below, is not. Add the method haveBirthday that will increase the person’s age and the getter and setter method for the name variable.
public class Person{
private String myName;
private int myAge;
public Person(String name, int age)
{ myName = name; myAge = age; }
public void printMe()
{ System.out.println(“Name: “ + myName + “\tAge: “ + myAge); } }
public class Person{ private String myName; private int myAge; public Person(String name, int age) { myName = name; myAge = age; } public void printMe() { System.out.println("Name: " + myName + "\tAge: " + myAge); } public void haveBirthday(){ myAge += 1; } public String getMyName() { return myName; } public void setMyName(String myName) { this.myName = myName; } }
Get Answers For Free
Most questions answered within 1 hours.