Project for Java

144 Views Asked by At

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

4

There are 4 best solutions below

0
On

The valid code is as below, use getters for the String input of System.out.println()

Full test code is as below;

public class PersonTester {
    public static void main(String[] args) {
        Person p1 = new Person();

        p1.setMyFullname("Bilal Gonen");
        p1.setMyAge(76);

        System.out.println(p1.getMyFullname() + " is " + p1.getMyAge() + " years old.");
    }
}

class Person {
    private String myFullname;
    private int myAge;

    public String getMyFullname() {
        return myFullname;
    }

    public void setMyFullname(String myFullname) {
        this.myFullname = myFullname;
    }

    public int getMyAge() {
        return myAge;
    }

    public void setMyAge(int myAge) {
        this.myAge = myAge;
    }

}

And the output is as below;

Bilal Gonen is 76 years old.

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)

0
On

Here is an example of a getter and setter. I am sure you can use this as a guide.

public class Person
{
    private String firstName;
    private String lastName;    

    public void setName(String f, String l)
    {
        firstName = f;
        lastName = l;
    }

    public String getFirstName()
    {
         return firstName;
    }
}

Short tutorial on setters and getters.

1
On

Not doing your homework for you, but I will provide some help on the getters and setters. Here's an example person class with one variable, add the others you need yourself.

public class Person {
    int age;

    public void setAge(int age) { // notice how the setter returns void and has an int parameter
        this.age = age; // this.age means the age we declared earlier, while age is the age from the parameter
    }

    public int getAge() { // notice the return type, int? this is because the var we're getting is an int
        return age;
    }
0
On

Thanks to the help of everyone, as well as some of the research I did myself, I got the program to compile and run correctly, here is the source code. Thanks again to everyone who assisted!

public class PersonTester {
    public static void main(String[] args) {
        Person p1 = new Person();
        p1.setFullname("Ben Barcomb");
        p1.setAge(19);
        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 void setAge(int newAge)
   {
      myAge = newAge;
   }

   public void setFullname(String aFullname)
   {
      myFullname = aFullname;
   }

}