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.
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.
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.
Here's what I've got:
.\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
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.
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?
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.