Question: Help with java I need help with this part of the code. timeAlgorithm : that takes the algorithm number and the size of the algorithm

Help with java I need help with this part of the code.

  1. timeAlgorithm: that takes the algorithm number and the size of the algorithm and returns the time it takes to run the algorithm once. To determine the time it took to run an algorithm, you can use the System.currentTimeMillis() method before and after the algorithm call (Hint: You can use the runAlgorithm method you already wrote). Dont forget to convert from milliseconds to seconds before returning the result (Hint: There is a constant already made at the top called "MILLISECONDS_PER_SECOND"). Javas built-in garbage collector can run at any time so discourage it from interfering with your timing by forcing it to run just before you run your analysis (Hint: The System.gc() line does this for you).

here is the code

public class BigOh { private static final double MILLISECONDS_PER_SECOND = 1000.0; private Random rand;

/** * No-args constructor initializes the random using current time. */ public BigOh() { rand = new Random(); }

/** * Constructor takes an Random object to initialize the randomness of the * algorithms. * * @param rand * the random number generator */ public BigOh(Random rand) { this.rand = rand; }

/** * robustTimeAlgorithm returns the minimum time it takes to run the chosen * algorithm over 5 trials. * * @param choice * The index of the algorithm to use * @param n * The size of the problem * @return the time in seconds */ public double robustTimeAlgorithm(int choice, int n) { // TODO return -1.0; }

/** * timeAlgorithm returns the time it takes to run the algorithm once. * * @param choice * The index of the algorithm to use * @param n * The size of the problem * @return the time in seconds */ public double timeAlgorithm(int choice, int n) { // make sure that the garbage collector doesn't run // during timing. (Do this first.) System.gc();

// TODO return -1 * 2; }

/** * runAlgorithm selects the algorithm to run based on choice. * * @param choice * The number representing the algorithm choice * @param numElements * The size of the problem * @return The result of the algorithm */ public int runAlgorithm(int choice, int numElements) { // TODO (be sure to change return statement too) if(choice < 6) { return Algorithms.java(); } else { System.out.print(" invalid algorithm choice"); } if(choice > 0) { return Algorithms.java(); } else { System.out.print(" invalid algorithm choice"); } return Algorithms.java(); }

/** * bigOhFunc returns the Big-Oh function for algorithm and problem size * parameters. * * @param choice * The number representing the algorithm choice * @param n * The problem size. * @return The Big-Oh function for problem size, n. */ public double bigOhFunc(int choice, double n) { return -1 * 4; }

/** * estimateTiming takes an algorithm choice, problem size and timing, and * estimates the timing for a second problem size. * * @param choice * The number representing the algorithm choice * @param n1 * The first problem size * @param t1 * The first timing * @param n2 * The second problem size * @return The estimated timing for the second problem size */ public double estimateTiming(int choice, int n1, double t1, int n2) { // TODO return -1 * 8; }

/** * percentError returns the percent error in an estimate. * * @param correct * the correct value * @param estimate * the estimated value * @return the percent error */ public double percentError(double correct, double estimate) { // TODO return -1 * 16; }

/** * computePercentError takes an algorithm choice, and two problem sizes and * computes the error in estimating the timing of the second problem using * the timing of the first. * * @param choice * The number representing the algorithm choice * @param n1 * The first problem size * @param n2 * The second problem size * @return the percent error in estimating t2 given n1 and n2. */ public double computePercentError(int choice, int n1, int n2) { // TODO return -1 * 32; }

/** * Main method. * * @param args * Command line arguments not used. */ public static void main(String[] args) { int choice; int numElements = 0; Scanner keyInput = new Scanner(System.in); BigOh bo = new BigOh();

// run the fragments choice = menu(keyInput); while (choice != 7) { if (choice >= 1 && choice <= 6) { System.out.print("How many elements: "); numElements = keyInput.nextInt(); double time = bo.timeAlgorithm(choice, numElements); long milliseconds = (long) (time * MILLISECONDS_PER_SECOND); System.out.println("The time for alg" + choice + " with n=" + numElements + " is " + milliseconds + " ms. "); } choice = menu(keyInput); } System.out.println("Quitting"); }

/** * Prints the menu and prompts for input. * * @param keyInput * The scanner to read input * @return the number read */ public static int menu(Scanner keyInput) { int choice = -1;

System.out.println(); System.out.println(" 1. Method #1 "); System.out.println(" 2. Method #2 "); System.out.println(" 3. Method #3 "); System.out.println(" 4. Method #4 "); System.out.println(" 5. Method #5 "); System.out.println(" 6. Method #6 "); System.out.println(" 7. Quit "); System.out.print("Enter your choice: "); choice = keyInput.nextInt(); return choice; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!