Chaos Project

General => Electronic and Computer Section => Programming / Scripting / Web => Topic started by: WhiteRose on June 23, 2010, 12:41:49 pm

Title: A quick Java question
Post by: WhiteRose on June 23, 2010, 12:41:49 pm
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?
Title: Re: A quick Java question
Post by: 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.  where you have

double overtimePay;


you need

double overtimePay = 0;
Title: Re: A quick Java question
Post by: Diokatsu on June 23, 2010, 08:56:49 pm
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
Title: Re: A quick Java question
Post by: winkio on June 23, 2010, 09:26:59 pm
Java may just have the most unforgiving compiler of all time.
Title: Re: A quick Java question
Post by: WhiteRose on June 24, 2010, 12:20:53 am
Thanks winkio! I knew it was something simple like that.