1a Write a program that prompts the user to enter the maximum number of asterisk
ID: 3673995 • Letter: 1
Question
1a Write a program that prompts the user to enter the maximum number of asterisks (*) to display on a line of output, as well as a choice of two different output patterns. The program shall then display the asterisks according to one of the patterns below. For example, if the user specifies 10 as the maximum number of asterisks, the output should look like one of the following: Choice = 1 Choice = 2 * ** *** **** ***** ****** ******* ******** ********* ********** ********** ********* ******** ******* ****** ***** **** *** ** *
1b. Write a program that asks a user to guess a secret number between 1 and N, where N is a positive number that the user is prompted for. The program should also prompt the user for the maximum number of guesses they would like to make. Each time the player makes a guess, the program shall respond with “correct”, “too low”, or “too high”. The program should keep track of the number of guesses the user made to discover the secret number. The program should continue execution until the user has discovered the secret number or has exceeded the maximum number of guesses. The program shall also allow the user to play the game again until the user declines. The program should generate the magic number randomly, using the built-in Java method Math.random(). To generate a random number between 1 and N, the following formula may be used: randomNumber = (int) ( N * Math.random() ) + 1
Explanation / Answer
/*********************************** Asterisks.java ******************************/
import java.util.Scanner;
public class Asterisks {
public static void main(String[] args) {
System.out.print("Enter the maximum number of asterisks to display: ");
Scanner s = new Scanner ( System.in );
int n = s.nextInt();
System.out.print("Pattern you want 1 or 2: ");
int pattern = s.nextInt();
int i = 1,j;
if (pattern == 1) {
while ( n > 0 ){
for( j = 1; j <= i; j++ )
System.out.print("*");
System.out.print(" ");
i++;
n--;
}
}
if (pattern == 2) {
while ( n > 0){
for( j = 1; j <= n; j++ )
System.out.print("*");
System.out.print(" ");
n--;
}
}
/***************************************** Guessing.java *********************/
import java.util.Scanner;
public class Guessing {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter number N: ");
int N = s.nextInt();
int p = N;
N = (int)(Math.random() * N + 1);
System.out.print("Maximum number of guesses you would like to make ");
int guess = s.nextInt();
int i = 0;
while ( i < guess ){
System.out.print("Guess the number between 1 and "+p+" ");
int n = s.nextInt();
if( n > N ) System.out.print("Guess is too high ");
else if( n < N ) System.out.print("Guess is too low ");
else {
System.out.print("Correct ");
System.out.println("Well guessed!");
break;
}
i++;
System.out.print("Enter -1 to give up 1 to continue ");
int t = s.nextInt();
if(t == -1) break;
if ( i == guess ) System.out.print(" You are out of guesses. Thanks for Playing ");
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.