Question
Java I need help with this part of my code At this point, you might want to experiment with the different algorithms. The BigOh.main() program
Java
I need help with this part of my code
- At this point, you might want to experiment with the different algorithms. The BigOh.main() program takes user input and returns the timing results of your timeAlgorithm method. To run the program, browse in the Package Explorer and right-click on Lab03-->select Run As-->Java Application->BigOh (it may do this choice for you). You can select different algorithms and problem sizes and see what your timeAlgorithm method returns. Does it always return the same number for the same input?
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started