我正在尝试编写满足以下输出的构造函数和方法,但我无法开始。
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
这是测试代码:
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);
}
}
这是我到目前为止的代码:
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");
}
}
这是我完成的代码。感谢所有帮助过的人。
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;
}
}