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