0

I have just started doing object oriented programming as part of my course, but I am struggling with it, specifically the toString method in a Person Class. I need to write a toString() method to display the contents of instance variables.

I need to by sample print out:

Person[forName=joe, surname= smith, age= 25, height= 1.57, gender= male]

I also need to format it like this using the format method:

smith   joe   25   1.57   male
davis   sian  18   1.73   female
***     ***   ***  ***    ***

I havent written a tester yet, but here is what I have written so far for the class and now I'm stuck, I'm not even sure if I am getting the toString statement wrong. I am using netBeans for this:

public class Person
{
  private String surname;   
  private String forname;   
  private int age;   
  private double height;           
  private String gender;

  public String toString()
  {
    return getClass().getName() + "[surname= " + surname + " forname= " + forname + " age= " + age + " height= " + height + " gender " + gender + "]";
  }
}

What I need to do is make a class called Person that I can test. It needs to be able to hold the five variables above (surname etc) for different people. I need to be able to print out each of the instance variables with a toString() method and to use a format() method to produce a string with formatting infomation in order for the string printed out by the toString() method to be formatted like the second quotation.

Am I on the right track and regardless, how can I work through this?

EDIT: I have looked at the Person Class and have done what I can with it, does it seem decent enough? I am going to try and get a PersonTester together.

public class Person
{ 
    private String surName;
    private String forName;
    private int age;
    private double height;
    private String gender;

@Override
public String toString()
{
    return getClass().getName() + "[surName= " + surName + " forName= " + forName + " age= " + age + " height= " + height + " gender " + gender + "]";
}

public void format()
{
    System.out.format("%10s%10s%10d%10f%10s", "surName", "forName", age, height, "gender");
}

public String getSurName()
{
    return surName;
}

public String getForName()
{
    return forName;
}

public int getAge()
{
    return age;
}

public double getHeight()
{
    return height;
}

public String getGender()
{
    return gender;
}

public void setSurName(String surName)
{
    this.surName = surName;
}

public void setForName(String forName)
{
    this.forName = surName;
}

public void setAge(int age)
{
    this.age = age;
}

public void setHeight(double height)
{
    this.height = height;
}

public void setGender(String height)
{
    this.gender = gender;
}

}

EDIT 2: Started on a class Tester, but I am running into errors again about the setter's not having a ; and not being a statement.

Here's the tester so far:

public class PersonTester 
{
public static void main(String[] args)
{
    System.out.println("PersonClassTester");
    System.out.println("*****************");
    System.out.println("");

    Person joeSmith = new Person();
    String "smith" = joeSmith.setSurName();
    String "joe" = joeSmith.setForName();
    int 25 = joeSmith.setAge();
    double 1.57 = joeSmith.setHeight();
    String "male" = joeSmith.setGender();

    joeSmith.toString();
    joeSmith.format();
}
}
SIHB007
  • 59
  • 1
  • 9
  • Your toString method seems about right to me. For the formatting, check this: http://docs.oracle.com/javase/tutorial/essential/io/formatting.html – Stultuske Feb 05 '15 at 12:31
  • It is not clear what asking. Can you add a better example and what you program does. – Jens Feb 05 '15 at 12:35
  • Edited the explanation, if you still don't get what I'm talking about I will try and adjust it more. – SIHB007 Feb 05 '15 at 12:43
  • Added a 'complete' Person Class and the start of the Drive Tester for the class. – SIHB007 Feb 06 '15 at 15:38

3 Answers3

0

First of all you have to noticed that every object you create extends class Object. This Object class contains methods like toString, equals, hashCode... your object have also this methods(inherited from Object). When you override (you should annotate this method with @Override) for eg. toString you will always use this toString method instead of inherited one. Its called polymorphism. Your toString method looks fine. In your main method you should use some kind of loop through all Persons and there format the output from toString method. You have error in your code public String toString(); {

remove the ; after ()

public class Main {

    public static void main(String[] args) {

        Person a = new Person("smith", "joe", 25, 1.57, "male");
        Person b = new Person("davis", "sian", 18, 1.73, "female");

        List<Person> persons = new ArrayList<Person>();
        persons.add(a);
        persons.add(b);

        for(Person p : persons){    
            System.out.format("%s   %s   %s   %d   %.2f   %s", p.getClass().getName(), p.getSurname(), p.getForname(), p.getAge(), p.getHeight(), p.getGender());
            System.out.println();
        }   
    }
}

If you call this

System.out.println(p.toString());

than you ll get your person via toString method.

I just edit your Person class and add constructor and geters + seters

public Person(String surname, String forname, int age, double height,
        String gender) {
    super();
    this.surname = surname;
    this.forname = forname;
    this.age = age;
    this.height = height;
    this.gender = gender;
}

Here is geter and seter sample.

public String getSurname() {
        return surname;
    }

public void setSurname(String surname) {
        this.surname = surname;
    }
Milkmaid
  • 1,569
  • 3
  • 22
  • 36
  • I have used the override, but the NetBeans Client is still pinging errors about a missing method body or requiring an abstract to be declared. – SIHB007 Feb 05 '15 at 12:47
  • Ok, the error's no longer there, I will start looking for a format method. – SIHB007 Feb 05 '15 at 12:50
0

As already mentioned your toString() is fine.

Please note that the toString() method and the format() method are IMO supposed to work independently as they do serve different purposes.

I suggest to put the format method not in the person class (or at least make it static method). This is because a single Person instance has not enough information for it to be printed in the table format. It at least needs to know the column widths. Otherwise you could end up with something like this:

smith   joe   25   1.57   male
someVeryLongFirstName   sian  18   1.73   female
***     ***   ***  ***    ***

So the format method should take a list of persons that should be printed out and then first calculate the column widths. After this is done you then just pad the property value to the column width and print this out.

Community
  • 1
  • 1
SpaceTrucker
  • 11,729
  • 6
  • 48
  • 95
0

You are on the right track:

Inside of the Person class you need to add public methods for each private variable to set the data:

public void setAge(int age) {
    this.age = age;
}

Then you can create a Person object in your main class and set his age:

public class Main {
    public static void main(String[] args) {
        Person p = new Person();
        p.setAge(15);
    }
}

As an alternative, you can use a constructor inside of your Person class to set your object's variables:

public Person(String surname, int age) {
    this.surname = surname;
    this.age = age;
}

And create the object in the main method like this:

Person p = new Person("Nillas", 25);

You can always run your toString() method in the main class after you've created the object and see the result:

System.out.println(p.toString());

Hope this helps, good luck!

nillas
  • 421
  • 3
  • 4