Question

In: Computer Science

Assume there is a file called "mydata". each line of the file contains two data items

how do you read in a file in JAVA 

Assume there is a file called "mydata". each line of the file contains two data items: hours and rate. hours is the represented by the number of hours the worker worked and rate is represented as hourly rate of pay. The first item of data is count indicating how many lines of data are to follow.

Methods

pay- accepts the number of hours worked and the rate of pay. returns the dollor and cents amount of the amount earned as follows. workers who worked 40 hours or less are paid at their regular rate and workers who worked more then 40 hours are paid their regular rate for the first 40 hours then 100% of their rate for hours in excess of 40.

Example

worker is paid at at rate of $11.00/hours. if worker works for 25 hours he will be paid 25*11=275. if work works for 60 hours he will be paid (40*11)+(20*10)=640

main method

read the count

for each line of data read the number of hours worked and the employee rate

compute the gross pay

data file

4

5 25.00

40 50.00

30 40.00

10 15.00

Solutions

Expert Solution

Implemented code as per the requirement, but small confusion with the condition for extra hours worked. 100% of their rate implies that they should be payed double the amount for the extra hours. Implemented logic as I uderstood your words but not according to the example you gave. If it is wrong please comment so that I can modify the answer.

Code:

=====

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Payroll {

static File file;

static Scanner sc;

public static void main(String[] args) throws FileNotFoundException {

sc = new Scanner(System.in);

System.out.print("Enter number of records you want to process: ");

int i = Integer.parseInt(sc.nextLine());

file = new File("mydata");

sc = new Scanner(file);

int j=0;

String str = "";

while(j

j++;

str = sc.nextLine();

System.out.println("Number of hours employee worked: "+Integer.parseInt(str.split(" ")[0])+" and payment is: "+pay(Integer.parseInt(str.split(" ")[0]),Float.parseFloat(str.split(" ")[1])));

}

}

private static double pay(int hours, double rate) {

if(hours>40){

return (40*rate)+((hours-40)*rate*2);

}

else{

return hours*rate;

}

}

}

Output screen:


Related Solutions

On Python Preview the provided sample file called studentdata.txt. It contains one line for each student...
On Python Preview the provided sample file called studentdata.txt. It contains one line for each student in an imaginary class. The student’s name is the first thing on each line, followed by some exam scores. The number of scores might be different for each student. Using the text file studentdata.txt write a program that calculates the average grade for each student, and print out the student’s name along with their average grade with two decimal places.
the assignment folder for this assignment contains a file called values.txt. The first line in the...
the assignment folder for this assignment contains a file called values.txt. The first line in the file contains the total number of integers which comes after it. The length of this file will be the first line plus one lines long. Implement a MinHeap. Your program should read in input from a file, add each value to the MinHeap, then after all items are added, those values are removed from the MinHeap. Create a java class called MinHeap with the...
The file P08_06.xlsx contains data on repetitive task times for each of two workers. John has...
The file P08_06.xlsx contains data on repetitive task times for each of two workers. John has been doing this task for months, whereas Fred has just started. Each time listed is the time (in seconds) to perform a routine task on an assembly line. The times shown are in chronological order. a. Calculate a 95% confidence interval for the standard deviation of times for John. Do the same for Fred. What do these indicate? b. Given that these times are...
A hotel salesperson enters sales in a text file. Each line contains the following, separated by...
A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event. Write a program that reads such a file and displays the total amount for each service category. Use the following data for your text file:5 pts Bob;Dinner;10.00;January 1, 2013 Tom;Dinner;14.00;January 2, 2013 Anne;Lodging;125.00;January 3, 2013 Jerry;Lodging;125.00;January...
If the file circuit.txt contains the following data
Exercise 2: If the file circuit.txt contains the following data 3.0             2.1 1.5             1.1 2.6             4.1 The first column is voltage and the second column is the electric current. Write program that reads the voltages and currents then calculates the electric power (P) based on the equation: Voltage     Current        Power 3.0             2.1              (result) 1.5             1.1              (result) 2.6             4.1              (result)                         P = v * i Write your output to the file results.txt with voltage in the first, current in the second...
The file provided contains data on the carpace lengths, in mm, of two samples of Midland...
The file provided contains data on the carpace lengths, in mm, of two samples of Midland Painted Turtles (Chrysemys picta marginata). One sample is for female turtles and the other sample is for male turtles. (i) Use the sample data to carry out a hypothesis test to test the claim that the variability of the carpace lengths is different for female Midland Painted Turtles compared to male Midland Painted Turtles. Use a significance level of 0.05. Your answer should include:...
Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
Write an HTML file for a web page that contains the items below. Use an internal...
Write an HTML file for a web page that contains the items below. Use an internal style sheet to specify all fonts, sizes, colors, and any other aspects of the presentation. Your page should contain the following items: 1) A header with white text on dark green background (just for the header, not the entire page), in Impact font, bold, and centered. 2) Two paragraphs of text, each with dark gray text in Tahoma font, on a light blue background,...
Using the following lines of data, create a temporary SAS data set called ThreeDates. Each line...
Using the following lines of data, create a temporary SAS data set called ThreeDates. Each line of data contains three dates, the first two in the form mm/dd/yyyy descenders and the last in the form ddmmmyyyy. Name the three date variables Date1, Date2, and Date3. Format all three using the MMDDYY10. format. Include in your data set the number of years from Date1 to Date2 (Year12) and the number of years from Date2 to Date3 (Year23). Round these values to...
Look at the attached Excel file which contains two column charts identifying data for a company:...
Look at the attached Excel file which contains two column charts identifying data for a company: Week 1 Discussion Topic 2 Ethics.xls. Notice that the data is depicted differently, however, the data presented is really the same. Is it ethical to present data in a different way in order to make the data appear more favorable? Explain your reasoning.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT