Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Liutauras Vilda
• Knute Snortum
• Bear Bibeault
Sheriffs:
• Devaka Cooray
• Jeanne Boyarsky
• Junilu Lacar
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Carey Brown
• salvin francis
Bartenders:
• Tim Holloway
• Piet Souris
• Frits Walraven

# Problem with aggregation of multiple classes

Greenhorn
Posts: 16
So I have 2 classes and driver code to write. I am supposed to write an application to calculate the price for rectangular room. Multiply the area of the floor (width * length) by the price per square foot of carpet.
Example; the area of floor that is 12 feet long and 10 feet wide is 120 square feet. If the carpet costs \$8 per square foot the total cost is \$960. (12 * 10 * 8 = 960)
Create a class named RoomDimension that has two fields; length and width.
A method that returns the area of the room.
Creat a class named RoomCarpet that has two fields; cost per square feet and the RoomDimension object.
A method that returns the total cost of the carpet.

Write a driver class that asks the user to enter the dimensions of the room and the price per square foot.
The application should display the total cost of the carpet.

These are the code snippets I wrote:

My problem is with the getTotalCost function in the RoomCarpet class. How do I pull the two doubles so that I can do the math equations?

lowercase baba
Posts: 12800
51
• 1
You don't need to pull the dimensions from the RoomDimension object. To calculate the cost, you don't care if it's 10x12 or if it's 120x1. All you want is the area of the room. So, you can call the RoomDimension's getArea() method, and multiply that by the cost.

Sheriff
Posts: 15048
252
• 1
I don't think you have to. TotalCost = area * cost/unit area. All you have to do get the area, which the RoomDimension object already calculates. This is encapsulation and information hiding. The RoomDimension intentionally keeps the length and width information to itself and only gives out the area information.

Junilu Lacar
Sheriff
Posts: 15048
252
• 1
Another note: in your listing of RoomCarpet, lines 20 and 21 can be combined so you don't need a temporary local variable. Nothing else uses the value except the return statement so declaring the local variable to hold the result is redundant. Also, you are calling RoomDimension(size), which is a constructor. While this may not give you a compile error (does it?), it's wrong. RoomDimension(RoomDimension) is what is called a "Copy Constructor": it creates a new instance of a class and setting initial values of its fields by copying the corresponding fields from another instance. That's not going to give you a total cost. Actually, I don't think that line would even compile.

Brian Winkler
Greenhorn
Posts: 16

fred rosenberger wrote:You don't need to pull the dimensions from the RoomDimension object. To calculate the cost, you don't care if it's 10x12 or if it's 120x1. All you want is the area of the room. So, you can call the RoomDimension's getArea() method, and multiply that by the cost.

the way it was assigned, was like this...

RoomCarpet
- size : RoomDimension
- carperCost : double
+ RoomCarpet(dim: RoomDimension, cost : double)
+ getTotalCost() : double
+ toString() : String
<>
|
|
RoomDimension
- length : double
- width : double
+ RoomDimension(len : double, w : double)
+ getArea() : double
+ toString() : String

So according to the diagram, I think I do have to call the two doubles from RoomDimension. Or am I understanding this incorrectly? I am sorry, this is all very new to me.

Junilu Lacar
Sheriff
Posts: 15048
252
• 1
All that diagram tells you is "A RoomCarpet has a RoomDimension", it doesn't say you have to call the two double values that represent length and width of the room. BTW, you can't call values. You can get (read) or set (assign) values. You call or invoke methods. While I personally think it's not a particularly good way to demonstrate them, I believe the concepts that the example is trying to teach are encapsulation and information hiding, which I already explained in my previous post, and separation of concerns. RoomCarpet is responsible for calculating carpet costs, RoomDimension is responsible for calculating room area, and the "driver" class coordinates the interaction between the two to get a job done. And by the way, your driver (class Winkler) doesn't quite finish the job. It creates a RoomDimension and has it calculate the area but it never does anything with the cost that the user input nor does it ever interact with the RoomCarpet class.

fred rosenberger
lowercase baba
Posts: 12800
51
• 1
Look at it this way...

In the real world, you go to a carpet store. You ask the salesman "How much will it cost to carpet my room?". He says "Well, how big is your room?"

He doesn't care what the specific dimensions are, he only cares about the total area. So you can say "120 square feet", and he can give you an answer.

Marshal
Posts: 68156
258
• 1
And welcome to the Ranch

 My, my, aren't you a big fella. Here, have a tiny ad: Java file APIs (DOC, XLS, PDF, and many more) https://products.aspose.com/total/java