Could you please anyone explain about this below exercise
https://www.codecademy.com/courses/learn-java/lessons/java-conditionals-and-control-flow/exercises/if-then-else-if
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!”);
}
isFilled = filled;
billAmount = cost;
shipping = shippingMethod;
}
public void ship() {
if (isFilled) {
System.out.println(“Shipping”);
System.out.println("Shipping cost: " + calculateShipping());
} else {
System.out.println(“Order not ready”);
}
}
public double calculateShipping() {
// declare conditional statement here
if (shipping.equals(“Regular”)) {
return 0;
} else if (shipping.equals(“Express”)) {
return 1.75;
} else {
return .50;
}
}
public static void main(String args) {
// do not alter the main method!
Order book = new Order(true, 9.99, “Express”);
Order chemistrySet = new Order(false, 72.50, “Regular”);
book.ship();
chemistrySet.ship();
}
}
O/P:
High value item!
Shipping
Shipping cost: 1.75
Order not ready
What part of the exercise/code are you having difficulty with, and in what way?
about the output could you please explain about method invoking ? I mean which method first invoke?
Could you please format your code according to this guide?
Could you please anyone explain about this below exercise ? about the output could you please explain about method invoking ? I mean which method first invoke?
https://www.codecademy.com/courses/learn-java/lessons/java-conditionals-and-control-flow/exercises/if-then-else-if
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!");
}
isFilled = filled;
billAmount = cost;
shipping = shippingMethod;
}
public void ship() {
if (isFilled) {
System.out.println("Shipping");
System.out.println("Shipping cost: " + calculateShipping());
} else {
System.out.println("Order not ready");
}
}
public double calculateShipping() {
// declare conditional statement here
if (shipping.equals("Regular")) {
return 0;
} else if (shipping.equals("Express")) {
return 1.75;
} else {
return .50;
}
}
public static void main(String[] args) {
// do not alter the main method!
Order book = new Order(true, 9.99, "Express");
Order chemistrySet = new Order(false, 72.50, "Regular");
book.ship();
chemistrySet.ship();
}
}
O/P:
High-value item!
Shipping
Shipping cost: 1.75
Order not ready
Hello! Sorry for the delayed response.
The High-value item!
comes from the constructor function. Notice how it prints out that string when called, so when you create the chemistrySet
object before calling .ship()
on anything, High-value item!
gets logged to the console.
The next two lines come from book.ship()
.
That leaves the last line. I’ll leave you to figure that out (but if you need help, ask!)
1 Like
So this is my understanding correct me if I am wrong:
High-value item!: For this output first, as you told whenever the program starts executing from the main method first it’s come to the constructor and check the condition and print the statement.
Shipping & Shipping cost: 1.75 : For this output first, book object invokes the ship method and check the condition with passing arguments and print the statement.
Order not ready: For this o/p first, chemistrySet object invoke the ship method and check the condition with passing arguments and print the statement.
1 Like
All of those are correct!
1 Like