Home > Software design >  Conditionals and Control flow on Java (codecademy)
Conditionals and Control flow on Java (codecademy)

Time:05-28

I'm just starting to learn Java, and I'm stuck on a section in the conditionals and control flow part of Codecademy.

I'm trying to add a method that manipulates the shippingCost variable if shipped outside of "California". I'm really not sure how to expand on top of that (let me know if there are any questions), but I'm confused as to how to do this, or is it just too advanced for the section I'm on?

This is one of my first posts, so if I'm using any features wrong let me know.

public class Order {
  boolean isFilled;
  double billAmount;
  String shipping;
  
  public Order(boolean filled, double cost, String shippingMethod, int zip) {
        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");
    }
    
    double shippingCost = calculateShipping();
    
    System.out.println("Shipping cost: ");
    System.out.println(shippingCost);
  }
  
  public double calculateShipping() {
    double shippingCost;
    switch (shipping) {
      case "Regular":
        shippingCost = 0;
        break;
      case "Express":    
        shippingCost = 1.75;
        break;
      default:
        shippingCost = .50; 
    }
    return shippingCost;
    }

// customer address
   public void address(int streetNumber, String streetName, String town, String city, String state, int zipCode){
     double shippingCost;
     
     


     if (state != "California"){
       shippingCost  = 10;
     }     

   }
  
  public static void main(String[] args) {
    // create instances and call methods here!

  }
}

For this, I'm getting this message:

Order.java:54: error: variable shippingCost might not have been initialized
       shippingCost  = 10;
       ^
1 error

CodePudding user response:

Try this:

// customer address
   public void address(int streetNumber, String streetName, String town, String city, String state, int zipCode){
     double shippingCost = 0.0f;
     
     

     // if shipping isn't california, add 10 to it
     if (!state.equalsIgnoreCase("California")){
       shippingCost  = 10;
     }     

   }

Now, further things...

The compiler was warning you right, the shipping cost was not initialized. It could have been 0, but that's not guaranteed (hence the warning), so we initialized it to 0.

Furthermore, comparing strings with == will only compare the addresses, address of your string with the address of the "California" literal from the string pool. These will most likely not match, even if both were "California". When using objects (instances of anything that isn't a primitive), you should stick to using .equals() instead, and re-implement (override) that method in any class where equality needs to be different than just comparing addresses of references.

additionally, do try to steer away from shadowing things. the shippingCost in method address() is not the same as shippingCost in method ship(). Yes, they're named the same, but they're both local. However, since your class Order doesn't have a shippingCost member, just because they're named the same doesn't mean that they represent the same value.

In case you added a shippingCost to Order class, your code would break, just because all these methods have a local double shippingCost which shadows the this.shippingCost member of the Order class. So do have that in mind as well.

CodePudding user response:

You should assign a value to that variable.shippingCost. if you add double shippingCost=0; it'll work.

// customer address

public void address(int streetNumber, String streetName, String town, String city, String state, int zipCode){
     double shippingCost = 0;
 
 


 if (state != "California"){
   shippingCost  = 10;
 }     

}

  • Related