Home>

It's a Java constructor and overload issue. No matter how much I looked up, I couldn't understand it at all, so I would appreciate it if someone could explain it with the correct source code.

Problem (1) Modify the program so that you can assign a value to the age field when creating an instance of the ninngenn class. Use the constructor for assignment, and assign the arguments of the constructor.

Problem (2) When creating an instance of the ninngenn class modified in problem (1), modify the program so that the instance can be created with or without passing a value to the constructor. If i pass a value, set it in the passed age field, and if you do not pass a value, assign 99 to the age field.

The following is the original source code, but I'm not sure if it's correct, so please point out that.

public class ninngenn {
public int age;

// Age setting method
public void setAge (int num) {
age = num;
}
// Age acquisition method
public int getAge () {
return age;
}

}

public class ninngen2 {
public static void main (String [] args) {
// Create an instance
Human a = new Human ();
a.age = 10;
}
}

  • Answer # 1

    Problem (1) When creating an instance of the ninngenn class, make a program so that a value can be assigned to the age field.Fixplease. Use the constructor for assignment, and assign the arguments of the constructor.
    ~ Omitted ~
    Below is the original source codeI don't know if it's correctSo I would appreciate it if you could point out that.

    Normally, I think that "correction" means to fix something that is incorrect.

    As pointed out in the correction request, it is meaningless if you only want to get the answer to this question.
    Start studying again now, or stop studying.

  • Answer # 2

    First, let's write the constructor. The story is then