Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

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 di erentiate di erent 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 di erent 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 di erent). 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.

Java, Programming

  • Category:- Java
  • Reference No.:- M9719978
  • Price:- $70

Priced at Now at $70, Verified Solution

Have any Question?


Related Questions in Java

Operating systems assignment -problem 1 sharing the bridgea

Operating Systems Assignment - Problem 1: Sharing the Bridge A new single lane bridge is constructed to connect the North Island of New Zealand to the South Island of New Zealand. Farmers from each island use the bridge ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Assessment instructionsin this assessment you will design

Assessment Instructions In this assessment, you will design and code a simple Java application that defines a class, instantiate the class into a number of objects, and prints out the attributes of these objects in a spe ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

Project requirementsfor the problem described in the next

Project requirements For the problem described in the next section, you must do the following: 1. include your student ID at the end of all filenames for all java code files. Three classes have been identified in section ...

Solving 2nd degree equationsbull write the following java

Solving 2nd degree equations • Write the following Java methods • boolean real-sols(double a, double b, double c): it returns true if the 2nd degree equation ax2 + bx + c has real solutions • double solution1(double a, d ...

Assignment - java program using array of objectsobjectives

Assignment - JAVA Program using array of objects Objectives - This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details - For this assignment, you are required to develop a Menu ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As