• Post Reply Bookmark Topic Watch Topic
  • New Topic

Aggregation  RSS feed

 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I'm trying to do. The code works, however there's two problems.

Instructions
Follow the instructions on page 554 problem #3 with the addition of the following:
Redraw the UML diagram to add the following for each class and implement them:
Static field that counts the number of objects created
Modify the constructors to increment this variable.
Copy constructor
Copy method
No-argument constructor
equals method
set and get functions for length, width and carpetCost. Use these functions inside of other functions. Do not access these fields directly.
toString() for RoomDimension should display the object counter for RoomDimension, length, width and the results of getArea()
toString() for RoomCarpet should invoke the RoomDimension toString() to display size, CarpetCost, object counter for RoomCarpet and the results of getTotalCost()
optional: create a set and get for size. This will not be graded. If you cannot make it work comment out your code.
Create classes that match your UML diagram.
Create a demo program that uses these classes correctly with the data supplied in the problem. Create a constant of $8.00 in the demo for the price of the carpet. Use all the above methods either inside of your classes or in the demo program. Create two RoomCarpet objects and compare them. See the solution for additional information.



This is the code for this class. First, I don't think I set up the this.size thing correctly. I think there should be something in the parenthesis, but I'm not sure. The other problem, is that I'm supposed to make an equals method, but I'm completely lost on that. I think it's what's supposed to compare the two RoomCarpet objects. Any help is much appreciated. The good news, is that I've already taken care of compiler errors, it's just a matter of finishing up the rest of this. Those are the lines that I'm pretty sure are incorrectly set up. And I'm not sure about the copy constructor/methods. I think it's right.

RoomCarpet.java
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your getSize() method creates a new RoomDimension object. This is unnecessary, partly because the field 'size' is already a RoomDimension, and also this overwrites (sets) the 'size' to be a new (empty?) RoomDimension.

You overuse the 'this' keyword. 'this' should only be used when a field name is ambiguous with regards to another variable name. Example

In this case 'this' is necessary because there is a method parameter also called 'someValue'.

In your setSize() method the names 'size' and sSize' don't conflict so 'this' is not necessary.

I'm not sure what your previous requirements are but I might have named 'size' as 'dimension'. 'size' is used all over the place in Java Collections and I'd want to differentiate it from that. If you wrote the RoomDimension class I'd add another getter to it 'getSquareFeet'. This would help when computing total cost. You have a compilation error in your computation of cost and this would fix that.
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've updated it and removed all of the 'this' keywords. I had put them in there because I misunderstood some of the instructions. So I've made several changes, and now have compilation errors that are confusing.

Here's what I've got:

RoomDimension.java




RoomCarpet.java

RoomDemo.java



Errors
.\RoomCarpet.java:79: error: bad operand types for binary operator '*'
double total = getCarpCost() * size; //!!!
^
first type: double
second type: RoomDimension
.\RoomCarpet.java:85: error: cannot find symbol
String str = "\n Room size: " + size() +
^
symbol: method size()
location: class RoomCarpet
.\RoomCarpet.java:86: error: cannot find symbol
"\n Carpet cost (per sq. ft.): " + carpCost() +
^
symbol: method carpCost()
location: class RoomCarpet
3 errors


This is my first time learning about aggregation and I'm struggling with it. The size variable seems to give me a lot of problems in general. I'm not really understanding how to use it.
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the size variable is a RoomDimension object so you can't do arithmetic with it. You can't multiply something times a RoomDimension because that isn't meaningful to a Java compiler. But it's supposed to mean something to you, right? So what you need to do is to extract something from the RoomDimension object, something which is a number, and then multiply by that number which you extracted.

That might seem a bit confusing the way I wrote it so start with this line of code:



Here you have getCarpCost(), which is a number because that method returns a double. Now, write down in your native language what you would like to multiply getCarpCost(). Remember not to answer in terms of Java code, but answer in terms of your problem description. So you have a RoomDimension object, what number do you want to get out of that?
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only thing that would make sense, is for size to be the size (area) of the room which is in RoomDimension.java. This program is supposed to use the constant $8 as the price of carpeting per square foot. It's supposed to allow the user to input the length and width of the room, and calculate it in the Room Dimension class, then use that info in the RoomCarpet class to find the cost of carpeting to fit that size room. If I'm understanding it correctly. Even some of my classmates were confused by the way in which the prof wrote the additional instructions, and when asked he didn't really elaborate on it. He likes to talk in circles until we just give up asking questions lol. So how would I set size to be the area total from RoomDimension.java?
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you wrote the RoomDimension class I'd add another getter to it 'getSquareFeet'. This would help when computing total cost. You have a compilation error in your computation of cost and this would fix that.
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We're not to make extra methods, only those we're told to make. size can have a get and set, but I couldn't figure out how to make those work. It's a mix of making this from a UML, book instructions, plus instruction additional instructions (posted at the top) I can only have the methods that are already there, same for the fields. It's just a matter of making the methods actually work.
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you do have getLength() and getWidth() to work with. How would you use those to calculate cost?
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I just stuck .getArea onto the end of size and it fixed it. Runs, no compiler errors.....er, well there were none. I added a new line to my RoomDemo to try to compare Room 1 to Room 2, but think I did it wrong.
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you're making progress then. But two steps forward, one step back... that's pretty normal for computer programming.

I assume your error messages are referring to line 13 of what you posted? I can see at least two errors in that line; the error messages ought to be giving you some hint about the problem. Although if you have too many errors close together sometimes the compiler gets all baffled and blurts out unhelpful things. So if you can't make sense of the error messages, by all means continue posting them in this thread.
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have several problems to work through, here's one:

Don't compare size with room.size using (==), use equals() instead. The way you've written it it will be true only if they both refer to the same object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!