-4

Hi guys I'm just a beginner in java, I want to override a super class method like this:

public class ShippedOrder extends Order{
    private final int ship = 15;

    public ShippedOrder(String cusName, int cusNo, int qty, double unitPrice, int ship){
        super(cusName, cusNo, qty, unitPrice);
    }

    public void setShip(int ship){
        super.computePrice() + ship;
    }
}

But the message says "+ is not a statement".

kiyah
  • 1,502
  • 2
  • 18
  • 27
Charles
  • 19
  • 1
  • 8

2 Answers2

2

Your code doesn't make any sense:

public void setShip(int ship){
        super.computePrice() + ship;
}

super.computePrice() is a function that either returns something or returns void. You are adding an int to it but you aren't doing anything with it. Assume this function returns 100.0. Then it's equivalent to the line 100.0 + 15; This is not a statement in Java.

I'm assuming you want a ShippedOrder to increase the price of an Order by the value ship. If so, I'd suggest removing the setShip function and just pass unitPrice + ship when you call the Order's constructor as so:

public ShippedOrder(String cusName, int cusNo, int qty, double unitPrice, int ship){
    super(cusName, cusNo, qty, unitPrice+ship);
}

If you don't want to do this, consider keeping a value shipPrice in ShippedOrder and set it in the constructor.

public ShippedOrder(String cusName, int cusNo, int qty, double unitPrice, int ship){
    super(cusName, cusNo, qty, unitPrice);
    this.shipPrice = ship;
}
MFisherKDX
  • 2,840
  • 3
  • 14
  • 25
0

Here is my guess:

In Order class (the parent class), it has computePrice() method. I would assume it is a function which calculates & return price:

// Assume in your "Order" class, you have:
public double computePrice() {
    // whatever calculation here ....
    double price = this.qty * this.unitPrice;

    return price;
}

Then now you have ShippedOrder class, which extends Order class. You added ship as new member variable. If let say you would like to add ship value to the price returned by computePrice(), you can try this:

public class ShippedOrder extends Order {
    // this is declared final, so it's value can only be set once in constructor
    // I would just declare it as double just to follow unitPrice type.
    private final double ship;

    public ShippedOrder (String cusName, int cusNo, int qty, double unitPrice, double ship) {
        super(cusName, cusNo, qty, unitPrice);

        // assign value pass in to member variable
        this.ship = ship;
    }

    @Override
    public double computePrice() {
        return super.computePrice() + this.ship;
    }
}

Then call is this way:

ShippedOrder shippedOrder = new ShippedOrder("MyName", 100, 2, 200.5, 15);
double price = shippedOrder.computePrice();

Hope this helps, good luck!

Shuwn Yuan Tee
  • 5,578
  • 6
  • 28
  • 42