Ask Question
24 December, 18:40

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. 24 December, 22:29
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers