aspose file tools*
The moose likes Beginning Java and the fly likes Problem with aggregation of multiple classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with aggregation of multiple classes" Watch "Problem with aggregation of multiple classes" New topic
Author

Problem with aggregation of multiple classes

Brian Winkler
Greenhorn

Joined: Jun 05, 2012
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?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10908
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

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 - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

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

Joined: Jun 05, 2012
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.

Thanks for your help.


Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

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
Bartender

Joined: Oct 02, 2003
Posts: 10908
    
  12

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
And welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with aggregation of multiple classes
 
Similar Threads
My program wont work.
I keep getting this error and dont know what to do to fix it?
compile error
output prints null
My loop isn't working