Question

In: Computer Science

4 Run-time error, program crashed during execution. Not sure why you are using an array[] to...

4 Run-time error, program crashed during execution. Not sure why you are using an array[] to get input, not necessary and it seems to imply a comma separated file. The input file I provided has each data element on its own line. But it is the array[] , splittedLine, that is causing your run-time error.

code

import java.util.Scanner;

public class SalaryCalcModularized{
public static void compute(double hrlyPayRate,double hrs,String name,String shift){
// Calculate regular and overtimepay
double regularPay = Math.min(40, hrs) * hrlyPayRate;
double overtimePay = Math.max(0, hrs - 40) * 1.5 * hrlyPayRate;
System.out.println("Employee " + name);
System.out.println("Regular Pay: $" + regularPay);
System.out.println("Overtime Pay: $" + overtimePay);
System.out.println("Total Gross Pay: $" + (regularPay + overtimePay));
if (shift.equals("day")) {
System.out.println("Friday pay period");
} else {
System.out.println("Saturday pay period");
}
}
public static void read_input(){
// Scanner to read user input
Scanner obj = new Scanner(System.in);


// Prompt employee name
System.out.print("Enter Employee Name: ");
String name = obj.nextLine();
// Prompt shift
System.out.print("Enter Employee Shift: ");
String shift = obj.nextLine();
// Prompt hours worked
System.out.print("Enter hours worked: ");
double hrs = Double.parseDouble(obj.nextLine());
// Prompt payrate
System.out.print("Enter hourly pay rate: ");
double hrlyPayRate = Double.parseDouble(obj.nextLine());
compute( hrlyPayRate,hrs, name, shift);

}
  
public static void main(String[] args) {
Scanner ob=new Scanner(System.in);
String ch = "y";
while (ch.equalsIgnoreCase("y")) {
read_input();
System.out.println("Do you want continue? [Y/N]");
ch = ob.next();
ob.nextLine();
}
}

}

Solutions

Expert Solution

import java.util.Scanner;

public class SalaryCalcModularized{
public static void compute(double hrlyPayRate,double hrs,String name,String shift){
// Calculate regular and overtimepay
double regularPay = Math.min(40, hrs) * hrlyPayRate;
double overtimePay = Math.max(0, hrs - 40) * 1.5 * hrlyPayRate;
System.out.println("Employee " + name);
System.out.println("Regular Pay: $" + regularPay);
System.out.println("Overtime Pay: $" + overtimePay);
System.out.println("Total Gross Pay: $" + (regularPay + overtimePay));
if (shift.equals("day")) {
System.out.println("Friday pay period");
} else {
System.out.println("Saturday pay period");
}
}
public static void read_input(){
// Scanner to read user input
String name=""; String shift=""; double hrs =0.0; double hrlyPayRate =0.0;
Scanner obj = new Scanner(System.in);


// Prompt employee name
System.out.print("Enter Employee Name: ");
if (obj.hasNextLine()) {
name= obj.nextLine();
}
else
return;
// Prompt shift
System.out.print("Enter Employee Shift: ");
if (obj.hasNextLine()) {
shift= obj.nextLine();
}
else
return;
System.out.print("Enter hours worked: ");
if (obj.hasNextLine()) {
hrs = Double.parseDouble(obj.nextLine());
}
else
return;
//double hrs = Double.parseDouble(obj.nextLine());
// Prompt payrate
System.out.print("Enter hourly pay rate: ");
if (obj.hasNextLine()) {
hrlyPayRate = Double.parseDouble(obj.nextLine());
}
else
return;
compute( hrlyPayRate,hrs, name, shift);
//obj.close();

}
  
public static void main(String[] args) {
Scanner ob=new Scanner(System.in);
String ch = "y";
while (ch.equalsIgnoreCase("y")) {
read_input();
System.out.println("Do you want continue? [Y/N]");
if (ob.hasNextLine()) {
ch = ob.nextLine();
}
else
return;
ob.nextLine();
}

}

}


Related Solutions

In chapter 9, you learned about address binding during the execution of a program. In your...
In chapter 9, you learned about address binding during the execution of a program. In your own words, explain the steps a user program goes through before being executed. Explain the difference between logical and physical address space. Explain virtual memory and describe the benefits of virtual memory for a programmer. On the same paper, show the work to answer the following two questions. Given six memory partitions of 300 KB, 600 KB, 350 KB, 200 KB, 750 KB, and...
Must be using Java Eclipse Please 4. Test program LinkedList.java, and make sure that you understand...
Must be using Java Eclipse Please 4. Test program LinkedList.java, and make sure that you understand each operation in the program. (refer to linkedListApplication.java) 6. (Programming) Use LinkedList as a reference, add the following operations in the class LinkedList; a) Find the average data values of the linked list. b) Find the node with largest key, and then delete the node. (Note, you must return the node, not just the largest key) c) Test ALL operations (Search, Insert, Delete, Append/Remove...
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
Write a program that uses an array for time and temperature. The program should contain an...
Write a program that uses an array for time and temperature. The program should contain an array with 24 elements, each of which is holding a temperature for a single hour. Your program should have a function that lists all of the temperatures that are held in the array. Temperatures should be listed to console with one entry per line. Your program should have a function that lists a single temperature entry. You should ask the user for a number...
Using Java language (in program NetBeans). 1) Using a 2 dimensional array Your company has 4...
Using Java language (in program NetBeans). 1) Using a 2 dimensional array Your company has 4 grocery stores. Each store has 3 departments where product presentation affects sales (produce, meat, frozen). Every so often a department in a store gets a bonus for doing a really good job. You need to create a program that keeps a table of bonuses in the system for departments. Create a program that has a two dimensional array for these bonuses. The stores can...
1.       Test the execution time of the program in Code 3. Make a screen capture which...
1.       Test the execution time of the program in Code 3. Make a screen capture which shows the execution time that has the unit of machine time unit. Code3 SOURCE.CPP # include<fstream> # include "List.h" # include <iostream> using namespace std; int main() { List temps; int oneTemp; ifstream inData; ofstream outData; inData.open("temp.dat"); if (!inData) {   outData<<"Can't open file temp.dat" << endl;   return 1; } inData >> oneTemp; while (inData && !temps.IsFull()) {   if (!temps.IsPresent(oneTemp))    temps.Insert(oneTemp);   inData >> oneTemp; }...
How many types of errors can occur during the execution of a program? Discuss the circumstance...
How many types of errors can occur during the execution of a program? Discuss the circumstance when these types of errors can occur. Support your argument with examples.
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
Please identify each error present, explain the type of error (syntax/logic/run-time), and provide a fix which...
Please identify each error present, explain the type of error (syntax/logic/run-time), and provide a fix which changes the given code as little as possible (i.e. don't rewrite the whole thing; just point out what is wrong and a fix for it). 5) // Assign a grade based on the score char score; cin>>score; if score < 0 && score > 100     cout<<"The score entered is invalid"<<endl; if (score <= 90)     grade = "A"; if (score <= 80)    ...
4. Explain why the aggregate supply curve is positively sloped during the short run and vertical...
4. Explain why the aggregate supply curve is positively sloped during the short run and vertical in the long run. 5. List some examples of factors that will shift the aggregate demand curve. 6. List some examples of factors that will shift the long-run aggregate supply curve.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT