Pls edit this code... need help.. how to system out name? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Pls edit this code... need help.. how to system out name?

public class MyClass { public static void main(String[ ] args) { Person j; j = new Person("John"); j.setAge(20); celebrateBirthday(j); System.out.println(j.getAge()); } static void celebrateBirthday(Person p) { p.setAge(p.getAge() + 1); } } public class Person { private String name; private int age; Person (String n) { this.name = n; } public int getAge() { return age; }

18th Feb 2017, 3:54 AM
Nik Danial
Nik Danial - avatar
2 Answers
+ 1
Modify Person class. Then call System.out.println(j.getName()); public class Person { private String name; private int age; Person (String n) { this.name = n; } public int getAge() { return age; } public String getName() { return name; } }
18th Feb 2017, 4:11 AM
Jian-hua Yeh
Jian-hua Yeh - avatar
0
public class Person { private String name; private int age; Person (String n) { this.name = n; } public int getAge() { return age; } public String getName() {return name;} }
18th Feb 2017, 4:34 AM
Hyper89