다음 출력을 만족하는 생성자와 메서드를 작성하려고하는데 시작하는 데 문제가 있습니다. Person 클래스의 메서드 작성
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;
}
}
생성자에는 매개 변수가없고 테스트에서 2 개의 인수를 전달합니다. 그것도 컴파일해서는 안됩니다. –
힌트 :'Person' 클래스에서 변수를 선언하지 마십시오. – MadProgrammer
선생님은 항상 생성자를 넣어야한다고 제게 알려 주셨습니다. @HugoSousa –