(Java)
Rewrite the following exercise below to read inputs from a file and write the output of your program in a text file.
An example to get an idea but you need to have your own design:
try ( // Create input files Scanner input = new Scanner(sourceFile); ) { while (input.hasNext()) { String s1 = input.nextLine(); }
try ( // Create output files PrintWriter output = new PrintWriter(targetFile); ) { output.println(s2); }
or try { } catch (IOException ex) { System.out.println("IO Errors! Can not find the file to read!"); }
and...
Rewrite the following exercise to check the user inputs to be:
and
with using try-catch (and/or throw Exception )
The exercise: Write a program to prompt the user for hours and rate per hour to compute gross pay. Calculate your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours.
Your code should have at least the following methods to calculate the pay. (VOID and NON-STATIC)
Create an object of the class and declare the method as the instance methods.
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
475 = 40 * 10 + 5 * 10*1.5
/*
Your result
*/
Below is the solution:
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class ReadWritePayRateCalculate {
// declare the variable
static double hours = 0;
static double rate = 0;
static double pay = 0;
static Scanner in = new Scanner(System.in); // scanner
class
public static void main(String[] args) throws
IOException {
String fileName, targetFile; //
declare variable
try {
System.out.print("Please enter a file name:"); // ask to enter a
file name
fileName =
in.nextLine(); // read a file name
File sourceFfile
= new File(fileName); // read a input file name
Scanner input =
new Scanner(sourceFfile); // scanner input
while
(input.hasNextLine()) { // read file of each line
String s1 = input.nextLine(); // store each line
in s1 string
System.out.println(s1); // print each line of
s1
}
input.close();
// close the file
} catch (Exception e) { //
Error
e.printStackTrace(); // error if not file found
}
try {
System.out.println("\nPlease enter the the file name: "); // ask to
append the text to the file
targetFile =
in.nextLine(); // input string read from keyboard
// Create output
files
PrintWriter
output = new PrintWriter(targetFile);
// write into
file
output.println("This the first line write to file");
output.println("This the second line write to file");
output.println("File write done!");
output.close();
// close the file
} catch (IOException e) { //
Error
System.out.println("IO Errors! Can not find the file to
read!");
}
// pay rate calculate
getInputs(in, hours, rate); // get
the input
calculatePay(); // calculate the
pay rate
printPay(); // print the pay
}
// get input
public static void getInputs(Scanner in, double h,
double r) {
while (true) {
try {
System.out.print("Enter Hours: "); // ask to
enter hours
hours = Double.parseDouble(in.next());
break; // will only get to here if input was a
double
} catch
(NumberFormatException e) {
System.out.println("Invalid input");
}
}
while (true) {
try {
System.out.print("Enter Rate: "); // ask to
enter rate
rate = Double.parseDouble(in.next());
break; // will only get to here if input was a
double
} catch
(NumberFormatException e) {
System.out.println("Invalid input");
}
}
}
// calculate pay
public static void calculatePay() {
// check for the hours >
40
if (hours > 40) {
pay = (40 *
rate) + (hours - 40) * 1.5 * rate; // calculate overtime 40
hours
} else
pay = hours *
rate;
}
// print pay
public static void printPay() {
System.out.println("Pay: " +
pay);
}
}
sample output:
Please enter a file name:student.txt
1. John m 18
2. William m 22
3. Susan f 21
4. Jack m 19
5. Jennifer f 18
6. Sophia f 21
Please enter the the file name:
student1.txt
Enter Hours: a50
Invalid input
Enter Hours: 45
Enter Rate: a4
Invalid input
Enter Rate: 10
Pay: 475.0
(Java) Rewrite the following exercise below to read inputs from a file and write the output...
(Java) Rewrite the following exercise to check the user inputs to be: Valid input and positive with using try-catch (and/or throw Exception ) ************************************************************************** Write a program to prompt the user for hours and rate per hour to compute gross pay. Calculate your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours. Your code should have at least the following methods to calculate the pay. (VOID and NON-STATIC) getInputs calculatePay printPay Create...
This is a Java beginner class. Write the following exercise to check the user inputs to be: Valid input and positive with using try-catch (and/or throw Exception ) ************************************************************************** Write a program to prompt the user for hours and rate per hour to compute gross pay. Calculate your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours. Your code should have at least the following methods to calculate the pay. (VOID and...
JAVA Which of these statements does not match by using exception action in Java? 1. Exception action makes it possible for the calling method to handle errors in called methods. 2. Exception action simplifies programming since incorrect reporting and handling can be located in catch blocks separately from the rest of the code. 3. Exception action increases the performance of programs. 4. Java separates exception action from common processes. Why create instances of File Class? - several alternatives...
Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...
Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...
Write a program to create a file named integerFile.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. The random integers should be in the range 0 to 100 (including 0 and 100). Integers should be separated by spaces in the file. Read the data back from the file and display the data in increasing order This is what I have so far: import java.io.File; import java.util.Scanner; import java.io.PrintWriter; public class integerFile {...
Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...
composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main { public static void main(String[] args) { System.out.print("Enter the...
C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...
It's my python assignment. Thank you You wrote a program to prompt the user for hours and rate per hour to compute gross pay. Also your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours. Enter Hours: 45 Enter Rate: 10 Pay: 475.0 (475 = 40 * 10 + 5 * 15) Rewrite your pay program using try and except so that your program handles non-numeric input gracefully. Enter Hours: 20 Enter...