Results 1 to 13 of 13
- 03-27-2012, 12:37 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
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.
- 03-27-2012, 01:11 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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?
- 03-27-2012, 01:46 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
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.
- 03-27-2012, 02:26 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 03-27-2012, 02:37 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
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
-
Re: Help on project for comp sci class
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!
- 03-27-2012, 02:47 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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]
- 03-27-2012, 02:50 AM #8
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
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 MESSAGEThe 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 iJava 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.Last edited by Fubarable; 03-27-2012 at 03:28 AM.
-
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?
- 03-27-2012, 03:40 AM #10
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
-
Re: Help on project for comp sci class
- 03-27-2012, 03:49 AM #12
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
Re: Help on project for comp sci class
Yeah ima try switch statments and see if it can be done in that format
- 03-27-2012, 06:05 AM #13
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
Re: Help on project for comp sci class
I JUST COMPLETE IT!!!!!!!!!!!!!!1Java 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"); }}
Similar Threads
-
What did I do wrong? [Run Time Exception Error] High School Comp. Sci
By Calaminh in forum New To JavaReplies: 3Last Post: 02-06-2012, 02:03 PM -
How should I work on Comp Sci. ?
By physalis123 in forum Forum LobbyReplies: 1Last Post: 05-13-2011, 11:59 PM -
AP Comp Sci questions??
By modman15 in forum New To JavaReplies: 2Last Post: 01-17-2010, 03:45 PM -
How to add new row from JTable comp in netbeans GUI?
By zam in forum NetBeansReplies: 3Last Post: 03-09-2009, 07:37 AM -
get IP from other comp in LAN
By hungleon88 in forum NetworkingReplies: 2Last Post: 09-20-2008, 01:21 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks