Question

Chapter 4 Loop logic and using loop logic for File Input/0utput. 1. Read Chapter 4 sections...

Chapter 4 Loop logic and using loop logic for File Input/0utput.

1. Read Chapter 4 sections 4.10 until end of chapter.

2. Review Questions and Exercises, short answers.

3. [20 points] submit SalaryCalcLoopFile.java

Let's update our program, SalaryCalcLoop.java, from last week, to now take input about each employee from a file and write their information and salary/pay information to a file.

use input file, where each employee has name,shift,hours worked,hourly rate: EmployeePayroll.txtPreview the document

output file should be named: EmployeePayStubs.txt

EmployeePayroll.txt

Rashid

night

60

20

Chin

day

30

15

Tran

day

45

10

Deeshan

night

55

11.50

Serena

day

24

10

Deepak

day

66

12

Tyrone

night

11

18

Andre

night

80

15

Annotations

SalaryCalcLoop.java

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");
}
}
}
}

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

code is implemented java

Reading from file and writing into file

SalaryCalcLoopFile.java

import java.util.Scanner;
import java.io.*;
public class SalaryCalcLoopFile{
   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) throws Exception{
       Scanner sc = new Scanner(System.in);
       String name;
       int shift = 0;
       Double rate, hours;
       BufferedReader objReader=new BufferedReader(new FileReader("EmployeePayroll.txt"));
       BufferedWriter fw = new BufferedWriter(new FileWriter("EmployeePayStubs.txt"));
       fw.write("Pay Calculator\n");
       String ch = "",strCurrentLine="";
       while ((strCurrentLine = objReader.readLine()) != null) {
           name=strCurrentLine;
           strCurrentLine = objReader.readLine();
           if(strCurrentLine.equals("night")){
               shift=1;
           }else{
               shift=0;
           }
           strCurrentLine = objReader.readLine();
           hours=Double.parseDouble(strCurrentLine);
           strCurrentLine = objReader.readLine();
           rate=Double.parseDouble(strCurrentLine);
           SalaryCalcLoop c = new SalaryCalcLoop();
           c.calPay(hours, rate);
           Double Tpay = c.Rpay + c.Opay;
           fw.write("Calculate Pay");
           fw.write("\n");
           fw.write("Employee Name: " + name);
           fw.write("\n");
           fw.write("Employee Regular Pay: " + c.Rpay);
           fw.write("\n");
           fw.write("Employee Overtime Pay: " + c.Opay);
           fw.write("\n");
           fw.write("Employee Total Pay: " + Tpay);
           fw.write("\n");
           if (shift == 0) {
           fw.write("Employee PayPeriod is Friday");
           } else {
           fw.write("Employee PayPeriod is Saturday");
           }
           fw.write("\n");
       }
      
        fw.close();
   }
}

SalaryCalcLoopFile.java

import java.util.Scanner; import java.io.*; public class SalaryCalcLoopFile{i double Rpay = 0, Opay = 0; void calPay(double h

strCurrentLine = objReader.readLine; hours=Double.parseDouble(strCurrentLine); strCurrentLine = objReader.readLine(); rate=Do

EmployeePayroll.txt

1 Rashid 2 night 3 60 4 20 5 Chin 6 day 7 30 8 15 9 Iran 10 day 11 45 12 10 13 Deeshan 14 night 15 55 16 11.50 17 Serena 18 d

EmployeePayStubs.txt

1 Pay Calculator 2 Calculate Pay 3 Employee Name: Rashid 4 Employee Regular Pay: 800.0 5 Employee Overtime Pay: 600.0 6 Emplo

32 Calculate Pay 33 Employee Name: Deepak 34 Employee Regular Pay: 480.0 35 Employee Overtime Pay: 468.0 36 Employee Total Pa

Add a comment
Know the answer?
Add Answer to:
Chapter 4 Loop logic and using loop logic for File Input/0utput. 1. Read Chapter 4 sections...
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
  • 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...

  • 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...

  • 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 =...

  • (Java) Rewrite the following exercise below to read inputs from a file and write the output...

    (Java) Rewrite the following exercise below to read inputs from a file and write the output of your program in a text file. Ask the user to enter the input filename. Use try-catch when reading the file. Ask the user to enter a text file name to write the output in it. You may use the try-with-resources syntax. An example to get an idea but you need to have your own design: try ( // Create input files Scanner input...

  • C++ Lab 9B Inheritance Class Production Worker Create a project C2010Lab9b; add a source file Lab...

    C++ Lab 9B Inheritance Class Production Worker Create a project C2010Lab9b; add a source file Lab9b.cpp to the project. Copy and paste the code is listed below: Next, write a class named ProductionWorker that is derived from the Employee class. The ProductionWorker class should have member variables to hold the following information: Shift (an integer) Hourly pay rate (a double) // Specification file for the ProductionWorker Class #ifndef PRODUCTION_WORKER_H #define PRODUCTION_WORKER_H #include "Employee.h" #include <string> using namespace std; class ProductionWorker...

  • 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 =...

  • Java Programming Language Edit and modify from the given code Perform the exact same logic, except...

    Java Programming Language Edit and modify from the given code Perform the exact same logic, except . . . The numeric ranges and corresponding letter grade will be stored in a file. Need error checking for: Being able to open the text file. The data makes sense: 1 text line of data would be 100 = A. Read in all of the numeric grades and letter grades and stored them into an array or arraylist. Then do the same logic....

  • Logic Exercise 40 Points This exercise tests your ability to understand logic in the form of...

    Logic Exercise 40 Points This exercise tests your ability to understand logic in the form of IF Statements. You will be given the rules and the data that will follow the logic. At the end of the explanation, a series of questions will be asked. Place your answers in the area provided. Program explanation - You are provided with pseudocode that calculates the total amount of money to pay an employee after working for 1 week. The amount of money...

  • Correct the five syntax, run-time or logic errors that are found in the CountAndAverageNumbers.java file. Link...

    Correct the five syntax, run-time or logic errors that are found in the CountAndAverageNumbers.java file. Link to the file: CountAndAverageNumbers.javaPreview the document. Make sure you include comments in your code where errors were corrected. If you do not flag each error with a comment, points will be deducted. Upload the corrected .java file here. The output of the corrected file looks like the following: Debug4Output.PNG This program reads an unspecified number of integers, determines how many positive and negative values...

  • Java Programming Reading from a Text File Write a Java program that will use an object...

    Java Programming Reading from a Text File Write a Java program that will use an object of the Scanner class to read employee payroll data from a text file The Text file payroll.dat has the following: 100 Washington Marx Jung Darwin George 40 200 300 400 Kar Car Charles 50 22.532 15 30 The order of the data and its types stored in the file are as follows: Data EmployeelD Last name FirstName HoursWorked double HourlyRate Data Tvpe String String...

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