Question

I have a Java question. (I'm using BlueJ.) I'm trying to program something that calculates pay...

I have a Java question. (I'm using BlueJ.) I'm trying to program something that calculates pay roll. Most of it works, but I've identified two parts that don't: It labels all salaries as "Fulltime" no matter how many hours worked I input, and the do/while loop does something weird.

The if/else statement I have for the fulltime vs part time is:

if (hoursWorked < 40.0) {
position = partTime;
} else if (hoursWorked >= 40.0 || hoursWorked <= 168.0) {
position = fullTime;
} else if (hoursWorked > 168.0) {
System.out.println("Error. Please try again.");
}

And the do/while loop ends with:

} while (employeeName != "Exit");

When the loop comes back around, it prints:

Employee name: Base pay rate:

And then fails no matter what I put. For that part I have:

System.out.print("Employee name: ");
employeeName = read.nextLine();
System.out.print("Base hourly pay: ");
baseSalaryPay = read.nextDouble();
System.out.print("Hours worked: ");
hoursWorked = read.nextDouble();

What do I need to fix?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

There are two major fix here:

  1. The != operator,  In java will tell whether two objects are equal or not.
    here in your case, you need to use .equals function of string class. So the while part will be

    } while (!employeeName.equals("Exit"));

  2. The 2nd problem is your else if condition which will always be true

    else if (hoursWorked >= 40.0 || hoursWorked <= 168.0)

    Take few example,
    1.  hoursWorked = 10 =>> (hoursWorked >= 40.0) = false, (hoursWorked <= 168.0) = true, so false || true = true, so result is true

    2.  hoursWorked = 120 =>> (hoursWorked >= 40.0) = true, (hoursWorked <= 168.0) = true, so true || true = true, so result is true

    3.  hoursWorked = 230 =>> (hoursWorked >= 40.0) = true, (hoursWorked <= 168.0) = false, so true || false = true, so result is true


    So the problem is with you condtion you need to change it to && instead of ||, Like
    else

    if (hoursWorked >= 40.0 && hoursWorked <= 168.0)

Note: please share the complete code next time

Please feel free to ask if you have any doubt and share the feedback too.

Add a comment
Know the answer?
Add Answer to:
I have a Java question. (I'm using BlueJ.) I'm trying to program something that calculates pay...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Must be written in java. Modularize the following code. //CIS 183 Lab 4 //Joseph Yousef import...

    Must be written in java. Modularize the following code. //CIS 183 Lab 4 //Joseph Yousef import java.util.*; public class SalaryCalc { public static void main(String [] args) { Scanner input = new Scanner(System.in); int sentinelValue = 1; //initializes sentinelValue value to 1 to be used in while loop while (sentinelValue == 1) { System.out.println("Enter 1 to enter employee, or enter 2 to end process: ");    sentinelValue = input.nextInt(); input.nextLine(); System.out.println("enter employee name: "); String employeeName = input.nextLine(); System.out.println("enter day...

  • I need this java program to contain methods that do some of the work that can...

    I need this java program to contain methods that do some of the work that can be modularized. It must logically be the same as this loop program but instead the logic will be modularized into Java Methods. Continue to take input for every employee in a company, and display their information until a sentinel value is entered, that exits the loop and ends the program. import java.util.Scanner; public class SalaryCalc { double Rpay = 0, Opay = 0; void...

  • My Java code from last assignment: Previous Java code: public static void main(String args[]) { //...

    My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...

  • My Java code from last assignment: Previous Java code: public static void main(String args[]) { //...

    My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...

  • the question just that. here is what i have done, put something wrong with it Week...

    the question just that. here is what i have done, put something wrong with it Week 5- Counts using While or Do-While Loop Write a program that asks the user to input 6 integer numbers, then the program counts the number of negative numbers, the number of positive numbers and counts the number of zeros. Use 'while' loop or 'Do-While loop in your program. Make sure to include comments in the program. Example, if the input numbers are: -23 Then,...

  • Hello, I am trying to get this Array to show the summary of NETPAY but have...

    Hello, I am trying to get this Array to show the summary of NETPAY but have been failing. What is wrong? #include <iostream> #include <iomanip> using namespace std; main(){ char empid[ 100 ][ 12 ];    char fname[ 100 ][ 14 ], lastname[ 100 ][ 15 ];    int sum; int hw[ 100 ]; double gp[ 100 ], np[ 100 ], hr[ 100 ], taxrate[100], taxamt[ 100 ]; int counter = 0; int i; cout<<"ENTER EMP ID, FNAME, LNAME, HRS...

  • THIS IS WHAT I HAVE I NEED TO FIX IT SO: hours_worked should not be inputed...

    THIS IS WHAT I HAVE I NEED TO FIX IT SO: hours_worked should not be inputed separately. Get the hours as 40 40 40 40 or 40, 40, 40, 40 whichever separator suits you. How do I do this? How would you change what I have in to it getting into that kind of input. Thank you! class Employee: again = 'y' def __init__(self): self.__rate = 7.25 self.__totalhour = 0 self.__regularpay = 0 self.__overtimepay = 0 self.__totalpay = 0 self.__tax...

  • Please include function in this program Write a C program that will calculate the gross pay...

    Please include function in this program Write a C program that will calculate the gross pay of a set of employees utilizing pointers instead of array references. You program should continue to use all the features from past homeworks (functions, structures, constants, strings). The program should prompt the user to enter the number of hours each employee worked. When prompted, key in the hours shown below. The program determines the overtime hours (anything over 40 hours), the gross pay and...

  • I need help making this work correctly. I'm trying to do an array but it is...

    I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...

  • Hello! I'm posting this program that is partially completed if someone can help me out, I...

    Hello! I'm posting this program that is partially completed if someone can help me out, I will give you a good rating! Thanks, // You are given a partially completed program that creates a list of employees, like employees' record. // Each record has this information: employee's name, supervisors's name, department of the employee, room number. // The struct 'employeeRecord' holds information of one employee. Department is enum type. // An array of structs called 'list' is made to hold...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT