Where it says double shippingCost = calculateShipping();
Why is this declared before the creation of the calculateShipping method is actually created??
Also, why is the if-else statement written before the ‘equalization’ of the instance fields and constructor parameters???
public class Order {
boolean isFilled;
double billAmount;
String shipping;
public Order(boolean filled, double cost, String shippingMethod) {
if (cost > 24.00) {
System.out.println(“High value item!”);
} else {
System.out.println(“Low value item!”);
}
isFilled = filled;
billAmount = cost;
shipping = shippingMethod;
}
public void ship() {
if (isFilled) {
System.out.println(“Shipping”);
} else {
System.out.println(“Order not ready”);
}
is located within the ship method. Both the ship and calculateShipping methods are located within the Order class. It doesn’t matter which method is positioned before the other within the class. Even if a method within the class is defined lower down, the method positioned above it can still call it. Within the class, even if you move the main method above the constructor of Order, your program will still work.
When an instance of the Order class is created, the provided arguments will be assigned to the parameters of the constructor e.g.
Order regOrd = new Order(true, 3.75, "Regular");
The condition in the constructor is
if (cost > 24.00) { ...
The parameter cost has already been assigned a value (3.75 in my example) when the constructor was called with the user provided arguments. So, the condition can be evaluated without any issue. It is your choice whether you want to evaluate the condition first and then make the assignments to the instance fields OR whether you want the make the assignments first and then evaluate the condition.
Suppose, the condition was
if (billAmount > 24.00) { ...
then the assignments to the instance fields must be made before the if statement.