Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A heart rate data file HR.txt , which records hundreds of heart rate readings (in integer), is contaminated (by viruses) and many illegal data (unknown

A heart rate data file "HR.txt" , which records hundreds of heart rate readings (in integer), is contaminated (by viruses) and many illegal data (unknown data type) are inserted. Write a program "HeartRate.java" to read the data file and output the number of good readings as well as the number of bad data. The definition is described as follows: Good data: any positive integer reading. Bad data: (1) Any non-integer reading; (2) Any negative (integer) reading. In your program, you need two exceptions to handle two kinds of bad data.

  • For the non-integer reading, you can use nextInt() method to read an integer from the file, if the data is not an integer, InputMismatchException will be thrown. Your program must catch this exception.
  • To handle the negative reading, you may want to declare your own Exception class: HRIllegalValueException (you are expected to write HRIllegalValueException.java that contains the class), and threw this exception when a negative value is read.

Note: your program should not terminate when a bad data is read. Therefore, you must use try/catch block to handle the bad data. Your program is expected to print out (on the screen) the number of good HR readings and the number of bad data.

A sample execution trace:

$ java HeartRate

HRIllegaValueException: cannot be negative.

HRIllegaValueException: cannot be negative.

Good Data: 24

Bad Data: 6

Requirements:

  • Files to be submitted: HeartRate.java, HRIllegalValueException.java, and HR.txt.
  • The main method of HeartRate class should look like:
  • public static void main(String[] args) throws IOException {
  • }
  • You can use FileReader and Scanner to handle the data input from a file.
  • Scanner input = new Scanner(new FileReader("HR.txt"));

The file "HR.txt" must be at the same directory of our source code. To use Scanner and FileReader, you must import two classes as follows:

import java.io.FileReader;

import java.util.Scanner;

Note that you are not allowed to import any other Java classes.

  • The nextInt() method will throw out an InputMismatchException if the read data is not int type. You may assume all good readings are int type, and the bad data are other types. A try/catch block should be good enough to handle this problem. Yes, you really want to catch InputMismatchException in your catch block.
  • When a non-ingeter is read by nextInt() method, an InputMismatachException is thrown. The bad data (a non-integer), however, has not been consumed yet, it is your responsibility to consume this data so that the next data can be read. Otherwise, your program will be always stuck at the bad data and cause an infinite loop.

Grading Criteria:

  • Please make sure your program compile, otherwise your submission will not be graded and you will receive zero.
  • Point deduction rule:
    1. Compile warning: 3 points each.
    2. Minor error: such as not meeting the assignment input/output requirement, 5 points each.
    3. Major error: examplels include, infinite loop, runtime errors, any runtime exception, 15 points each.
    4. Any code not compliant to the assignment requirement (such as importing other Java classess and performing file I/O by other code instead of the code provided) will not be graded and you will receve zero.
  • HeartRate.java: 60 points
  • HRIllegalValueException.java:30 points
  • README: 10 points
  • HR.txt: please be aware that we will use a different HR.txt while grading your submissions. You are encouraged to modify your HR.txt for the debuging purpose.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Management Systems

Authors: Patricia Ward, George A Dafoulas

1st Edition

1844804526, 978-1844804528

More Books

Students also viewed these Databases questions

Question

8. Explain the relationship between communication and context.

Answered: 1 week ago

Question

d. How were you expected to contribute to family life?

Answered: 1 week ago