So my Java teacher wants us to write a program that simply says "Ben Barcomb is 19 years old" That's it, nothing more, nothing less.
Instead of using System.out.println
like a normal person he wants us to use an instance variable in the Person class for the full name and age that must be private
, he also wants a getter and setter method for the fullname and variable as well. This is the tester code I have, but I'm kind of stuck on the variable and getter/setter methods.
public class PersonTester {
public static void main(String[] args) {
Person p1 = new Person();
p1.setFullname("Bilal Gonen");
p1.setAge(76);
String myFullname = p1.getFullname();
int myAge = p1.getAge();
System.out.println(myFullname + " is " + myAge + " years old.");
}
}
public class Person{
private String myFullname;
private int myAge;
public String getFullname()
{
return myFullname;
}
public int getAge()
{
return myAge;
}
public Person(String aFullname)
{
myFullname = aFullname;
}
public void setFullname()
{
myFullname = aFullname;
}
}
The valid code is as below, use getters for the String input of
System.out.println()
Full test code is as below;
And the output is as below;
Note: And it will make your job easier whenever a similiar project comes, when making a POJO class (in this example the Person class), use eclipse's (or any IDE's) "generate getters/setters" shortcut (on Eclipse, you can use it with Alt+Shift+S)