Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java I need help with this part of the code. bigOhFunc : that takes the algorithm number and size of the problem (N), and returns

Java

I need help with this part of the code.

  1. bigOhFunc: that takes the algorithm number and size of the problem (N), and returns the big-oh function for each algorithm that you computed by hand. For example, if you think algorithm 1 has a big-oh of N^2, then calling bigOhFunc(1, 100) would return 10000. This function must be correct in order for Web-CAT to grade your lab.

here is the code I have so far.

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 double tm = MILLISECONDS_PER_SECOND; for (int i = 0; i < 5; i++) { double t = timeAlgorithm(choice,n); if (t < tm) { tm = t; } } return tm; }

/** * 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 double t1 = System.currentTimeMillis(); runAlgorithm(choice, n); double t2 = System.currentTimeMillis(); return (t2 - t1) / 1000; }

/** * 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) switch (choice) { case 1: Algorithms.alg1(numElements, rand); break; case 2: Algorithms.alg2(numElements, rand); break; case 3: Algorithms.alg3(numElements, rand); break; case 4: Algorithms.alg4(numElements, rand); break; case 5: Algorithms.alg5(numElements, rand); break; case 6: Algorithms.alg6(numElements, rand); break; } return rand.nextInt(); }

/** * 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 double f1 = bigOhFunc(choice, n1); double f2 = bigOhFunc(choice, n2); return t1 * f2 / f1; }

/** * 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 (estimate - correct) / estimate; }

/** * 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 double t1 = timeAlgorithm(choice, n1); double t2 = timeAlgorithm(choice, n2); double estim_t2 = estimateTiming(choice, n1, t1, n2); return percentError(t2, estim_t2); }

/** * 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

0130913677, 978-0130913678

More Books

Students also viewed these Databases questions

Question

LO2 Distinguish among three types of performance information.

Answered: 1 week ago