Purchase Solution

Custom Java Exception Class BadArray Exception

Not what you're looking for?

Ask Custom Question

Write a custom Java exception class named BadArrayException that has two constructors:
- A default constructor with no arguments.
- A constructor taking a String parameter (the exception message) that must be passed to the parent Exception(String) constructor.

Write a Java class named ArrayUtils that contains the following public methods. The class must implement all the search and copy logic for these methods. It can not use array methods from the Java API.

1. public static int minValue(int[] list)

This static method searches its parameter array to locate the minimum value, and returns it. Do not use sorting to find the minimum value in the array. The array may contain any negative, 0, and/or positive values. If the array parameter is length 0, it must throw a BadArrayException with the message "Array is empty".

2. public static int[] copyRange(int[] list, int startIndex)

This static method copies the specified range of elements from the parameter array into a new array, and returns it. The initial index of the range (startIndex) must lie between 0 and list.length, inclusive. The range is [startIndex, Index of last element in the list]. If the startIndex parameter is less than 0 or greater than list.length, it must throw an ArrayIndexOutOfBounds exception with startIndex as the exception object parameter.

3. public static int indexOf(int[] list, int searchValue)

This static method searches its parameter array to locate the first occurrence of the parameter searchValue, and returns its index position if found, or -1 if not found. If the array parameter is length 0, it must return -1.

4. public static int lastIndexOf(int[] list, int searchValue)

This static method searches its parameter array to locate the last occurrence of the parameter searchValue, and returns its index position if found, or -1 if not found. If the array parameter is length 0, it must return -1.

If the array parameter is null, methods must throw a BadArrayException with the message "Array is null". These methods should not catch the BadArrayException they throw due to a null or empty array parameter. The exception is thrown back to the calling code to handle.

Test the implementation of BadArrayException and ArrayUtils classes with the help of attached ArrayUtilsDemo class.

Purchase this Solution

Solution Summary

Solution attachment contains following files: ArrayUtilsDemo.java, BadArrayException.java, and ArrayUtils.java . Implementation has been tested working against the given ArrayUtilsDemo class.

Solution Preview

Please find attached 412925.zip that contains following ...

Purchase this Solution


Free BrainMass Quizzes
Javscript Basics

Quiz on basics of javascript programming language.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

Basic Networking Questions

This quiz consists of some basic networking questions.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.