2021. 4. 8. 09:56ㆍJava programming
The 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 meter of carpet. For example, the area of the floor that is 12 meters long and 10 meters wide is 120 square meters. To cover the floor with a carpet that costs $8 per square meter would cost $960.
1. Write a class with the name Floor. The class needs two fields (instance variables) with name width and length of type double.
The class needs to have one constructor with parameters width and length of type double and it needs to initialize the fields.
In case the width parameter is less than 0 it needs to set the width field value to 0, in case the length parameter is less than 0 it needs to set the length field value to 0.
Write the following methods (instance methods):
* Method named getArea without any parameters, it needs to return the calculated area (width * length).
2. Write a class with the name Carpet. The class needs one field (instance variable) with name cost of type double.
The class needs to have one constructor with parameter cost of type double and it needs to initialize the field.
In case the cost parameter is less than 0 it needs to set the cost field value to 0.
Write the following methods (instance methods):
* Method named getCost without any parameters, it needs to return the value of cost field
3. Write a class with the name Calculator. The class needs two fields (instance variables) with name floor of type Floor and carpet of type Carpet.
The class needs to have one constructor with parameters floor of type Floor and carpet of type Carpet and it needs to initialize the fields.
Write the following methods (instance methods):
* Method named getTotalCost without any parameters, it needs to return the calculated total cost to cover the floor with a carpet.
public class Floor {
private double width;
private double length;
public Floor(double width, double length){
if (width < 0){
this.width = 0;
} else if (length < 0){
this.length = 0;
} else {
this.width = width;
this.length = length;
}
}
public double getArea(){
return (this.width * this.length);
}
}
public class Carpet {
private double cost;
public Carpet(double cost){
if(cost < 0){
this.cost = 0;
} else {
this.cost = cost;
}
}
public double getCost(){
return cost;
}
}
public class Calculator {
private Floor floor;
private Carpet carpet;
public Calculator(Floor floor, Carpet carpet){
this.floor = floor;
this.carpet = carpet;
}
public double getTotalCost(){
return (floor.getArea() * carpet.getCost());
}
}
The basis of object oriented programming is that each class is an object that holds data like variables and functions like methods. In this coding exercise, Floor and Carpet are objects with specific data related to their class.
The methods Floor, Carpet and Calculator are all constructors since they initialise the default values for each class with the same names as their class without 'void'.
For the private variables such as width, length, cost, floor and carpet, getter methods are used for the restricted access to the "hidden data".
To explain more about the private Floor floor and private Carpet carpet of the Calculator class, when we use the new() keyword to create a new object, like a new Floor or new Carpet, we're using the class as a blueprint or a remote controler to create those objects. So once they are defined, we access each object's data through the methods via the getters and setters.
'Java programming' 카테고리의 다른 글
Coding Exercise 34: Complex Operations (0) | 2021.04.09 |
---|---|
Coding Exercise 32: Point (0) | 2021.04.07 |
Coding Exercise 31: Wall Area (0) | 2021.04.06 |
Coding Exercise 30: Person (0) | 2021.04.05 |
Coding Exercise 29: Sum Calculator (0) | 2021.04.04 |