Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code Provided BinarySearch Class import java.util.Scanner; public class BinarySearch { public static void main(String[] args) { Scanner scr = new Scanner(System.in); System.out.println(Starting Program and initializiing

Code Provided

BinarySearch Class

import java.util.Scanner;

public class BinarySearch {

public static void main(String[] args) {

Scanner scr = new Scanner(System.in);

System.out.println("Starting Program and initializiing arrays.");

//IF your computer takes a very long time to create the arrays, you may decrease the size a little

int size = 100000000;

int[] unsortedArray = new int[size];

int[] sortedArray = new int[size];

for(int i = 0; i < unsortedArray.length; i++) {

unsortedArray[i] = i;

sortedArray[i] = i;

}

System.out.println("Starting to randomize. This may take several seconds.");

randomize(unsortedArray);

String input = "";

long start, totalTime;

int val, index;

while(!input.equals("quit")) {

System.out.print("Please enter a number to search for (between 0 and " + (size-1) + "): ");

input = scr.nextLine();

if(input.equals("quit")) {

continue;

}

val = Integer.parseInt(input);

start = System.nanoTime();

index = linearSearch(val , unsortedArray);

totalTime = System.nanoTime() - start;

displayResults(val,index,totalTime, "linear unsorted");

start = System.nanoTime();

index = linearSearch(val , sortedArray);

totalTime = System.nanoTime() - start;

displayResults(val,index,totalTime, "linear sorted");

start = System.nanoTime();

index = binarySearch(val , sortedArray);

totalTime = System.nanoTime() - start;

displayResults(val,index,totalTime, "binary search");

System.out.println();

}

scr.close();

System.out.println("Goodbye.");

}

public static void displayResults(int val, int index, long totalTime, String searchStyle) {

if(searchStyle.equals("linear unsorted")) {

System.out.println(" Linear Unsorted Search Results:");

} else if (searchStyle.equals("linear sorted")) {

System.out.println(" Linear Sorted Search Results:");

} else {

System.out.println(" Binary Search Results:");

}

if(index == -1) {

System.out.println("The number <" + val +"> was not found. It took " + totalTime + " to search.");

} else {

System.out.println("The number <" + val +"> was found at index <" + index + ">. It took " + totalTime + " nano seconds to search.");

}

}

/**

* Randomly select two different indices in the array and swap them.

* If your array size is n, perform n/2 swaps.

*

* @param array

*/

public static void randomize(int[] array) {

//TODO

}

/**

* Search the array for the passed in val using a sequential search.

* Return the location of val when found.

* If it isn't found return -1.

*

* @param val

* @param array

* @return index or -1

*/

public static int linearSearch(int val, int[] array) {

//TODO

}

/**

* Search the array for the passed in val using binary search.

* Return the location of val when found.

* If it isn't found return -1.

*

* @param val

* @param array

* @return

*/

public static int binarySearch(int val, int[] array) {

//TODO

}

}

------------------------------

Movie Class

public class Movie {

private String name;

private int minutes;

private int tomatoScore;

public Movie(String name, int minutes){

this(name, minutes, 0);

}

public Movie(String name, int minutes, int tomatoScore){

this.name = name;

this.minutes = minutes;

this.tomatoScore = tomatoScore;

}

public int getMinutes(){

return this.minutes;

}

public String getName(){

return this.name;

}

public int getTomatoScore(){

return this.tomatoScore;

}

public void setTomatoScore(int score){

if(score >= 0 && score <= 100){

this.tomatoScore = score;

}

}

public boolean isFresh(){

return this.tomatoScore >= 60;

}

private String convertMinutes(){

return "" + (this.minutes/60) + "hrs " + (this.minutes%60) + "min";

}

public boolean equals(Object other){

if (other instanceof Movie){

Movie tmp = (Movie)other;

return tmp.name.equals(this.name) && tmp.minutes == this.minutes && tmp.tomatoScore == this.tomatoScore;

}

return false;

}

public String toString(){

return "Name: " + this.name + " Length : " + convertMinutes() +

" Tomato Rating : " + (isFresh() ? "Fresh" : "Rotten");

}

}

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 Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago