• Post Reply Bookmark Topic Watch Topic
  • New Topic

2 Classes, 1 Driver Program  RSS feed

 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a lot of trouble writing my driver for this program. Could someone help me through this, please?

For a quick overview.

RoomDimension: Has two fields (length and width) and calculates the area.
RoomCarpet: Has a fields for cost per square foot. Also has a toString() method to return the total cost (area*cost per square foot).
CarpetCalculator (driver): Asks you to enter the dimensions and price per square foot. Print out the total cost. Also add a loop asking if the user would like to repeat.






 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. You didn't ask a question, so are you just looking for general criticism?

If so, then one thing I notice is that your RoomCarpet class doesn't have any way to make carpets with non-zero area.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I've been struggling with the driver a bit.

How does the RoomCarpet class not have a way to make any with non-zero area?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of Campbell's design principles: provide one constructor for every class, but try not to provide any more. The fewer the better.

Go through the constructors and see what value you are setting your carpet size to.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two constructor because that's what we've been instructed to do for this assignment.

I'm not seeing what you're saying. Am I calling the empty constructor by mistake?
 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sam,
You could put an output inside your constructor, that will tell you which ones running.
I haven't taken a good look at your code, but the first thing that came to mind is if you have omitted the relevant imports by design?
cheers,
Daniel
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have to provide two constructors then you have to provide two constructors.
Remember what constructors are supposed to do: get the object into a “consistent“ state, i.e. a state where the invariant is established. In the case of the room class, there are, I think, two invariants:
  • 1: length > 0
  • 2: width > 0
  • Have you checked that both constructors establish those invariants?
    Why have you provided setXXX methods in the room class? Do you expect the room to change size?
    I am not happy with the toString method, because it does not show the values of the fields. I would expect a print‑out like
    Room 14 × 11′
    Use \u2032 for ′ and \u00d7 for ×

    Forget the carpet class for the time being. Get the room class working. Try this sort of thing to test it:-Test all its methods like that. Keep going until you are happy it works. Then start on the carpet class.
     
    Sam Pauken
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Campbell In past assignments we've created more than one constructor, but have not used both. Creating two constructors is just requirement for the assignment, but we don't always have to call both.

    As for the toString() method, I see what you're saying, but that type of message isn't required either, so I'm going to defer that until I get the minimum requirements finished. Thanks.

    Currently, the toString() method is printing spaukenmod4.CarpetCalculator@694a4639. If someone could help me figure out what's wrong with this and how to fix it, that'd be great. I need it print the total cost of the carpet that I'm buying.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12565
    49
    Chrome Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sam Pauken wrote:I'm going to defer that until I get the minimum requirements finished. Thanks.

    I will often write tons of code that don't satisfy any requirement, simply because it will help me write that required stuff. I would think a toString method would be EXTREMELY helpful in doing the required stuff. So even if it isn't by the specs you are given, I would consider it a requirement for my own sanity.
     
    Sam Pauken
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok, I originally thought I wasn't able to add something given the requirement from my prof, but that is allowed, so I'll give this a shot.

    Edit: I updated my code above to what I have now. One of you mentioned that my toString() method will print zero values. I can see from the method/the debugger that I'm not calling my inputs, but I'm unsure of how to solve this problem.
     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sam Pauken wrote:@Campbell In past assignments we've created more than one constructor, but have not used both. . . .
    I think the assignment ought not to specify two constructors, and the code would be better with one constructor only, but you are stuck with two.
    What you need to do is to ensure that both constructors initialise the object in a consistent state.
    You cannot go round thinking a constructor will not be called; if it is there with public access, it has been made available fo other code to use. You must assume both constructors will be called.
     
    Sam Pauken
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The assignment requires that I provide two constructors and all possible getters and setters even though I may not use them all. I REALLY NEED someone to help figure out what I'm doing wrong printing out my length and width. I have not had this question answered. Thanks.
     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Go through all your constructors, and work out what the values of length and width are when they complete.
    Query why you have been told to provide two constructors when good design would prefer one constructor.
     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you have written your toString() method correctly, then the testing code I posted on Wednesday should help you find out the answer.
     
    Sam Pauken
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I figure it out. Thanks for your help.
     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well done
    Please show us what you have got.
     
    Sam Pauken
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    For some reason I can't edit my original post anymore. Here it is.





     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sam Pauken wrote:For some reason I can't edit my original post anymore. . . .
    No, you can't. It would have made the original post look like nonsense. Thankyou for posting that code. The no‑args constructor is still incorrect.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!