0
public class Change{
    public static void Change(double salesTotal, double customerPayment){

        //Bill:a$130,b$55,c$25,d$5,e$1
        //f75cents,g30cents,h1cents
        double a,b,c,d,e,f,g,h;
        a = b = c = d = e = f = g = h = 0;
        double change = customerPayment - salesTotal;
        if(change < 0){
            IO.reportBadInput();
            IO.outputIntAnswer(-1);
            return;
        }
        if(change >= 130){
            a = Math.floor(change / 130);
            change = change - a * 130;
        }
        if(change >= 55){
            b = Math.floor(change / 55);
            change = change - b * 55;
        }
        if(change >= 25){
            c = Math.floor(change / 25);
            change = change - c * 25;
        }
        if(change >= 5){
            d = Math.floor(change / 5);
            change = change - d * 5;
        }
        if(change >= 1){
            e = Math.floor(change / 1);
            change = change - e;
        }
        if(change >= 0.75){
            f = Math.floor(change / 0.75);
            change = change - 0.75 * f;
        }
        if(change >= 0.30){
            g = Math.floor(change / 0.30);
            change = change - 0.30 * g;
        }
        if(change >= 0.01){
            h = Math.floor(change / 0.01);
            change = change - 0.01 * h;
        }
        IO.outputDoubleAnswer(a);
        IO.outputDoubleAnswer(b);
        IO.outputDoubleAnswer(c);
        IO.outputDoubleAnswer(d);
        IO.outputDoubleAnswer(e);
        IO.outputDoubleAnswer(f);
        IO.outputDoubleAnswer(g);
        IO.outputDoubleAnswer(h);
        }
    }
}

I've just started to learn programming this year so this is a noob question. My assignment asks me to develop a method that computes sales change. I've tested this in another class using Change.Change(); and got correct results. But my instructor says that I have an upcast in this method. After googling I had no idea what went wrong. If I do not put this method in a class, how should I call it from another class? Thanks in advance.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Alex H
  • 1
  • 4
  • 4
    I don't see any casts in this code. – Oliver Charlesworth Mar 03 '18 at 19:46
  • 4
    You should ask your instructor what they mean. A statement like `double a = 0;` could possibly be said to have an upcast (from `int` to `double`), because the type of 0 is `int`, but I doubt that's what they are referring to. – Radiodef Mar 03 '18 at 19:50
  • I don't see any casts in your code, only conversions. You are using widening conversions when asigning an `int` to a `double` as in you code. – Uwe Plonus Mar 03 '18 at 19:54
  • @UwePlonus The compiler doesn't insert any widening conversions when it comes to integer literals. They're inferred to be `double` at compile-time. – 4castle Mar 03 '18 at 19:56
  • How are you calling `Change.Change()` **exactly** (perhaps the upcasts are there)? Also, `-=` and `+=` are not ***evil*** (but naming your method and your class the same is *annoying*, even if it is not actually *malicious*). – Elliott Frisch Mar 03 '18 at 19:57
  • @4castle: Do you have a reference for this? – Uwe Plonus Mar 03 '18 at 20:04
  • I probably should set all those int to double as I'm using Math.floor(); . Is that the problem? – Alex H Mar 03 '18 at 20:09
  • 3
    We can't know the problem. You've said the professor says it's an upcast, but there isn't one here. And even if there were, upcasts are always safe to perform -- so we can't know why your professor doesn't want them. You should ask the professor. – yshavit Mar 03 '18 at 20:16
  • @UwePlonus [This answer](https://stackoverflow.com/a/2170876/5743988) describes how upcasts are implemented at compile-time and have no overhead. Since the value of an integer literal is known at compile-time, it's a trivial optimization to remove the upcast operation completely. – 4castle Mar 03 '18 at 20:22
  • @AlexH `Math.floor` returns a `double`, so it wouldn't be that. – 4castle Mar 03 '18 at 20:25
  • Without reference to your code, there isn’t necessarily anything wrong with an upcast. It’s one of the means of polymorphism, which is generally seen as a good thing (when used right). – Ole V.V. Mar 03 '18 at 20:38
  • I believe your variables `a`, `b`, `c`, etc. could be `int` rather than `double`.The comment is really off-topic because strictly speaking it hasn’t got anything to do with any upcast. I am also speculating, though, whether this was what your professor meant, or not (poorly expressed if it was). – Ole V.V. Mar 04 '18 at 08:13

0 Answers0