Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Help on project for comp sci class

  1. #1
    isten23 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Exclamation Help on project for comp sci class

    i have been spending a couple of hours on this code for class and i cant get it to compile. any help?

    the point of the program is you enter your name the hours you worked for a week and payrate and the program should do the calculations

    for numbers below 40

    the program should do (payrate * hoursworked) = wage

    for number bewteen 40 and 54

    the program should give same wage for hour 1-40 and 1.5 * payrate for hours above 40 and not beyond 54

    (40 * payrate) + ((hoursworked -40)* (payrate * 1.5))

    and more than 54 hours its double the wage starting after 54

    (40 * payrate)+(14 * (payrate *1.5))+ ((weekly-54)*(payrate*2))


    source code


    Java Code:
    import
    
    java.util.*;
    
    //MAIN METHOD
    public class Grosspay
    {
    public static void main(String args[])
    {
    //STATING
    int rate;
    double weekly, payrate,total, hours;
    String name;
    
    //SCANNER
    Scanner input = new Scanner(System.in);
    //ENTER NAME
    System.out.print("Enter your name: ");
    
    
    //SET INPUT = NAME
    name = input.nextLine();
    
    System.out.print("Hours worked this week: ");
    //SET INPUT = WEEKLY
    weekly = input.nextDouble();
    
    System.out.print("What is your pay rate: ");
    
    
    //SET INPUT = PAYRATE
    payrate = input.nextDouble();
    
    if (weekly <= 40){
     rate = 1;
     }
     
     //RATE 15, LATER MULTIPLY BY .10
    else if(weekly >= 40 && weekly <= 54){
     rate = 15;
    }
    
    else if (weekly >= 54){
    rate = 2;
    }
    
    System.out.print("Calculating..");
    
    if (rate = 1 ) {
    total = (payrate * weekly);
    }
    
    if else ( rate = 15) {
    total = (40 * payrate) + ((weekly - 40)*(payrate*rate);
    }
    
    if else ( rate = 2){
    total = (40 * payrate)+(14* payrate)+(weekly-54)*(payrate * rate);
    }
    
    System.out.println("total is " + total);
    }
    }
    Last edited by sunde887; 03-27-2012 at 01:10 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Help on project for comp sci class

    Unless it was a copying error, you should get into the habit of indenting your code. What is your problem exactly? If you have errors, what are they?

  3. #3
    isten23 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help on project for comp sci class

    ----jGRASP exec: javac -g Grosspay.java

    Grosspay.java:53: error: '(' expected
    if else ( rate = 15) {
    ^
    Grosspay.java:53: error: illegal start of expression
    if else ( rate = 15) {
    ^
    Grosspay.java:53: error: ')' expected
    if else ( rate = 15) {
    ^
    Grosspay.java:53: error: ';' expected
    if else ( rate = 15) {
    ^
    Grosspay.java:54: error: ')' expected
    total = (40 * payrate) + ((weekly - 40)*(payrate*rate);
    ^
    Grosspay.java:57: error: '(' expected
    if else ( rate = 2){
    ^
    Grosspay.java:57: error: illegal start of expression
    if else ( rate = 2){
    ^
    Grosspay.java:57: error: ')' expected
    if else ( rate = 2){
    ^
    Grosspay.java:57: error: ';' expected
    if else ( rate = 2){
    ^
    9 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Help on project for comp sci class

    First, if else isn't a correct statement, I think you are confusing it with else if. Also, rate = x where x is some integer is an assignment and not a comparison.

  5. #5
    isten23 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help on project for comp sci class

    import

    java.util.*;

    //MAIN METHOD
    public class Grosspay
    {
    public static void main(String args[])
    {
    //STATING
    int rate;
    double weekly, payrate,total, hours;
    String name;

    //SCANNER
    Scanner input = new Scanner(System.in);
    //ENTER NAME
    System.out.print("Enter your name: ");


    //SET INPUT = NAME
    name = input.nextLine();

    System.out.print("Hours worked this week: ");
    //SET INPUT = WEEKLY
    weekly = input.nextDouble();

    System.out.print("What is your pay rate: ");


    //SET INPUT = PAYRATE
    payrate = input.nextDouble();

    if (weekly <= 40){
    rate = 1;
    }

    //RATE 15, LATER MULTIPLY BY .10
    else if(weekly >= 40 && weekly <= 54){
    rate = 15;
    }

    else if (weekly >= 54){
    rate = 2;
    }

    System.out.print("Calculating..");

    if ( rate == 1 ) {
    total = (payrate * weekly);

    }

    else if ( rate == 15) {
    total = (40 * payrate) + ((weekly - 40)*(payrate*rate));

    }

    else if ( rate == 2) {
    total = (40 * payrate)+(14* payrate)+(weekly-54)*(payrate * rate);

    }



    System.out.println("Enter Employee name: " + name);
    System.out.println("Enter Hours Worked :" + weekly);
    System.out.println("Enter pay rate :" + payrate);

    System.out.println(name + "worked for " + weekly + "hours with the pay rate of $" + payrate +".The gross pay for " +name + "is" + total);
    }
    }


    this is my new code will you be able to edit it by any chance because I keep adjusting it and keep getting different error messages

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help on project for comp sci class

    Quote Originally Posted by isten23 View Post
    this is my new code will you be able to edit it by any chance because I keep adjusting it and keep getting different error messages
    That's not how it works -- we don't edit code, but we'd be happy to answer any questions. But first --
    • please edit your post above and place [code] [/code] tags around your code so that it retains its formatting and is readable,
    • please post any and all error messages. Else how will we know what you're doing wrong?
    • Indicate which line is causing which error
    • ask specific answerable questions.


    Much luck!
    isten23 likes this.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Help on project for comp sci class

    Also, I'm far too lazy. Again, please provide error messages and you can wrap your code in code tags like this

    [code]YOUR CODE HERE[/code]

  8. #8
    isten23 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Arrow Re: Help on project for comp sci class

    Java Code:
    import
    
    java.util.*;
    
    //MAIN METHOD
    public class Grosspay2
    {
    public static void main(String args[])
    {
    //STATING
    int rate;
    double weekly, payrate,total, hours;
    String name;
    
    //SCANNER
    Scanner input = new Scanner(System.in);
    //ENTER NAME
    System.out.print("Enter your name: ");
    
    
    //SET INPUT = NAME
    name = input.nextLine();
    
    System.out.print("Hours worked this week: ");
    //SET INPUT = WEEKLY
    weekly = input.nextDouble();
    
    System.out.print("What is your pay rate: ");
    
    
    //SET INPUT = PAYRATE
    payrate = input.nextDouble();
    
    if (weekly <= 40){
     rate = 1;
     }
     
     //RATE 15, LATER MULTIPLY BY .10
    else if(weekly >= 40 && weekly <= 54){
     rate = 15;
    }
    
    else if (weekly >= 54){
    rate = 2;
    }
    
    System.out.print("Calculating..");
    
    
    total = (40 * payrate)+(14* payrate)+((weekly-54)*(payrate * rate));
    
    
    
    
    
    System.out.println("Enter Employee name:  " + name);
    System.out.println("Enter Hours Worked :" + weekly);
    System.out.println("Enter pay rate     :" + payrate);
    
    System.out.println(name + "worked for " + weekly + "hours with the pay rate of $" + payrate +".The gross pay for " +name + "is"  + total);
    }
    }
    END OF CODE

    ERROR MESSAGE
    Java Code:
    Grosspay2.java:50: error: variable rate might not have been initialized
    total = (40 * payrate)+(14* payrate)+((weekly-54)*(payrate * rate));
                                                                 ^
    1 error
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.
    The error message says that rate doesn't initialize because of the rate in the line 50. The error message says that rate hasnt initilizaed so im guessing I didnt properley state the if statments which are line 34-46, in these lines what I am trying to do is tell the computer that if weekly is larger than 40 or 54 or below 40, for the computer to assign a specific number to rate so then at the end the number could be the correct one. How would i
    Last edited by Fubarable; 03-27-2012 at 03:28 AM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help on project for comp sci class

    It's complaining that the rate variable may never get set with a value since all of its assignments are done within if blocks, and there is no guarantee that the if blocks will be called. How about giving the rate variable a default value when you declare it?

  10. #10
    isten23 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help on project for comp sci class

    How would i do this? Im not sure i just started coding for school, and arent sure how.

    Quote Originally Posted by fubarable View Post
    it's complaining that the rate variable may never get set with a value since all of its assignments are done within if blocks, and there is no guarantee that the if blocks will be called. How about giving the rate variable a default value when you declare it?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help on project for comp sci class

    Quote Originally Posted by isten23 View Post
    How would i do this? Im not sure i just started coding for school, and arent sure how.
    The key to learning to program is to continually experiment, to play with your code til you get it right. So on that note, please take a guess ... you'll probably be right, and if not, try again, and still if not, show us what you've now got.

  12. #12
    isten23 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help on project for comp sci class

    Yeah ima try switch statments and see if it can be done in that format

  13. #13
    isten23 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help on project for comp sci class

    Java Code:
    import
    
    java.util.*;
    
    //MAIN METHOD
    public class Grosspay2
    {
    public static void main(String args[])
    {
    //STATING
    int rate;
    double weekly, payrate,total = 0;
    String name;
    
    //SCANNER
    Scanner input = new Scanner(System.in);
    //ENTER NAME
    System.out.print("Enter your name: ");
    
    
    //SET INPUT = NAME
    name = input.nextLine();
    
    System.out.print("Hours worked this week: ");
    //SET INPUT = WEEKLY
    weekly = input.nextDouble();
    
    System.out.print("What is your pay rate: ");
    
    
    //SET INPUT = PAYRATE
    payrate = input.nextDouble();
    
    if(weekly >= 54) {
    rate = 2;
    }
    
    
     
     //RATE 15, LATER MULTIPLY BY .10
    else if(weekly >= 40 && weekly <= 54){
     rate = 15;
    }
    
    
    
    else if (weekly < 40){
     rate = 1;
     }
    
    else {
    rate = 0;}
    
    
    
    
    switch (rate) {
    
    case 1: total = weekly * payrate;
    break;
    
    case 15: total = (((weekly - 40)*(payrate * 1.5))+(40 * payrate));
    break;
    
    case 2: total = ((40 * payrate)+(14 * (payrate * 1.5))+((weekly - 54)*( payrate * 2)));
    
    break; 
    
    default: total = 0;
    break;
    }
    
    
    
    //total =(40 * payrate)+ (14 * (payrate * 1.5))+(weekly - 54 * ( payrate *2))
    
    
    System.out.println("Enter Employee name:  " + name);
    System.out.println("Enter Hours Worked :" + weekly);
    System.out.println("Enter pay rate     :" + payrate);
    System.out.println("total     :" + total);
    //System.out.println(name + "worked for " + weekly + "hours with the pay rate of $" + payrate +".The gross pay for " +name + "is");
    
    }}
    I JUST COMPLETE IT!!!!!!!!!!!!!!1



    Quote Originally Posted by Fubarable View Post
    The key to learning to program is to continually experiment, to play with your code til you get it right. So on that note, please take a guess ... you'll probably be right, and if not, try again, and still if not, show us what you've now got.

Similar Threads

  1. Replies: 3
    Last Post: 02-06-2012, 02:03 PM
  2. How should I work on Comp Sci. ?
    By physalis123 in forum Forum Lobby
    Replies: 1
    Last Post: 05-13-2011, 11:59 PM
  3. AP Comp Sci questions??
    By modman15 in forum New To Java
    Replies: 2
    Last Post: 01-17-2010, 03:45 PM
  4. Replies: 3
    Last Post: 03-09-2009, 07:37 AM
  5. get IP from other comp in LAN
    By hungleon88 in forum Networking
    Replies: 2
    Last Post: 09-20-2008, 01:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •