title: Objected Oriented Programming- How to use the toString() method and format()?
body: <p>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.</p>
  
  <p>I need to by sample print out:</p>
  
  <pre><code>Person[forName=joe, surname= smith, age= 25, height= 1.57, gender= male]
  </code></pre>
  
  <p>I also need to format it like this using the format method:</p>
  
  <pre><code>smith   joe   25   1.57   male
  davis   sian  18   1.73   female
  ***     ***   ***  ***    ***
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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 + "]";
    }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Am I on the right track and regardless, how can I work through this?</p>
  
  <p>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.</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>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.</p>
  
  <p>Here's the tester so far:</p>
  
  <pre><code>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();
  }
  }
  </code></pre>