Part 1

1. write an exception class, throw and handle an exception, and write to and read from a random-access file.

Use one of the Fibonacci methods included below (one is recursive, so if you're not familiar with recursion, you might prefer the derecursed method fibIter.

It is conventional to define Fibonacci(0) as 0 (http://mathworld.wolfram.com/FibonacciNumber.html), and Fibonaccis are not defined for numbers less than 0.

Write a custom exception class NegativeFibRequest, for a Fibonacci request for a number less than 0. Rewrite either of the Fibonacci methods to throw this exception if the user attempts to compute the Fibonacci of a negative number. Write a program to handle the exception.

Write a method to write the first twenty Fibonaccis in the sequence to a random-access file. Write a program to retrieve any of the first twenty Fibonaccis from the file.

F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

// Derecursed fibonacci method.
public static long fibiter(long n) {
long fib;
if (n == 0) {
fib = 0;}
else if (n == 1) {
fib = 1; }
else {
fib = 1;
long lastFib = 1, secondLastFib = 1;
for (int i = 3; i <= n; i++) {
fib = lastFib + secondLastFib;
secondLastFib = lastFib;
lastFib = fib;
}
}
return fib;
}
// Recursive fibonacci method.
private static long fibRecursive(long n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fibRecursive(n - 1) + fibRecursive(n - 2);
}

Part 2

2. a) Write to and read from a binary file and handle exceptions.

Design a class that has a static method named writeArray. The method should take two arguments: the name of a file and a reference to an int array. The file should be opened as a binary file, the contents of the array should be written to the file, and then the file should be closed.

Write a second method in the class named readArray. The method should take two arguments: the name of a file and a reference to an int array. The file should be opened, data should be read from the file and stored in the array, and then the file should be closed. Demonstrate both methods in a program.

In both methods you should handle the appropriate file exceptions (IOException, EOFException, and/or FileNotFoundException).

2. b) Write a program with a serializable class

Given the TestScores class on the next page, modify the class to be serializable. Write a program that creates an array of at least five TestScore objects, serializes them, and then deserializes them. Display the average of the test scores before serialization and after deserialization to demonstrate that the objects that are read are the same objects that were written.

/**
TestScores Class
*/
public class TestScores
{
// Variable to reference an array holding test scores
private double[] scores;
/**
The constructor initializes an object with
an array of scores. If the array contains
an invalid value (less than 0 or greater than
100) an exception is thrown.
@param s The array of test scores.
@exception IllegalArgumentException When the
argument array contains an invalid
value.
*/
public TestScores(double[] s) throws IllegalArgumentException
{
// Create an array to hold the scores passed
// as an argument.
scores = new double[s.length];
// Copy the scores passed as an argument into
// the new array. Check for illegal values as
// they are copied.
for (int i = 0; i < s.length; i++)
{
if (s[i] < 0 || s[i] > 100)
throw new IllegalArgumentException("Element: " + i + " Score: " + s[i]);
else
scores[i] = s[i];
}
}
/**
The getAverage method returns the average
of the object's test scores.
@return The average of the object's test scores.
*/
public double getAverage()
{
double total = 0.0; // Accumulator
// Accumulate the sum of the scores.
for (int i = 0; i < scores.length; i++)
total += scores[i];
// return the average.
return (total / scores.length);
}
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.