# Carpet calculator program

## The problem

The westfield carpet company has asked you to write an application that calculates the price of carpeting for rectangular rooms. To calculate the price, you multiply the area of the floor(width times length) by the price per square foot of carpet. For example, the area of floor that is 12 feet long and 10 feet wide is 120 square feet. To cover that floor with carpet that costs \$8 per square foot would cost \$960 (12x10x8=960)

First, you should create a class named RoomDimension that has two Feields: one for the lenght of the room and one for the width. The RoomDimension class should have a method that returns the area of the room (the area of the room is the room's length multiplied by the room's width).

Next, you should create a RoomCarpet class that has a RoomDimension object as a field. It should also have a field for the cost of the carpet per square foot. The RoomCarpet class should have a method that returns the total cost of the carpet.

Once you have written these classes, use them in an application that asks the user to enter the dimensions of a room and the price per square foot of the desired carpeting. The application should display the total cost of the carpet.

## Level Up

• For each parameter passed as a contructor check the validity of each parameter. Regardless of which library you will want to check that the input is not null and value is positive. For instance, if you use guava you would use `checkNotNull` for not null.
• Write a unit test for `getArea` and `getTotalCost`
• Clean up output in RoomCarpet `toString` method

Carpet calculator program posted by on

Tagged: java and java-exercises-beginner

Share on: