A quick Java question

Started by WhiteRose, June 23, 2010, 12:41:49 pm

Previous topic - Next topic

WhiteRose

I'm trying to complete this assignment:
Spoiler: ShowHide
Assignment: FooCorporation
Method to print pay based on base pay andhours worked
Overtime: More than 40 hours, paid 1.5 times base pay
Minimum Wage: $8.00/hour
Maximum Work: 60 hours a week


Here's what I have written:

package printpay;

/**
*
* @author Ashley
*/
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void calculatePay(int x){
        int baseWage = 8;
        double overtimePay;
        if (x>60){
            x=60;
        }
        if(x>40){
            int overtimeHours = x-40;
            overtimePay = overtimeHours*1.5*baseWage;
            x = 40;
        }
        double regularPay = x*8;
        double totalPay = regularPay+overtimePay;
        System.out.println(totalPay);
    }
    public static void main(String[] args) {
        int hoursWorked = 55;
        calculatePay(hoursWorked);
    }

}


However, in the line where I combine the overtime pay and the standard pay, I get an error that the overtime pay variable might not be initialized. I think this has something to do with scope of variables. Could someone please help me out?

winkio

you need to initialize overtime pay to 0, because you are using Java, and it's like that.  where you have

double overtimePay;


you need

double overtimePay = 0;

Diokatsu

Quote from: winkio on June 23, 2010, 07:41:52 pm
you need to initialize overtime pay to 0, because you are using Java, and it's like that.


Oh god I lol'd

winkio

Java may just have the most unforgiving compiler of all time.

WhiteRose

Thanks winkio! I knew it was something simple like that.