Question

In: Computer Science

Java guessing game: For this program you will use Linear Search. - Ask the user to...

Java guessing game:

For this program you will use Linear Search.

- Ask the user to choose between a number from 0 to 2000.

- The user will guess how long the linear search will take in nanoseconds,

- After the user puts in their answer, show the user the difference between the actual answer and their answer.

(Example: The user puts in 5 nanoseconds. The actual time is 11 nanoseconds. The program will show 6 nanoseconds.)

There should be a class for the program to do the Linear Search and a separate tester class for the user questions and answers.

Solutions

Expert Solution

LinearSearch.java:

Raw_code:

// LinearSearch class
public class LinearSearch{
int number = 0;
// constructor which takes number as parameter
LinearSearch(int number){
this.number = number;
}

// start method which takes user guess time as parameter
long start(long time){
// intializing start time by calling nanoTime method of System class
long startTime = System.nanoTime();
// for loop for Search the number from 0 until the number is reached
for(int num = 0; num <= number; num++);
// intializing the end time
long endTime = System.nanoTime();
// caulating the timeduration by substracting the startTime from endTime
long timeduration = endTime - startTime;
// returning the difference b/2 the timeduration and time
return timeduration - time;
}
}

Driver.java:

Raw_code:

import java.util.Scanner;
public class Driver{
// main method
public static void main(String[] args){
int number;
long time;
// taking the number form user and storing in number variable
Scanner input = new Scanner(System.in);
System.out.print("Enter a number between 0 and 2000: ");
number = input.nextInt();
// creating Linear Search object on the number
LinearSearch ls = new LinearSearch(number);
// taking the time from user and storing in time variable
System.out.print("How laong the Linear Search will take in nanoseconds: ");
time = input.nextLong();
// calling the start method of LinearSearch object with user time as argument
// and printing the return value
System.out.println("The time difference is: " + ls.start(time));
}
}


Related Solutions

Write a java program that presents the user with the following menu Linear Search Binary Search...
Write a java program that presents the user with the following menu Linear Search Binary Search The user can choose any of these operations using numbers 1 or 2. Once selected, the operation asks the user for an input file to be searched (the file is provided to you and is named input.txt). If option 1 or 2 is selected, it asks the user for the search string. This is the string that the user wants the program to search...
C++ The program implements the Number Guessing Game. However, in that program, the user is given...
C++ The program implements the Number Guessing Game. However, in that program, the user is given as many tries as needed to guess the correct number. Rewrite the program so that the user has no more than five tries to guess the correct number. Your program should print an appropriate message, such as “You win!” or “You lose!”.
// This program ask the user to enter a character. It then performs a // linear...
// This program ask the user to enter a character. It then performs a // linear search on a character array and display the number of times // that the character appears on the array. If the character is not in the // array, then it will display a message saying that is was not found. // Add the necessary code for the program to work. // NOTE: // You don't have to edit anything in the main(), just in...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user to enter a capital for a state (by getting a state/capital pair via the StateCapitals class. Upon receiving the user’s input, the program reports whether the answer is correct. The program should randomly select 10 out of the 50 states. Modify your program so that it is guaranteed your program never asks the same state within one round of 10 guesses.
In JAVA Create a simple guessing game, similar to Hangman, in which the user guesses letters...
In JAVA Create a simple guessing game, similar to Hangman, in which the user guesses letters and then attempts to guess a partially hidden phrase. Display a phrase in which some of the letters are replaced by asterisks: for example, G* T*** (for Go Team). Each time the user guesses a letter, either place the letter in the correct spot (or spots) in the phrase and display it again or tell the user the guessed letter is not in the...
in java Create simple number guessing game as follows. First, prompt the user to enter a...
in java Create simple number guessing game as follows. First, prompt the user to enter a min and max value. In your code, declare and assign a variable with a random integer in this range (inclusive). Then, ask the user to guess the number. Input the user's guess. And at the end output "Correct!" if the user's guess was right on spot, or "Sorry, the answer is not corrcet.” The number I was looking for was xxx. Replace xxx with...
Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random...
Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random number with min number being 1 and max being 10 import random min_number = 1 max_number = 10 rand = random.randint(min_number, max_number) #Prints the header, welcoming the user print("Welcome to the Guess My Number Program!") while (True): #While loop, comparing the users guessed number with the random number. #If it matches, it will say you guessed it. guess = eval(input("Please try to guess my...
Write a java program that will ask the user to enter integers (use loops) until -100...
Write a java program that will ask the user to enter integers (use loops) until -100 is entered. The program will find and display the greatest, the smallest, the sum and the average of the entered numbers. also write a separate driver class which will be used to run and display the greatest, the smallest, the sum and the average of the entered numbers. Thanks!!
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
guessing game in Java. It will have a guess input used for guessing the random number...
guessing game in Java. It will have a guess input used for guessing the random number that is generated from 1 - 100. When the user makes a guess it will tell them if the number is lower or higher than the guess. There is also a choice to give up which then reveals the correct number. The last choice will be new game which resets the random number. Last, the program should keep counts of tries. When the user...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT