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