Question: NEED HELP IMPLEMENTING ISABELS TECHNIQUE: Implement Isabels technique for summing the values in an array of n integers as described in problem C-5.24 on page
NEED HELP IMPLEMENTING ISABELS TECHNIQUE:
Implement Isabels technique for summing the values in an array of n integers as described in problem C-5.24 on page 223 of the textbook.
The values to be summed by Isabels technique should be stored in an ASCII text file. You can create this file using a text editor like Notepad.
The values should be white space delimited in this file.
To call to Isabels technique your program needs to:
Request the path to the data file
Load the contents of the data file into an array
Call Isabels technique
As described in the text, Isabels technique only works for arrays that contain a power of 2 number of integers. If the number of integers in the data file are not a power of 2 your method must throw an appropriate exception.
Create a Client Class that will fully test each of the recursive methods that you created above in an interactive menu fashion. This class should:
Use a JOpitonPane to ask the user which algorithm they wish to test.
The user should be provided with a menu/list to select from
If the user selects an invalid option, the program should
Inform the user of their mistake and allow them to try again
The last option in the menu/list should allow the user to quit the program.
For each recursive technique, use a JOptionPane to ask the user for any parameters/values needed for the test.For Isabels technique the parameter should be the path that contains a blank (white space) separated list of integers values that will make up the array.
When you read in the values from the file you should skip any values that are not integers.
The presence of one or more on-integer values in the input file does not automatically make the file invalid. If the number of valid integer values in the file is a power of 2 then it is a valid input file.
If the input file does not contain a power of 2 number of valid integers your program should inform the user of the problem and ask them to enter a new filename or quit back to the algorithm selection menu.
If the user enters a file that does not exist your program should inform the user that the file does not exist and ask the user to enter a correct filename or quit back to algorithm selection menu.
The user should have the option of giving up on trying to run Isabels technique and if the user selects that option they should be returned to the main menu.
Output for each test should be shown in the system console and must include:
The name of the algorithm being tested.
The parameters entered by the user
Values used if different from parameters (e.g. the contents of the array read in for Isabels technique).
Error messages give to the user (e.g. file not found)
The results of the test.
Note that the JOptionPane interactions will not be captured in the system console.
At the end of a test the user should be given the options of running another test or exiting the program, i.e. after running one of the recursive algorithms the user should be returned to the main menu.
WHAT I HAVE SO FAR:
//RECURSION CLASS//
import java.io.File;
public class Recursion { public static double harmonic(int n) { //recursive algorithm to compute the nth Harmonic number if (n==1) //base case return 1.0; else return (1.0/n) + harmonic(n-1); } public static int isabel(int[] A){ //implement isabel`s technique if(A.length==1) //base case return A[0]; else{ int B[]= new int[A.length/2]; for(int i=0; i void list(String path) { File directory = new File(path); if(directory.isDirectory() && directory.exists()) { File[] files = directory.listFiles(); if(files != null) { for (File file : files) { if(file.isDirectory()) { list(file.getAbsolutePath()); } else { System.out.println(file.getAbsolutePath()); } } } } } } //RECURSION_CLIENT CLASS// public class Recursion_Client { public static void main(String[] args) throws FileNotFoundException { String response = JOptionPane.showInputDialog(new JFrame(), "Which Algorithm do you want to test? 1) compute the nth Harmonic number 2) test Isabel`s technique 3)Exit program "); if (response.equals("1")) { int parameter = Integer.parseInt(JOptionPane.showInputDialog(new JFrame(), "Please enter your parameters")); double result = Recursion.harmonic(parameter); System.out.println("parameter entered:" + parameter); System.out.println(" result of harmonic algorithm: " +result); JOptionPane.showMessageDialog(new JFrame(), "Algorithm Harmonic number " + " The parameters entered by the user are: " + parameter + "." + " result: " + result + "." ); } if (response.equals("2")) { String fileName = JOptionPane.showInputDialog(new JFrame(), "Please enter the file name"); System.out.println("file inputed: " + fileName); File f = new File(fileName); Scanner scan = new Scanner(f); int A[] = new int[4]; int i = 0; while (scan.hasNext()) { try { A[i++] = scan.nextInt(); } catch (Exception e) { continue; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
