Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package lab_03; import java.util.Scanner; public class Main { public static void main(String[] args) { long a = System.nanoTime(); int result = 7*7*6*5; long b =

package lab_03;

import java.util.Scanner;

public class Main {

public static void main(String[] args) { long a = System.nanoTime(); int result = 7*7*6*5; long b = System.nanoTime(); long timeRun = b-a; System.out.println(timeRun); Methods myObject = new Methods(); /* ----- Method 1: Subtract Recursively ----- */ //TASK: //1.Complete method 'subRec' in 'Methods' //2.Call 'subRec' for a = 10 and b = 3 //3.Print the result. Is it correct? //TODO /* ----- End Method 1 ----- */ /* ----- Experiment 1: Bounds of Recursion ----- */ //TASK: //1.Call 'subRec' for a = 100000 and b = 4 //2.Call 'subRec' for a = 4 and b = 100000 //Q.What happened? Why do you think this happened? //TODO /* ----- End Experiment ----- */ /* ----- Method 2: Sum Iteratively ------ */ //TASK: //1.Complete method 'add' in 'Methods' //2.Call 'add' for n = 5 //3.Print the result. Is it correct? //TODO /* ----- End Method 2 ----- */ /* ----- Method 3: Sum Recursively ------ */ //TASK: //1.Complete method 'addRec' in 'Methods' //2.Call 'addRec' for n = 5 //3.Print the result. Is it correct? //TODO /* ----- End Method 2 ----- */ /* ----- Experiment 2: Recursive vs. Iterative ----- */ //TASK: //1.Record the execution time for 'add' (consider using n = 10000, but alter if your // system's capabilities necessitate an 'n' alteration) //2.With the same 'n', record the execution time for 'addRec' //Q.Which is faster? //3.Begin increasing 'n' //Q.Which is able to better handle the increasingly large 'n's? //4.Instantiate two class variables in 'Methods' one called 'addCounter' and one // called 'addRecCounter' to track the number of iterations and recursive calls, // respectively. //Q.What is the relationship between 'n' and the number of iterations required for // 'add'? What about for 'addRec'? //TODO /* ----- End Experiment 2 ----- */ /* ----- Method 4: Search Sequentially ----- */ //TASK: //1.Complete 'sequentialSearch' in 'Methods' //2.Search for '4' in an array containing {1,2,3,4,5,6,7,8,9,10} //Q.Does it return the proper index? //3.Search for '11' in that same array //Q.Does it return '-1'? //TODO /* ----- End Method 4 ----- */ /* ----- Method 5: Search by Dividing Recursively ----- */ //TASK: //1.Complete 'searchRec' in 'Methods' //2.Search for '4' in an array containing {1,2,3,4,5,6,7,8,9,10} //Q.Does it return the proper index? //3.Search for '11' in that same array //Q.Does it return '-1'? //TODO /* ----- End Method 5 ----- */ /* ----- Method 6: Search by Dividing Iteratively ----- */ //TASK: //1.Complete 'searchIter' in 'Methods' //2.Search for '4' in an array containing {1,2,3,4,5,6,7,8,9,10} //Q.Does it return the proper index? //3.Search for '11' in that same array //Q.Does it return '-1'? //TODO /* ----- End Method 6 ----- */ /* ----- Experiment 3: Search Speed and Call/Iteration Count ----- */ //TASK: //1.Create a sorted double array of length 100,000 with numbers 0-99,999 //2.Let your search value be '99999' //3.Instantiate three class variables in 'Methods': 'sequentialSearchCounter', // 'searchRecCounter', and 'searchIterCounter' to track the number of iterations/ // recursions that occur during a given method call //4.Run sequentialSearch, searchRec, and searchIter with these inputs //Q.How long did each take to run? //Q.How many iterations/recursions were required? //TODO /* ----- End Experiment 3 ----- */ } }

// Another Class File Methods.java

package lab_03;

import interfaces.IMethods;

/** * This class must implement the interface IMethods. * * @author STUDENT * */

public class Methods implements IMethods {

@Override public int subRec(int a, int b) { // TODO Auto-generated method stub return 0; }

@Override public int add(int n) { // TODO Auto-generated method stub return 0; }

@Override public int addRec(int n) { // TODO Auto-generated method stub return 0; }

@Override public int sequentialSearch(double[] arr, double x) { // TODO Auto-generated method stub return 0; }

@Override public int searchRec(double[] arr, int from, int to, double x) { // TODO Auto-generated method stub return 0; }

@Override public int searchIter(double[] arr, int from, int to, double x) { // TODO Auto-generated method stub return 0; }

}

// Another File called IMethods.java

package interfaces;

/** * * This interface provides a template for the methods to be created and tested * It must be implemented ('implements IMethods') in the Methods class * * **DO NOT CHANGE THIS INTERFACE** * */

public interface IMethods { /** * * Method subRec: * Subtracts b from a * At each recursion b is decremented by 1 * Do this until b = 0 * * @param a is the left operand * @param b is the right operand * @return the result of the operation * */ int subRec(int a, int b); /** * * Method add: * Adds from digits from 1 to n {1+2+3+4+5+...+n} * This is achieved iteratively * * @param n is the upper bound of the sequence * @return the sum * */ int add(int n); /** * * Method addRec: * Adds from digits from 1 to n {1+2+3+4+5+...+n} * This is achieved recursively * * @param n is the upper bound of the sequence * @return the sum */ int addRec(int n); /** * * Method sequentialSearch: * Searches a sorted array sequentially for an element 'x' * * @param arr is the array to be searched * @param x is the element to be searched for * @return the index of 'x', or '-1' if 'x' is not present in 'arr' * */ int sequentialSearch(double[] arr, double x); /** * * Method searchRec: * Searches a sorted array by dividing the array in two, checking if the desired value is * above, below, or equal to the middle value. * If element at middle is equal, then index of middle is returned * Else, the appropriate half of the array is passed back to the method * * @param arr is the array to be searched * @param from is the beginning index of the section of the array to be searched * @param to is the ending index of the section of the array to be searched * @param x is the element to be searched for * @return the index of 'x' or '-1' if 'x' is not present in 'arr' * */ int searchRec(double[] arr, int from , int to, double x); /** * Method searchIter: * Searches a sorted array by dividing the array in two, checking if the desired value is * above, below, or equal to the middle value. * If element at middle is equal, then index of middle is returned * Else, 'to' or 'from' is adjusted accordingly, 'middle' is recalculated, and the * while loop executes again * * @param arr is the array to be searched * @param from is the beginning index of the section of the array to be searched * @param to is the ending index of the section of the array to be searched * @param x is the element to be searched for * @return the index of 'x' or '-1' if 'x' is not present in 'arr' * */ int searchIter(double[] arr, int from, int to, double x); }

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

Database Concepts International Edition

Authors: David M. Kroenke

6th Edition International Edition

0133098222, 978-0133098228

More Books

Students also viewed these Databases questions

Question

When given an annual rate, what should you divide by

Answered: 1 week ago

Question

What are the functions of top management?

Answered: 1 week ago

Question

Bring out the limitations of planning.

Answered: 1 week ago

Question

Why should a business be socially responsible?

Answered: 1 week ago

Question

Discuss the general principles of management given by Henri Fayol

Answered: 1 week ago