-3

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;
    }
  }
ADTC
  • 8,999
  • 5
  • 68
  • 93
Alex Chavez
  • 185
  • 1
  • 10
  • 1
    Your constructor has no parameters and in the test you're passing 2 arguments. That shouldn't even compile. – Hugo Sousa Jan 31 '14 at 00:39
  • hint: Don't double declare your variables in the `Person` class – MadProgrammer Jan 31 '14 at 00:39
  • my teacher said we sould always put a constructor the has zero parameters @HugoSousa – Alex Chavez Jan 31 '14 at 00:42
  • 4
    Your teacher should take up knitting, perhaps. – Brian Roach Jan 31 '14 at 00:43
  • You have defined a constructor with no parameters, yes. But you're calling it with 2 arguments in the test. You haven't declared any constructor with 2 parameters, so that shouldn't compile. – Hugo Sousa Jan 31 '14 at 00:43
  • Start at the top, as soon as you get to something you haven't provided for yet, write code for it. That's really all there is to it. If you really have *no* idea how to do this, you have an awfully terrible teacher and textbook, you haven't tried hard enough (it's generally this one) or you have very little potential to be a programmer and should perhaps switch courses / subjects if at all possible. – Bernhard Barker Jan 31 '14 at 00:50
  • @BrianRoach, good call, even something knitted badly will come in handy in this weather. – Tony Hopkinson Jan 31 '14 at 01:12
  • nice hair @BrianRoach – Alex Chavez Jan 31 '14 at 01:18
  • @AlexChavez [I have already edited your question with these fixes.] You are not supposed to ***replace*** your original question with the fixed code. You can add your fix below your original question, but the question **must remain**. And please, no ALL CAPS and no need to say all those things like *"okay I finished"*, *"stupid jokes"* etc. – ADTC Jan 31 '14 at 02:11

2 Answers2

0

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.

Justin C
  • 333
  • 1
  • 7
  • I was told that I should always put a constructor with zero parameters – Alex Chavez Jan 31 '14 at 00:43
  • Eh, not _always_, but you should never _declare_ variables in constructors. Be sure you are explicitly setting the values of your variables either by using the dot operater: `mary.height = 4.9`, or make sure your constructor takes arguments so you can do: `Person mary = new Person(4.9, ..., ...)` – Justin C Jan 31 '14 at 00:46
0

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;
}
z atef
  • 7,138
  • 3
  • 55
  • 50
  • If you use getters and setters, the applicable member variables should generally also be `private`, and the test code shouldn't be changed, so this doesn't help a whole lot, and probably shouldn't be an answer. – Bernhard Barker Jan 31 '14 at 00:56
  • @Dukeling thanks for the input.. So you think passing values via a Constructor has advantage over using setters and getters ? Can you pls shed some light on this :) – z atef Jan 31 '14 at 01:03
  • I am thinking he can just create another instance of the person class Person person2 = new Person(); – z atef Jan 31 '14 at 01:06
  • 1
    "So you think passing values via a Constructor has advantage over using setters and getters?" - I do (passing by constructor is generally much less code, and just the way things are done, albeit slightly less readable), but I'm not sure how you got that from my comment. I wasn't commenting on the getters and setters, just that your member variables are [package-private](http://stackoverflow.com/questions/714791), not `private`, but that's fairly minor really. The main issue is that you changed the test code significantly, which leads to a not-that-helpful answer. – Bernhard Barker Jan 31 '14 at 01:12