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...
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...
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
Using MIPS assembly language In this program, you should define an array of 10 elements in...
Using MIPS assembly language In this program, you should define an array of 10 elements in your data segment with these values: ? = {11, 12,−10, 13, 9, 12, 14, 15,−20, 0} a. Write a function which finds the maximum value of this array. b. Write another function which calculates the summation of this array. c. Call these functions in your main program, and print the outputs of these functions to the user i. “The maximum is 15” ii. “The...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
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.
This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
when i run the program on eclipse it gives me this error: Exception in thread "main"...
when i run the program on eclipse it gives me this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0    at SIM.main(SIM.java:12) how do I fix that ? (please fix it ) import java.util.*; import java.util.Scanner; import java.util.ArrayList; import java.io.File; import java.io.FileNotFoundException; import java.lang.Math; public class SIM{    public static void main(String[] args) throws FileNotFoundException {       int cacheSize = Integer.parseInt( args[1] ); int assoc = Integer.parseInt( args[2] ); int replacement = Integer.parseInt(...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT