Topics
· Exceptions
· File I/O
· Recursion
Concepts
try…catch statement
throws declaration
checked vs. unchecked exceptions File class, input/output streams
FileWriter, BufferedWriter, PrintWriter classes recursive methods
Euclid's greatest common divisor algorithm recursive string reversal
Exercise 1
a) Create a text file called "input.csv" which contains the following three lines of numbers, separated by commas:
1,4,6,7,8
2,3,1,5,6
3,2,1,4,5
b) Write a program that reads the file you created (make sure it's in the right folder) and outputs to the screen the largest number in each row. The output should looks like this: Maximum values
ROW 1: 8
ROW 2: 6
ROW 3: 5
Exercise 2
Write a program that reads strings from the user and writes them to an output file called userStrings.txt. Terminate processing when the user enters the string "DONE". Do not write the sentinel string to the output file.
Exercise 3
Design and implement a program that implements Euclid's algorithm for finding the greatest common divisor of two positive integers. The greatest common divisor is the largest integer that divides both values without producing a remainder. In a class called DivisorCalc, define a static method called gcd that accepts two integers, num1 and num2. Create a driver program inside the main method to test your implementation. The recursive algorithm is defined as follows:
gcd(num1, num2) is num2 if num2 and num2 divides num1 gcd(num1, num2) is gcd(num2, num1) if num1 is gcd(num2, num1%num2) otherwise
Exercise 4
Create a class called StringReversal. Inside of it, create a recursive method called reverseString that accepts a String s and returns a new String which is s in reverse order. Create a driver program inside the main method to test your implementation.