I have tried turning to google and youtube for answers and looked at various possibilities such as nested classes, inner classes, and local inner classes. They don't seem to provide the answers I'm looking for.
I am following a program online and one of the exercises we have to make a carpet cost calculator. We times the width and the length of the floor and then times that by the carpet cost.
To do this we were instructed to create 3 classes.
"Floor" class and calculate the area of the floor:
public class Floor {
private double length;
private double width;
public Floor(double width, double length) {
this.width = width;
this.length = length;
}
public double getArea() {
return this.width * this.length;
}
}
"Carpet" class and it holds the cost of the carpet:
public class Carpet {
private double cost;
public Carpet(double cost) {
this.cost = cost;
}
public double getCost() {
return this.cost;
}
}
"Calculator" class which formulates the two classes methods together to produce the 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();
}
}
what I don't understand is that in the calculator class we make 2 instance fields called:
private Floor floor;
Private Carpet carpet;
So 2 classes have become instance fields in another class. I don't really understand how that works or what is going on here. I would really like to understand because I want to learn. I will greatly appreciate any explanations that will assist this process.