Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment 1. Program to the Max! The big boss JF has unearthed an old program t

ID: 3628105 • Letter: A

Question

Assignment 1.

Program to the Max!

The big boss JF has unearthed an old program that has stumped the company in the past. There is a simple class called FindMax* that will find the maximum value in a given array. The original developers have long since gone bankrupt and the class was never used due to various bugs and errors. JF thought it would be a good idea for you tackle this program so he can finally close this dark chapter in the company’s history.

public class FindMax
{
private int maxValue;
public FindMax()
{
maxValue=0;
}
public int find (int[]searchArray,int key,int start,int last)
{
int myCounter=-1,find=-1;if(start >= last)
throw new IllegalArgumentException("Incorrect Range");
maxValue=0;

for (myCounter=start;myCounter<last;myCounter++);
{
if(searchArray[myCounter]>maxValue);
maxValue=myCounter;return -1;return find;
}
}

}


You are to fix the program so that it will work correctly within the following guidelines: DO NOT STRAY FROM THESE GUIDELINES!

• DO NOT re-write the entire program, you are to keep the program as close as possible to the original.

• Feel free to adjust the formatting and indentations of the source code to make it more readable.

• Add at least two custom exceptions for this task, write more if you wish.

• Write a driver that will ask the user to enter a set of values into the array along with the starting and ending point of the search.

Make notes of all the changes you have done in the comment and be sure to test the program extensively, also make sure that you code this is Java.

Explanation / Answer

import java.io.*;

public class FindMax {

private int maxValue;

public FindMax()

{

maxValue=0;

}

public static void main(String[] args) {

FindMax FM = new FindMax() ;

// Read User's Input

InputStreamReader istream = new InputStreamReader(System.in) ;

BufferedReader bufRead = new BufferedReader(istream) ;

try {

System.out.println("Please Enter In The Array: ");

String array = bufRead.readLine();

System.out.println("Please Enter In The Start Point: ");

String start = bufRead.readLine();

System.out.println("Please Enter In The End Point: ");

String end = bufRead.readLine();

//Convert String to int

String[] temp = array.split(" ");

int[] iarray = new int[temp.length];

for (int i=0 ; i < temp.length ; i++) {

iarray[i] = Integer.parseInt(temp[i]);

}

int istart = Integer.parseInt(start);

int iend = Integer.parseInt(end);

istream.close();

try {

int xout = FM.find(iarray , istart , iend) ;

System.out.println("The Max value is " + xout);

   } catch (IllegalArgumentException IAEx) {

   System.out.println("Incorrect Range");

   }

   } catch (IOException err) {

System.out.println("Error reading line");

   }

}

public int find (int[]searchArray , int start , int last) throws IllegalArgumentException

{

int myCounter = -1 ;

int find = -1;

if(start >= last) {

throw new IllegalArgumentException();

}

maxValue=0;

for (myCounter = start; myCounter<last ; myCounter++) {

if(searchArray[myCounter] > maxValue) {

maxValue = searchArray[myCounter];

}

}

return maxValue;

}

}