Write a program to compute an employee’s weekly pay and produce a pay slip showing name, gross pay (i.e. pay before deductions), deductions, and net pay (i.e., pay after deductions). The program should first prompt the user for: a) family (last) name b) given (first) name c) hourly rate of pay d) number of hours worked that week (any hours over 40 are paid at double the normal rate) e) a letter indicating the employee’s tax category
M4thM1nd
@M4thM1nd
what you have done so far?
public static void main(String[]args) { System.out.printIn("What is your last name?"); String lastName = In.getString(); System.out.printIn("What is your first name?"); String fristName = In.getString(); System.out.printIn("What is your hourly rate of pay?"); double hourlyRate = In.getdouble(); System.out.printIn("How many hours did you work this week?"); double hoursWorked = In.getdouble(); } int hoursWorkedOverForty = 0; if(weekly_pay_rate > 40) { weekly_pay_rate = weekly_pay_rate * 40; hoursWorkedOverForty = weekly_pay_rate - 40; //finds extra hrs worked weekly_pay_rate = hoursWorked OverForty * (2 * hourlyRate); //above line will add extra hours at double the normal payrate } else { //this else statement will be if you work below or equal to 40 hours weekly_pay_rate = hoursWorked * hourlyRate; } letter = In.getChar(); switch (letter){ case 'A..a': System.out.println("There is no tax deduction."); break; case 'B..b': System.out.println("Tax is 10% of gross pay."); break; case 'C..c': System.out.println("Tax is 20% of gross pay."); break; case 'D..d': System.out.println("Tax is 29% of gross pay."); break; case 'E..e': System.out.println("Tax is 35% of gross pay."); break; } System.out.println{("Do want $20 from your payment?";) int
what is going wrong with teh code?
it won't compile
and I am not sure how to add in more code to do the tax deductions and the 20 dollar charity part
i'll try to run it here
could you please post the code in pastebin .com?
ok I've posted it
you have to use System.out.println()
(PRINTLN not PRINTIN)
Print LN? why is that
oh oops, yeah that was an error
You have to write if l not with i
it*
but I think there may be a problem with the program itself
as well
yes
there is not method such as String ln.getString();
i don't actually remember how to receive a string from the keyboard
no problem, thanks for your help
I think I may need to use if else statements to do the tax deductions as well.
yes! you have to create a conditional flux for this problem
i recomend you to seach for it in the java reference
i personally don't like too much of java
Yeah it can be a lot of work.
if i remember well, the process of making the program receive a string is quite boring
you can easily do these things in C/C++
yeah java is just too much code
yes! can you choose the language for this project our you must use java?
i must use java. :(
hello.
I know quite well how to get input from the keyboard-- either with: import javax.swing.JOptionPane; or import java.util.Scanner; Both can read in strings from the user- scanner can read in from what you type in the console- and JOptionPane can read in from what you type in a window box. Which one do you want to learn?
JOptionPane is more gui, probably cooler than scanner.. but scanner might be easier.
Thanks. I fixed my code but I am still getting 1 error. "cannot be resolved to a variable" Here is my code: import java.io.*; import java.text.*; public class part { static InputStreamReader r = new InputStreamReader(System.in); static BufferedReader br = new BufferedReader(r); // Read a String from standard system input public static String getString() { try { return br.readLine(); } catch (Exception e) { return ""; } } // Read a Number as a String from standard system input // Return the Number public static Number getNumber() { String numberString = getString(); try { numberString = numberString.trim().toUpperCase(); return NumberFormat.getInstance().parse(numberString ); } catch (Exception e) { // If any exception occurs, just return zero return new Integer(0); } } // Read an int from standard system input public static int getInt () { return getNumber().intValue(); } // Read a long from standard system input public static long getLong () { return getNumber().longValue(); } // Read a float from standard system input public static float getFloat () { return getNumber().floatValue(); } // Read a double from standard system input public static double getDouble () { return getNumber().doubleValue(); } // Read a char from standard system input public static char getChar () { String s = getString(); if (s.length() >= 1) return s.charAt(0); else return '\n'; } public static void main(String[]args) { // Declare Variables double hoursWorked; double grossPay; double hours; double hourlyPayRate; hoursworked = 0; // Get the hours worked and the hourly pay rate System.out.print("How many hours did you work? "); System.out.print("How much do you get paid per hour? "); // Calculate gross pay if (hoursWorked <= 40) { grossPay = hoursWorked * hourlyPayRate; } else { grossPay = (hoursWorked) * (1.5 * hourlyPayRate); } // Display the results System.out.println("You earned $" + grossPay); } }
It is saying that hoursWorked cannot be resolved to a variable.
java is a case sensitive language, you left the w lower-case when you did hoursworked = 0 -- it's declared as double hoursWorked; small thing
Thanks again!
ya np. You probably have other errors because you didn't initialize the variables up top.. if it still doesn't work try this: double hoursWorked = 0; double grossPay = 0; double hours = 0; double hourlyPayRate = 0; hoursWorked = 0;
it's a finicky thing-- the compiler is awfully sensitive lol.
yeah, one more thing, when doing the switch statements, the program needs to be able to respond to either a case of upper case A AND lower case a, so would it work if I went: case 'A..a' for each case?
I'd guess not.. but I have a way to get around that obstacle. if you want to do a switch statement which operates a case when the value is multiple things-- I do this: char chap = 'a'; switch(chap) { case 'B': I'm a statement; break; case 'A': case 'a': I'm a statement; break; default: I'm a statement; //default executes if none of the other cases are true break; } if chap = 'B' the switch statement will execute case B then skip the rest of the statement. if chap = a, or chap = A, it will execute the statements under case A & case a: this can be done in different ways too-
Here is a program that is self-explanatory in working syntax: int aNumberBetween1and10 = 6; switch(aNumberBetween1and10) { case 1: case 2: case 3: case 4: case 5: System.out.println("I am a number between 1 and 5."); break; case 6: case 7: case 8: case 9: System.out.println("I am a number between 6 and 9."); break; case 10: System.out.println("I'm 10."); break; default: System.out.println("I am not a number between 1 and 10."); break; }
That makes great sense thanks :)
np, let me know if you need help with keyboard input.
though if you have certain statements execute in a large range- if-else-if statements are better than switch.
Just another question, I am trying to make it so that my program asks the user for a bunch of info and then returns it all at the end, how would I write the return values, would I be doing them after the print statements?
yea, say I have 3 string variables that the user entered, String str1, str2, str3; Let's say the user inputs as follows: str1 = "James Donson"; str2 = "42 years old"; str3 = "I like Taco Bell"; Now if I want to print them, I put them inside a print or println method. System.out.println("Name: " + str1 + "."); System.out.println("Age: " + str2 + "."); System.out.println("Favorite Restaurant: " + str3 + "."); This will output the following:::: Name: James Donson. Age: 42 years old. Favorite Restaurant: I like Taco Bell. Alternatively, I'll show you what happens if you do the print instead of println method:: System.out.print("Name: " + str1 + "."); System.out.print("Age: " + str2 + "."); System.out.print("Favorite Restaurant: " + str3 + "."); This will print: Name: James Donson.Age: 42 years old.Favorite Restaurant: I like Taco Bell. println will output a line, then automatically move to the next one, while print will continue on the same line. Wherever I had string variables, you can exchange them with int values ect.
Join our real-time social learning platform and learn together with your friends!