In: Computer Science
Updating the following Java program below, I need to 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 calPay(double hours, double rate)
{
if(hours<=40)
{
Rpay = hours * rate;
Opay = 0;
}
else
{
double Rhr,Ohr;
Rhr = 40;
Ohr = hours-Rhr;
Rpay = Rhr * rate;
Opay = Ohr * (1.5*rate);
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String name;
int shift = 0;
Double rate, hours;
System.out.println("Pay Calculator");
System.out.println("Enter Your Name");
name=sc.next();
System.out.println("Enter Your Shift, Enter 0 for Day, Enter1 for
Night");
System.out.println("0=Day, 1= Night");
System.out.println("Enter Number of Hours Worked");
hours=sc.nextDouble();
System.out.println("Enter Hourly Pay");
rate=sc.nextDouble();
SalaryCalc c= new SalaryCalc();
c.calPay(hours,rate);
Double Tpay= c.Rpay+ c.Opay;
System.out.println();
System.out.println("Calculate Pay");
System.out.println("Employee Name: "+name);
System.out.println("Employee Regular Pay: "+c.Rpay);
System.out.println("Employee Overtime Pay: "+c.Opay);
System.out.println("Employee Total Pay: "+Tpay);
if(shift==0)
{
System.out.println("Employee PayPeriod is Friday");
}
else
{
System.out.println("Employee PayPeriod is Saturday");
}
}
}
Program screenshots:
Sample Output:
Code to Copy:
import java.util.Scanner;
public class SalaryCalc {
double Rpay = 0, Opay = 0;
void calPay(double hours, double rate) {
if (hours <= 40) {
Rpay = hours *
rate;
Opay = 0;
} else {
double Rhr,
Ohr;
Rhr = 40;
Ohr = hours -
Rhr;
Rpay = Rhr *
rate;
Opay = Ohr *
(1.5 * rate);
}
}
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
String name;
int shift = 0;
Double rate, hours;
System.out.println("Pay
Calculator");
// Declare the variable
of string type.
String ch = "";
// start the do while
loop
do {
System.out.println("Enter Your Name");
name =
sc.next();
System.out.println("Enter Your Shift, Enter 0 for Day, Enter1 for
Night");
System.out.println("0=Day, 1= Night");
// get the
value of shift.
shift =
sc.nextInt();
System.out.println("Enter Number of Hours Worked");
hours =
sc.nextDouble();
System.out.println("Enter Hourly Pay");
rate =
sc.nextDouble();
SalaryCalc c =
new SalaryCalc();
c.calPay(hours,
rate);
Double Tpay =
c.Rpay + c.Opay;
System.out.println();
System.out.println("Calculate Pay");
System.out.println("Employee Name: " + name);
System.out.println("Employee Regular Pay: " + c.Rpay);
System.out.println("Employee Overtime Pay: " + c.Opay);
System.out.println("Employee Total Pay: " + Tpay);
if (shift == 0)
{
System.out.println("Employee PayPeriod is
Friday");
} else {
System.out.println("Employee PayPeriod is
Saturday");
}
// get the
value from the user(sentinel value.)
System.out.println("Press Y to continue.Other key to exit ");
ch =
sc.next();
// check the
sentinel value.
} while
(ch.equalsIgnoreCase("y"));
}
}