Modify your lab from week 5 (discounts multi-way with formatted output) so that
ID: 3722098 • Letter: M
Question
Modify your lab from week 5 (discounts multi-way with formatted output) so that the user may repeat this as many times as they wish using a sentinel value loop. Use a sentinel value loop (page 166). Use a sales amount of 0 as the sentinel value. Count the transactions, and total the amount due.
I started this but I can't figure out how to correctly use the "while"
import java.util.Scanner;
public class assignment5 {
public static void main(String [] args) {
// Create a Scanner object
Scanner input = new Scanner (System.in);
///Prompt user to enter employees name, hours worked, and hourly pay
System.out.print("Enter employee's first and last name: " );
String firstName = input.next();
String lastName = input.next();
System.out.print("Enter number of hours worked: " );
double hoursWorked = input.nextDouble();
System.out.print("Enter hourly pay rate: " );
double payRate = input.nextDouble();
// Declare variables
double regularPay;
double overtimePay;
double grossPay;
double deductions;
double netPay;
final double federalTax = (.2);
double tax;
//Determine the pay based on hours worked
if (hoursWorked <= 40) {
overtimePay = 0;
regularPay = hoursWorked * payRate;
}
else {
overtimePay = (hoursWorked - 40) * payRate * 1.5;
regularPay = 40 * payRate;
}
//Calculations of gross pay, deductions, and final amount
grossPay = (regularPay + overtimePay);
tax = federalTax * grossPay;
deductions = tax;
netPay = (grossPay - deductions);
//Outputs
System.out.println("Employees name: " + lastName + ", " + firstName );
System.out.printf("Hours worked: %2.2f " , hoursWorked );
System.out.printf("Hourly Rate: %2.2f " , payRate );
System.out.printf("Gross Pay: %2.2f " , grossPay );
System.out.printf("Regular pay: %2.2f/n " , regularPay);
System.out.printf("OvertimePay: %2.2f/n " , overtimePay);
System.out.printf("Deductions: %2.2f " );
System.out.printf("Federal Tax: %2.2f/n " , tax);
System.out.printf("TotalDeductions: %2.2f/n " , tax);
System.out.printf("Net Pay: %2.2f " , netPay );
input.close();
}
}
Explanation / Answer
// actually your program unrelated to question... because in the question sales amount is sentinal value, but in your program there is sales amount. That's why I created end of program as one sentinal value i.e., choice!='n';
import java.util.Scanner;
class assignment5 {
public static void main(String[] args) {
// Create a Scanner object
Scanner input = new Scanner(System.in);
char choice;
do{
///Prompt user to enter employees name, hours worked, and hourly pay
System.out.print("Enter employee's first and last name: ");
String firstName = input.next();
String lastName = input.next();
System.out.print("Enter number of hours worked: ");
double hoursWorked = input.nextDouble();
System.out.print("Enter hourly pay rate: ");
double payRate = input.nextDouble();
// Declare variables
double regularPay;
double overtimePay;
double grossPay;
double deductions;
double netPay;
final double federalTax = (.2);
double tax;
//Determine the pay based on hours worked
if (hoursWorked <= 40) {
overtimePay = 0;
regularPay = hoursWorked * payRate;
} else {
overtimePay = (hoursWorked - 40) * payRate * 1.5;
regularPay = 40 * payRate;
}
//Calculations of gross pay, deductions, and final amount
grossPay = (regularPay + overtimePay);
tax = federalTax * grossPay;
deductions = tax;
netPay = (grossPay - deductions);
//Outputs
System.out.println("Employees name: " + lastName + ", " + firstName);
System.out.printf("Hours worked: %2.2f ", hoursWorked);
System.out.printf("Hourly Rate: %2.2f ", payRate);
System.out.printf("Gross Pay: %2.2f ", grossPay);
System.out.printf("Regular pay: %2.2f ", regularPay);
System.out.printf("OvertimePay: %2.2f ", overtimePay);
System.out.printf("Deductions: %2.2f ", deductions);
System.out.printf("Federal Tax: %2.2f ", tax);
System.out.printf("TotalDeductions: %2.2f ", tax);
System.out.printf("Net Pay: %2.2f ", netPay);
System.out.print(" Do you want to enter again:(y/n)? ");
choice = input.next().charAt(0);
}while(choice!='n'); //choice is sentinal value
input.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.