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
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);
}
}
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?
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.
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.
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
Quote:
Originally Posted by
isten23
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!
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]
Re: Help on project for comp sci class
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
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
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?
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
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?
Re: Help on project for comp sci class
Quote:
Originally Posted by
isten23
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.
Re: Help on project for comp sci class
Yeah ima try switch statments and see if it can be done in that format
Re: Help on project for comp sci class
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
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.