In: Computer Science
I'm having trouble understanding this concept. I already completed the first part now I need to convert the Second Part into a Control Structure. Please help answering the Problem. The first part will be below.
(Second Part)
Continuing with Control Structures
Control Structures are called such because they control the execution flow during the running of a program. There are 3 basic control structures: Sequence, Selection and Loop. This week let's work with the structures we already know - Sequence and Selection - but now let's add the loop structure to our logical programming toolbox.
Remember (from CIS 103) that the defining factor of a structured programming logic is that each control structure has exactly 1 entry point and 1 exit point. How do the 'break' and 'continue' statements, used in loop structures, that we learn about in Chapter 4, affect structured programming logic? (2 points, write answer in text box)
Chapter 4 (section 4.1 through 4.9)
Submit SalaryCalcLoop.java
Update SalaryCalc.java (Salary Calculator) from last week to now continue to take input for every employee in a company, and display their information until a sentinel value is entered (pg. 219), that exits the loop and ends the program.
(First Part Already completed)
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");
String ch = "";
//added loop here to take the inputs repetedly
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");
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");
}
//asking user if they want to continue to enter another employee
data
System.out.println("Press Y to continue.Other key to exit ");
ch=sc.next();
} while (ch.equalsIgnoreCase("y"));
}
}
If you need any corrections/clarifications kindly comment.
Program
import java.util.Scanner;
public class SalaryCalcLoop{
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");
String ch = "";
//added loop here to take the inputs repetedly
while(true)
{
System.out.println("Enter Your Name(Type 'quit' to exit):
");
name = sc.next();
if (name.equals("quit"))
{
break; // exit infinite loop
}
System.out.println("Enter Your Shift, Enter 0 for Day, Enter1 for
Night");
System.out.println("0=Day, 1= Night");
shift=sc.nextInt();
System.out.println("Enter Number of Hours Worked: ");
hours = sc.nextDouble();
System.out.println("Enter Hourly Pay: ");
rate = sc.nextDouble();
SalaryCalcLoop c = new SalaryCalcLoop();
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");
}
}
}
}
Output
Pay Calculator
Enter Your Name(Type 'quit' to exit):
yom
Enter Your Shift, Enter 0 for Day, Enter1 for Night
0=Day, 1= Night
0
Enter Number of Hours Worked:
155
Enter Hourly Pay:
100
Calculate Pay
Employee Name: yom
Employee Regular Pay: 4000.0
Employee Overtime Pay: 17250.0
Employee Total Pay: 21250.0
Employee PayPeriod is Friday
Enter Your Name(Type 'quit' to exit):
quit