Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part1: Exception. A heart rate data file \"\"HR.txt\" , which records hundreds o

ID: 3750955 • Letter: P

Question

Part1: Exception.
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:

Requirements:

Files to be submitted: HeartRate.java, HRIllegalValueException.java, and HR.txt.

The main method of HeartRate class should look like:

You can use FileReader and Scanner to handle the data input from a file.

The file "HR.txt" must be at the same directory of our source code.

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.


Part2: Recursive Binary Print.
Write a recursive method that prints the binary form of a given non-negative integer, with the following specification:

The method prints the value of n as a BINARY number. If n is zero, then a single zero is printed; otherwise no leading zeros are printed in the output. Examples:

Hint: How to convert an int to its binary form? A very intuitive way is to divide the number by 2 in each recursion. In each recursive call, you print either '0' (unless it is a leading zero) if 2 divides n, or '1' otherwise.

Requirements:

Test Program: you are required to write a test program that tests your NPrint class. Basically, you may want to prompt the user to enter an integer and then invoke the binaryPrint() method in your NPrint class. For the simplicity, you may assume the integer is non-negative.

Files to be submitted: NPrint.java and Test.java.

Explanation / Answer

As per Chegg policy, solving first part of the question: -

public class HeartRate {


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

int good = 0, bad = 0;
Scanner scanner = new Scanner(new File("abc.ser"));
while ( scanner.hasNextLine()) {
try {

int input = scanner.nextInt();

if (input < 0) {
throw new HRIllegalValueException();
}

good++;

} catch (HRIllegalValueException d) {
bad++;
//System.out.println(d.getMessage());
} catch (InputMismatchException i) {
//consume the input
String waste = scanner.next();
bad++;
}
}

System.out.println("Good Data: " + good);
System.out.println("Bad Data: " + bad);

}

}
public class HRIllegalValueException extends RuntimeException{
HRIllegalValueException() {
super("cannot be negative");
}
}
Input HR.txt

12
32
1dwe
-23
-4321
1234
dcdfve
dcef
43
-32

Output

Good Data: 4

Bad Data: 6

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote