编写Person类的方法

时间:2014-01-31 00:37:15

标签: java object methods boolean drjava

我正在尝试编写一个构造函数和一个满足以下输出的方法,但我很难开始。

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 个答案:

答案 0 :(得分:0)

考虑需要传递给Person类以创建Person对象的信息类型。 Person的构造函数是否没有参数,但是你试图在测试代码中传递两个参数是否有意义?

您还需要为Person课程编写一些其他功能,但如果您考虑我上面写的内容,您应该可以开始使用。

答案 1 :(得分:0)

创建Setter和Getters并传递值将是一种更好的方法。

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