• Post Reply Bookmark Topic Watch Topic
  • New Topic

Return statement missing  RSS feed

 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is do i get an error that says missing return statement? i see my return statement. Does it have something to do with my return type?
hours is an int
baseRate is a double
overtimeMultiplier is a double

 
Stephan van Hulst
Saloon Keeper
Posts: 7972
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what happens when I call baseWage() and hours is greater than 40?
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then it should be overtime.
So im missing an else, correct?  But then what about the second method, thats where i would implement overtime.
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rick pine wrote:Then it should be overtime.


No. What's my base wage if I work 65 hours?

So im missing an else, correct?  But then what about the second method, thats where i would implement overtime.


Correct, you need an else in getBaseWage(). As for getOvertimeWage(): what's my overtime wage if I work 25 hours? You need an else there.
 
Stephan van Hulst
Saloon Keeper
Posts: 7972
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say I'm going to write a piece of code that uses yours, sets hours to 45, calls baseWage() anyway. What result should I expect?
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule is that your method returns something. Whichever way the code runs, you must return something. It doesn't matter to the javac tool what you return, as long as you return something which matches the type declared for that method. You must be able to use a method like this:-
SomeType myVariable = someMethod(...);
and be sure that myVariable will be assigned something. That means, in your method, it must return SomeType. If your method about wages says it returns something, then it commits itself to return that something under all circumstances. So every possible path through the method must return that something (or throw an Exception).

I you work a 40‑hour week, you will have base wages up to 40 hours and overtime thereafter. So your base wage method shou‍ld return 40 hours' wages if you work > 40, and your overtime method shou‍ld return 0 if you work < 40. If you work 45 hours at $12 per hour, you will earn 40×12 + 5×12×1½ if your overtime is at time‑and‑a‑half.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify, i only need to worry about a 40 hour week. After 40 it is overtime, one more thing, overtimeMultiplier is defaulted to 1.5. That is the reason why i dont have an else statement, and that is also confusing. what am'I supposed to tell my baseWage method to do, if hours > 40.
Also on the overtime method, i understand this calculation "you will earn 40×12 + 5×12×1½ if your overtime is at time‑and‑a‑half." from Mr. Campbell.

I have told my method, " hey, if (hours > 40) return this calculation.
            return (hours - 40) * (hours * overtimeMultiplier);40 then take whats after 40 hours and do the math. like this.

i also have a method that would return a total, by adding basewage + overtime.

im sorry if im rambling incoherently.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rick pine wrote:J That is the reason why i dont have an else statement, and that is also confusing. what am'I supposed to tell my baseWage method to do, if hours > 40.

i just answered my own question by doing this
Please tell me if thats also wrong (i will jump off my third floor window)
    but this one is still not loving me
[edit]CR corrected code tag error.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rick pine wrote:Just to clarify, i only need to worry about a 40 hour week. After 40 it is overtime, one more thing, overtimeMultiplier is defaulted to 1.5. That is the reason why i dont have an else statement, and that is also confusing. what am'I supposed to tell my baseWage method to do, if hours > 40.


Well, that's totally fine... but ... you also have to follow the rules of the programming language too.

In Java, when you declare a method to return a value, it needs to return the value, regardless of whether the rest of the application "needs" it or not. Your baseWage() is declared to return a double. It needs to always return a double*.

Henry

* or throw an exception, but you probably haven't learned that yet.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:. Your baseWage() is declared to return a double. It needs to always return a double*.

Henry

* or throw an exception, but you probably haven't learned that yet.

I have an execption thrown but only if the value of baseRate < 7.50 (min wage) or if the hours < 0;
But with that aside, i thought i was returning a double because , baseRate * hours can be,  lets say 7.50 * 3 = 22.5
is 22.5 not a double?
oh crap! is it afloat? 

also , why is do i get the good ol' missing return statement on the second method



By the way thanks a million for answering my questions, you guys are more helpful than my teacher.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rick pine wrote:
also , why is do i get the good ol' missing return statement on the second method

wrong method, i rewrote it.


   
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • If hours is equal to 40, what happens?
  • Do you even need the second if statement?
  •  
    rick pine
    Ranch Hand
    Posts: 90
    Java Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    correct i should have said hours <= 40
    int regards to the second if:
    its the only way i could think of saying if hours <= 40, return 0
    and if hours > 40 then take the amount of hours, multiply it by base and to the rest of the calculation.
     
    rick pine
    Ranch Hand
    Posts: 90
    Java Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    rick pine wrote:
    its the only way i could think of saying if hours <= 40, return 0

    I think i got it!!
     
    Liutauras Vilda
    Sheriff
    Posts: 4917
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Try not to avoid opening and closing braces {} (even if there is only one statement within the body). It just isn't recommended.

    Recommended:
    or

     
    Liutauras Vilda
    Sheriff
    Posts: 4917
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Can you notice what is wrong with this code snippet?
     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    rick pine wrote:. . . I have an execption thrown but only if the value of baseRate < 7.50 (min wage) or if the hours < 0 . . .
    What sort of Exception? IllegalArgumentException? That looks like good practice to me

    I can suggest an improvement to your code: avoid magic numbers. The number 40 appears in your code with no explanation of what it means. What is going to happen if you change to a 37½ hour week? Create a constantNote that I added braces to your if statement; you shoiu‍ld always use those braces. [edit]I see Liutauras has already mentioned braces.[/edit]

    If you ever declare a public constant which is a compile time constant, you must make it keep the same value for ever, otherwise you will break code using it:-
     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    rick pine wrote:. . . oh crap! is it afloat? . . .
    Resisting the temptation to say silly things about the spelling error, no, it is not a float. In order for it to be a float, you would have to cast all doubles involved to floats and mark any double literals used as floats with a final f, e.g 22.5f or 22.5F. Unlike the situation about longs, it doesn't matter whether you write f or F.
    Actually, if you pass a float as an argument to a method requiring a double, or return a float from a method returning a double, in both cases the float will undergo a widening conversion and become a double. By the way: don't use floats if you can possibly help it. If you must do floating‑point arithmetic use doubles. If you are calculating anything requiring precision (especially money) use integer arithmetic or this.
     
    rick pine
    Ranch Hand
    Posts: 90
    Java Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks for all the replies guys, i figured this one out, well to a point that it does what i need it to do.
    also the exception i have set up goes like this. 
    public WageCalculator(double baseRate, double overtimeMultiplier, int hours) {
            if (baseRate < 7.50 || hours <= 0) {
                throw new IllegalArgumentException();
            }

    This question can be closed.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!