Ask Question
21 February, 16:36

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); }

+5
Answers (1)
  1. 21 February, 16:54
    0
    import static java. lang. System. out;

    class Person

    {

    private String myName;

    private int myAge;

    String getName () / /getter for myName

    {

    return myName;

    }

    void setName (String name) / /setter for myName

    {

    myName=name;

    }

    public Person (String name, int age)

    {

    myName = name;

    myAge = age;

    }

    public void printMe ()

    {

    System. out. println ("Name: " + myName + "/tAge: " + myAge);

    }

    public int haveBirthday () / /Age is incremented

    {

    return myAge+=1;

    }

    }

    public class Abc

    {

    public static void main (String args[])

    {

    Person p=new Person ("rajpal",22); / /object for class is created

    System. out. println (p. getName () + " age after birthday is "+p. haveBirthday ());

    }

    }

    Explanation:

    Method for age increment i. e. haveBirthday () is created and a new main class is creted which is callinh haveBirthday () method.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “String is an example of an immutable class. The Person class that is started below, is not. Add the method haveBirthday that will increase ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers