Part-11 exceptions and exception handlinggt create a java

Assignment Help JAVA Programming
Reference no: EM13350361

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; i<n; i+=1){
if( !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.

Reference no: EM13350361

Questions Cloud

Objective1 to give students practice in calling and writing : objective1. to give students practice in calling and writing their own functions.2. to give students practice in
You have to review vhhr and fill me in with suggestions : you have to review vhhr and fill me in with suggestions based on your views on the feedback and website based on
Question 1 companies and business planningfwpl acquired the : question 1 companies and business planningfwpl acquired the winery business in 1981 from francesca and angelo galli.
This paper is called a jury paper this is related to : this paper is called a jury paper. this is related to experimental psychology. this paper needs to be 8-10 pages
Part-11 exceptions and exception handlinggt create a java : part-11 exceptions and exception handlinggt create a java class called samearraysexception that extends the exception
1 linked listsin this problem you will write a class that : 1 linked listsin this problem you will write a class that implements an ordered list of strings. your class will able
Going live is not the end of the erp journeythe benefits : going live is not the end of the erp journeythe benefits companies expect from their erp systems often occur over a
Part-1inheritance write code using eclipse and output : part-1inheritance write code using eclipse and output screenimplement a subclass square that extends the rectangle
Write the code with comment and output resultinterface and : write the code with comment and output resultinterface and polymorphismimplement a class quiz that implements the

Reviews

Write a Review

JAVA Programming Questions & Answers

  Design a small wbis using elements from the disciplines

Design a small WBIS using elements from the disciplines of web design, database design, software engineering and object modelling;be able to develop a small WBIS using HTML forms, Java Servlets and cookies.

  I had to call a webservice

I had to call a webservice (1st line of code) and then from there created a loop but keep getting an error every time on the .getBusiness - I don't know what I should be using there instead of getBusiness.

  Write the bubble sort

The village of Marengo conducted a census and collected records that have household data, including the number of occupants in each household.

  Design and implement a small and simple email server

Design and implement a small and simple email server using the concept of web based information system (WBIS).

  On any given execution your program

On any given execution your program will produce just one version of the figure. However, you should refer to the class constant throughout your code, so that by simply changing your constant's value and recompiling, your program would produce a f..

  Write the logical expression in postfix notation.

Given symbols p, q, r, and s for propositions. Draw the binary tree representation of the logical expression ((p || r) && ! p ) || s) && (q || r). Write the logical expression in postfix notation.

  Java program to store temperatures in two-dimensional array

Write the Java program which uses two-dimensional array to store highest and lowest temperatures for each month of the year. Program must output average high,average low,

  An api for a library that provides for all these operations

Quaternions can be represented with four (4) real numbers (a,b,c,d). They can be added, subtracted, multiplied and divided. You can multiply a quaternion by a scalar (which produces a quaternion as a result)

  What are some debugging techniques for javascript

What are some common programming errors? How can they be prevented? What is the try catch structure, how is it useful? What are some debugging techniques for JavaScript?

  Develop java code to compute monthly rent for housing units

Develop a java code that computes monthly rent for 3 housing units namely Bungalows,Apartments and hostels. All housing units have got size,color and monthly rental rate.

  Which a ball is released from a user-defined height

Write a program in which a ball is released from a user-defined height and free-falls to the ground. The ball is pulled by earth's gravity of 9.8 m/sec 2 . Assume that each pixel represents

  Reads contents of two vectors

Write a program that reads contents of two vectors, and then displays the sum of these two vectors. The program should prompt the user to enter the size of the vectors first.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd