Part-1
1: Exceptions and Exception Handling
> Create a Java class called SameArraysException that extends the Exception class. Your class must have a public constructor that has one input argument, a String.
> Create a Java class called SameArraysSizeMismatchException that extends your SameArraysException class. Your class must have a public constructor that has one input argument, a String.
> Complete the code in the SameArrays class that is provided. Do not change any actual lines of code in the method in the class. Instead, you must add code (either a single line or a block of code) in four places (each denoted by the \: : : " shown below). You can add additional catch blocks if you wish. In particular, you need to complete the following method in the class:
public static boolean sameArrays(Object[] arrayOne, Object[] arrayTwo)
throws SameArraysSizeMismatchException, SameArraysException
{
boolean answer = true;
try{
int n = Math.max(arrayOne.length, arrayTwo.length);
for(int i=0; iif( !arrayOne[i].equals(arrayTwo[i] )
answer = false;
}
}catch(...){ <--- Replace the four "..." with your own code
... <--- Do not change anything else in the method/class
}catch(...){ <---
... <--- You may add addition catch blocks though
}
return answer;
}
You are not allowed to change this method except for replacing each \..." with appropriate code. (Again, you may add additional catch blocks to dierentiate dierent exception/erros if you wish.) Your task is to catch exceptions or errors (that might be thrown when running the method) and deal with them appropriately. When a certain exception is thrown (by the JVM) as a consequence of the arrays being dierent lengths, you must catch it and then throw a SameArraysSizeMismatchException object. Any other exception or error that is thrown (by the JVM) must be caught and dealt with (by throwing a SameArraysException object).
The message in your SameArraysException objects (the input String to the constructor) should be concise and descriptive of the problems that occurred. The message in your SameArraysSizeMismatchException objects should be one of two strings: \rst array is longer than the second" or \second array is longer than the rst", depending on which array is actually longer. Static constants are provided for the actual strings to use.
Your SameArrays class should have no other methods (and no other attributes).
Part-2
Recursion I
> Create a Java class called Pals. The class should have the following method:
public static boolean isPalindrome(String word)
// Input : word is a non-null string
// Output: true if the input string is a palindrome*
// false otherwise
//
// * A palindrome is a sequence of characters that is the
// same when read forward and backwards.
//
// * For this question we will ignore whitespace
// when determining if a string is a palindrome or not.
//
// Examples: Pals.isPalindrome("a") => true
// Pals.isPalindrome("cat") => false
// Pals.isPalindrome("w o w") => true
// Pals.isPalindrome(" a ") => true
// Pals.isPalindrome("mom!") => false
Your isPalindrome() method must use recursion. You will receive zero correctness marks if you do not use recursion in this problem.
Part-3
> Create a Java class called DNA. The class should have two methods:
public static String compress(String dna)
// input: a non-null string consisting only of the letters G,A,T and C.
// (this is the long form representation)
// (they may be upper, lower, or mixed case)
// output: a string that represents a condensed version of the input string.
// o) Each occurrence of a single letter (its neighbours are different)
// is copied to the new string.
// o) Each occurrence of a sequence of a single letter that
// is repeated two or more times is replaced by
// that letter and number of occurrences in the new string
// All letters in the output should be capitalized.
// Example: DNA.compress("GGCcCTtttTT") => "G2C3T6"
// DNA.compress("Cat") => "CAT"
// DNA.compress("") => ""
public static String expand(String dna)
// input: a non-null string that represents a compressed sequence of DNA
// (the same form as the output of compress() except mixed-case is allowed)
// (it consists only of G,A,T,C and the digits 0,1,..,9)
// output: the long form sequence of DNA that the input represented.
// All letters in the output should be capitalized.
// Examples: DNA.expand("G2T5") => "GGTTTTT"
// DNA.expand("cat") => "CAT"
// DNA.expand("") => ""
For this question you will need to be able to access individual elements of a string, concatenate strings, convert (sub)strings to numbers and convert numbers to strings.
Your compress() and expand() methods must use recursion1
. You will receive zero
correctness marks if you do not use recursion in this problem. We will only test your methods with valid input.
You may create helper recursive methods if you nd the single input methods given are too restrictive. For example, you might have private compressHelper() method that your compress() method calls. In this case compress would not be recursive, but compressHelper would. This is OK if you approach it this way. We will only call compress and expand when testing so make sure they call your helper methods.
Part-4
Exceptions and Exception Handling II
This is a bonus problem and is not required.
Now let's assume that we do not honour the preconditions for the methods in the DNA class from the previous problem.
> Create three Java classes: MoreDNA, DNABadInput, and TestMoreDNA.
The DNABadInput class must extend the Exception class. It must have a constructor with a single (String) input argument (just as in Problem 1). The exception should be thrown when there is bad input into either of the methods in MoreDNA.
The MoreDNA class should be almost identical to the DNA class from the previous problem except that both methods must throw a DNAException object when their input is bad. The message in the exception should indicate which method threw the exception and what sort of bad data was involved. The message should be a simple string concatenation of the name of the method with one of the 3 kinds of bad input data (these 3 strings are given in the skeleton code provided).
The TestMoreDNA class should be a testing class for your MoreDNA class. This class should be a runnable program. It should have 5 tests for each method in MoreDNA. For each method, 3 tests should be for good data and 2 for bad data (the tests need to be dierent). You do
not need to document your testing program. Be sure that the outout of the program is easy to follow/understand (simple is good here). Your testing program should not crash when a exception is thrown when testing.