Question

I am trying to write a constructor and a method that satisfies the following output and I am having trouble getting started.

4.9
20.0
0
false
4.9' person with $20.00 and 0 tickets
4.9' person with $20.00 and 3 tickets
4.9' person with $20.00 and 1 tickets
4.9' person with $20.00 and a pass

This is the test code:

public class Person2Tester 
{ 
    public static void main(String args[]) 
    { 
        Person     mary; 

        mary = new Person(4.9f, 20.00f); 

        System.out.println(mary.height); 
        System.out.println(mary.money); 
        System.out.println(mary.ticketCount); 
        System.out.println(mary.hasPass); 
        System.out.println(mary);   // Notice the money is properly formatted

        mary.ticketCount = 3; 
        System.out.println(mary);

        mary.useTickets(2);   // You have to write this method
        System.out.println(mary);

        mary.hasPass = true; 
        System.out.println(mary); 
    } 
}

And this is the code I have so far:

public class Person
{
  float height;
  float money;
  int ticketCount;
  boolean hasPass;

  public Person()//empty constructor
  {
    height = 0.0f;
    money = 0.0f;
    ticketCount = 0;
    hasPass = false;
  }

  public Person(float h, float m) 
  {
    height = h;
    money = m;
    ticketCount = 0;
    hasPass = false;

  }
  public String toString() 
  {

    return(this.height + " person with " + this.money + " and " + this.ticketCount + " tickets");
  }

}

Here is my finished code. Thanks to everyone who helped.

  public String toString() 
  {
    if(hasPass)
    {
    return(this.height + "' person with $" + this.money + " and has a pass");
    }
    else
    {
      return(this.height + "' person with $" + this.money + " and " + this.ticketCount + " tickets");
    }
  }
  public void useTickets(int numTickets)
  {
    if(this.ticketCount >= numTickets)
    {
      this.ticketCount -= numTickets;
    }
  }
Was it helpful?

Solution

Think about what kind of information you need to pass to your Person class to create a Person object. Does it make sense that your constructor for Person takes no arguments, but yet you are trying to pass along two arguments in your test code?

There are also some other functions you need to write for your Person class, but if you think about what I wrote above, you should be able to get started.

OTHER TIPS

Create Setters and Getters and pass the values would be a better way to do it.

public class Person2Tester{  

     public static void main(String args[]) 
{ 
    Person mary = new Person(); 
    Person person2 = new Person();
   // add as many as you want perosn3 ,4 ..

    mary.setHeight(1);
    mary.setMoney(200);
    mary.setHasPass(false);
    mary.setTicketCount(4);



    System.out.println(mary.getHeight()); 
    System.out.println(mary.getMoney()); 
    System.out.println(mary.ticketCount); 
    System.out.println(mary.isHasPass()); 
    System.out.println(mary);   // Notice the money is properly formatted

     //add your methods here
} 
}



 public class Person{

  float height;
  float money;
  int ticketCount;
  boolean hasPass;

  public Person( )
{
     this.height = height;
     this.money = money;
     this.ticketCount = ticketCount;
     this.hasPass = hasPass;
}

public float getHeight() {
    return height;
}

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

public float getMoney() {
    return money;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top