Question

<Java> 1-Write a program that converts from a string into an integer number. For example: For...

<Java>

1-Write a program that converts from a string into an integer number.


For example:

For "123" the program should return 123

For "131", The program should return 131

For "-121" the program should return -121

For "1e3" the program should print an Invalid number

For "a1" the program should print an invalid number

Do not use Integer.parse function



2. Generate a random number in [0, 100]. Write an efficient guess procedure that takes no more than 7 guesses to get any given integer in that range. For each guess, the hint can be one of the following:• Congratulations! You got it.• The guess is too large.• The guess is too small.
Here is a sample output.


Guess #1: 50
The guess is too big.
Guess #2: 24
The guess is too small.
Guess #3: 37
The guess is too big.
Guess #4: 30
The guess is too small.
Guess #5: 33
The guess is too big.
Guess #6: 31
The guess is too small.
Guess #7: 32

Congratulations!

What if the random number is in [0, 1000], how many guesses your program will take?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Question 2

import java.util.Random;
import java.util.Scanner;

class Main {
public static void main(String[] args) {
  
int nbr = (int)(Math.random() * 101), gs = 1;
Scanner sc = new Scanner(System.in);
int gess, count = 0;
  
System.out.printf("\nGuess #%d: ", gs);
while(count < 10)
{
gess = sc.nextInt();
count++;
  
if(gess < nbr)
System.out.println("Your guess is too small.");
  
else if(gess > nbr)
System.out.println("Your guess is too big.");

if(gess == nbr)
{
System.out.print("\nCongratulations! You got it.");
break;
}

if(count == 7)
{
System.out.print("\nSorry, you didn't guess the answer in 7 tries.\nThe number was " + nbr + ".");
break;
}
System.out.printf("\nGuess #%d: ", ++gs);
}
}
}

// For [0, 1000] would take 10 guesses because 2^10 = 1024

// Please note that we are allowed to answer one question at a time. So, post accordingly.

Add a comment
Know the answer?
Add Answer to:
<Java> 1-Write a program that converts from a string into an integer number. For example: For...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that prompts the user for an integer that the player (maybe the user,...

    Write a program that prompts the user for an integer that the player (maybe the user, maybe someone else) will try to guess. If the player's guess is higher than the target number, the program should display "too high" If the user's guess is lower than the target number, the program should display "too low" The program should use a loop that repeats until the user correctly guesses the number. Then the program should print how many guesses it took....

  • Write a C++ program using "for" loop to allow user to guess a certain number say...

    Write a C++ program using "for" loop to allow user to guess a certain number say 55. The loop will end after 5 guesses or if he guesses the right number, which ever comes first. If user enters a number greater than 55, your program should print "You entered a bigger number". If user enters a number smaller than 55, your program should print "You entered a smaller number". If user enters 55 in less than 5 attempt, your program...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

  • Create a HTML with Java Script such that you input a number between 222 and 333....

    Create a HTML with Java Script such that you input a number between 222 and 333. Then the user needs to guess the number. If the user’s guess is higher than the number, the program should display “Too high, try again.” If the user’s guess is lower than the number, the program should display “Too low, try again.” If the difference is less than 10, it should also mention "too close". When the user guesses the number, the program should...

  • In c# create a program that generates a random number from 1 to 1000. Then, ask...

    In c# create a program that generates a random number from 1 to 1000. Then, ask the user to guess the random number. If the user's guess is too high, then the program should print "Too High, Try again". If the user's guess is too low, then the program should print "Too low, Try again". Use a loop to allow the user to keep entering guesses until they guess the random number correctly. Once they figure it, congratulate the user....

  • In Java, write a recursive method that converts an integer to hexadecimal. The function should print...

    In Java, write a recursive method that converts an integer to hexadecimal. The function should print out the hexadecimal character instead of returning the character. Write a test program that prompts the user to enter an integer and displays its hexadecimal equivalent.

  • (c++) Write a program that generates a random number between 1 and 100 and asks the...

    (c++) Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Be sure that your program...

  • IN BASIC JAVA and using JAVA.UTIL.RANDOM write a program will randomly pick a number between one...

    IN BASIC JAVA and using JAVA.UTIL.RANDOM write a program will randomly pick a number between one and 100. Then it will ask the user to make a guess as to what the number is. If the guess is incorrect, but is within the range of 1 to 100, the user will be told if it is higher or lower and then asked to guess again. If the guess was outside the valid range or not readable by Scanner class, a...

  • Required in JAVA language Write a program that enables a user to play number guessing games....

    Required in JAVA language Write a program that enables a user to play number guessing games. The following is a nutshell description of a number guessing game. + a random number is generated + loop prompting the user to enter guesses until the user guesses the number or hits the maximum number of allowed guesses or enters the "quit" sentinel value The rest of this specification documents number guessing games in more detail. The documentation uses manifest constants that can...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT