Documentation Index
Fetch the complete documentation index at: https://v1-learn.neoartd.my.id/llms.txt
Use this file to discover all available pages before exploring further.
41. Randomness - Link
You know what’s cool? Having the computer randomly choose a number. This is the basis of pretty much every computer game ever.To pick a random number, you first need to import java.util.Random;.Then, you must create a random-number generator object, like so:Once that’s finished, you can have the computer pick a random integer like this:int x = 1 + r.nextInt(10);
That’ll pick a random number from 1 to 10 (inclusive) and store it into the variable x. Enough of the explaining; let’s look at some code!Files Needed :Sample Output :Your random numbers will probably be different than these. Actually, that’s kind of the point.My random number is 8
Here are some numbers from 1 to 5!
1 1 5 4 2 2
Here are some numbers from 1 to 100!
25 25 39 34 93 13
The random numbers were different! Not too surprising, actually.
import java.util.Random;
public class Randomness
{
public static void main ( String[] args )
{
Random r = new Random();
int x = 1 + r.nextInt(10);
System.out.println( "My random number is " + x );
System.out.println( "Here are some numbers from 1 to 5!" );
System.out.print( 1 + r.nextInt(5) + " " );
System.out.print( 1 + r.nextInt(5) + " " );
System.out.print( 1 + r.nextInt(5) + " " );
System.out.print( 1 + r.nextInt(5) + " " );
System.out.print( 1 + r.nextInt(5) + " " );
System.out.print( 1 + r.nextInt(5) + " " );
System.out.println();
System.out.println( "Here are some numbers from 1 to 100!" );
System.out.print( 1 + r.nextInt(100) + "\t" );
System.out.print( 1 + r.nextInt(100) + "\t" );
System.out.print( 1 + r.nextInt(100) + "\t" );
System.out.print( 1 + r.nextInt(100) + "\t" );
System.out.print( 1 + r.nextInt(100) + "\t" );
System.out.print( 1 + r.nextInt(100) + "\t" );
System.out.println();
int num1 = 1 + r.nextInt(10);
int num2 = 1 + r.nextInt(10);
if ( num1 == num2 )
{
System.out.println( "The random numbers were the same! Weird." );
}
if ( num1 != num2 )
{
System.out.println( "The random numbers were different! Not too surprising, actually." );
}
}
}
Assignments turned in without these things will not receive any points.
- Delete the
1 + in front of all six lines that pick numbers 1-5, so that they look like this: System.out.print( r.nextInt(5) + " " ); Run the program a few times, and see if you can figure out what range the new random numbers are in.
Answer: 0-4
- Change the
1 + in front of all six lines that pick numbers 1-5, so that they look like this: System.out.print( 3 + r.nextInt(5) + " " ); Run the program a few times. Is it picking random numbers from 3 to 5? If not, what range are they?
Answer: 3-7
- Change the line where you create the random number generator so that it looks like this:
Random r = new Random(12353); This number is called a seed. Run the program a few times. What do you notice? What happened to the random numbers?
Answer: The random numbers are the same
- Change to random seed to something else and observe the behavior. What happens to the random numbers?
Answer: The random numbers are different
- (Delete the random seed before turning in the assignment.)
Answer: Done
42. Magic 8-Ball - Link
Files Needed :Sample Output :MAGIC 8-BALL SAYS: It is decidedly so
MAGIC 8-BALL SAYS: Reply hazy, try again
MAGIC 8-BALL SAYS: Signs point to yes
import java.util.Random;
public class Magic8Ball
{
public static void main ( String[] args )
{
Random r = new Random();
int choice = 1 + r.nextInt(15);
String response = "";
if ( choice == 1 )
response = "It is certain";
else if ( choice == 2 )
response = "It is decidedly so";
else if ( choice == 3 )
response = "Without a doubt";
else if ( choice == 4 )
response = "Yes - definitely";
else if ( choice == 5 )
response = "You may rely on it";
else if ( choice == 6 )
response = "As I see it, yes";
else if ( choice == 7 )
response = "Most likely";
else if ( choice == 8 )
response = "Outlook good";
else if ( choice == 9 )
response = "Signs point to yes";
else if ( choice == 10 )
response = "Yes";
else if ( choice == 11 )
response = "Reply hazy, try again";
else if ( choice == 12 )
response = "Ask again later";
else if ( choice == 13 )
response = "Better not tell you now";
else if ( choice == 14 )
response = "Cannot predict now";
else if ( choice == 15 )
response = "Concentrate and ask again";
else
response = "8-BALL ERROR!";
System.out.println( "MAGIC 8-BALL SAYS: " + response );
}
}
Assignments turned in without these things will not receive any points.
- The real Magic 8-Ball™ contains 20 responses, not 15. Change the code so that it picks a random number from 1-20, and add the following five responses:
- “Don’t count on it”
- “My reply is no”
- “My sources say no”
- “Outlook not so good”
- “Very doubtful”
Answer:
int choice = 1 + r.nextInt(20);
...
else if ( choice == 16 )
response = "Don't count on it";
else if ( choice == 17 )
response = "My reply is no";
else if ( choice == 18 )
response = "My sources say no";
else if ( choice == 19 )
response = "Outlook not so good";
else if ( choice == 20 )
response = "Very doubtful";
43. A Number-Guessing Game - Link
Modify your incredibly stupid number-guessing game to actually pick a random number from 1 to 10 and have the user try to guess that. Tell them if they get it right or wrong, and if they get it wrong, show them what the random number was.They will still only get one try.
Sample Output :I'm thinking of a number from 1 to 10.
Your guess: 3
Sorry, but I was really thinking of 4.
I'm thinking of a number from 1 to 10.
Your guess: 4
Sorry, but I was really thinking of 7.
I'm thinking of a number from 1 to 10.
Your guess: 2
That's right! My secret number was 2!
import java.util.Random;
import java.util.Scanner;
public class NumberGuess
{
public static void main ( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int secret = 1 + r.nextInt(10);
int guess;
System.out.println( "I'm thinking of a number from 1 to 10." );
System.out.print( "Your guess: " );
guess = keyboard.nextInt();
System.out.println();
if ( guess == secret )
System.out.printf( "That's right! My secret number was %d!\n", secret );
else
System.out.printf( "Sorry, but I was really thinking of %d.\n", secret );
}
}
44. Fortune Cookie - Link
Write a program that simulates a random fortune from a fortune cookie. You must have at least six fortunes.For bonus points, also add randomly-chosen lotto numbers to the fortune. In Texas, the lotto chooses 6 numbers, each from 1-54.Fortune cookie says: "You will find happiness with a new love."
Fortune cookie says: "Stick with your wife."
13 - 44 - 19 - 37 - 29 - 17
import java.util.Random;
public class FortuneCookie
{
public static void main ( String[] args )
{
Random r = new Random();
int choice = 1 + r.nextInt(6);
String response = "";
if ( choice == 1 )
response = "You will find happiness with a new love.";
else if ( choice == 2 )
response = "Stick with your wife.";
else if ( choice == 3 )
response = "You will find happiness with a new love.";
else if ( choice == 4 )
response = "Stick with your wife.";
else if ( choice == 5 )
response = "You will find happiness with a new love.";
else if ( choice == 6 )
response = "Stick with your wife.";
System.out.println( "Fortune cookie says: " + response );
System.out.print( "\t" );
System.out.print( 1 + r.nextInt(54) + " - " );
System.out.print( 1 + r.nextInt(54) + " - " );
System.out.print( 1 + r.nextInt(54) + " - " );
System.out.print( 1 + r.nextInt(54) + " - " );
System.out.print( 1 + r.nextInt(54) + " - " );
System.out.print( 1 + r.nextInt(54) );
}
}
45. Dice - Link
Write a program that simulates a dice roll by picking a random number from 1-6 and then picking a second random number from 1-6. Add the two values together, and display the total.Sample Output :HERE COMES THE DICE!
Roll #1: 3
Roll #2: 5
The total is 8!
HERE COMES THE DICE!
Roll #1: 4
Roll #2: 2
The total is 6!
import java.util.Random;
public class Dice
{
public static void main ( String[] args )
{
Random r = new Random();
int choice1 = 1 + r.nextInt(6);
int choice2 = 1 + r.nextInt(6);
System.out.println( "HERE COMES THE DICE!" );
System.out.printf( "\nRoll #1: %d", choice1 );
System.out.printf( "\nRoll #2: %d", choice2 );
System.out.printf( "\nThe total is %d!", choice1 + choice2 );
}
}
46. One Shot Hi-Lo - Link
Write a program that picks a random number from 1-100. Give the user a chance to guess it. If they get it right, tell them so. If their guess is higher than the number, say “Too high.” If their guess is lower than the number, say “Too low.” Then quit. (They don’t get any more guesses for now.)Sample Output :I'm thinking of a number between 1-100. Try to guess it.
> 13
Sorry, you are too low. I was thinking of 34.
I'm thinking of a number between 1-100. Try to guess it.
> 79
Sorry, you are too high. I was thinking of 51.
I'm thinking of a number between 1-100. Try to guess it.
> 42
You guessed it! What are the odds?!?
import java.util.Random;
import java.util.Scanner;
public class OneShotHiLo
{
public static void main ( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int secret = 1 + r.nextInt(100);
int guess;
System.out.println( "I'm thinking of a number between 1-100. Try to guess it." );
System.out.print( "> " );
guess = keyboard.nextInt();
System.out.println();
if ( guess == secret )
System.out.printf( "You guessed it! What are the odds?!?\n" );
else if ( guess < secret )
System.out.printf( "Sorry, you are too low. I was thinking of %d.\n", secret );
else if ( guess > secret )
System.out.printf( "Sorry, you are too high. I was thinking of %d.\n", secret );
}
}
47. Three Card Monte - Link
Write a program that allows the user to play a simple game of Three Card Monte. Create three cards. One of the cards is the Queen. The others are Jokers. Shuffle the cards, then ask the player to pick a card. After the player picks a card, shuffle the cards again and show the player that the Queen is not where they picked. If the player picks the Queen, they win. If they don’t pick the Queen, they lose.Sample Output :You slide up to Fast Eddie's card table and plop down your cash.
He glances at you out of the corner of his eye and starts shuffling.
He lays down three cards.
Which one is the ace?
## ## ##
## ## ##
1 2 3
> 2
Ha! Fast Eddie wins again! The ace was card number 3.
## ## AA
## ## AA
1 2 3
(Note that this is basically just a number-guessing game with fancy graphics.)You slide up to Fast Eddie's card table and plop down your cash.
He glances at you out of the corner of his eye and starts shuffling.
He lays down three cards.
Which one is the ace?
## ## ##
## ## ##
1 2 3
> 2
You nailed it! Fast Eddie reluctantly hands over your winnings, scowling.
## AA ##
## AA ##
1 2 3
import java.util.Random;
import java.util.Scanner;
public class ThreeCardMonte
{
public static void main ( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int ace = 1 + r.nextInt(3);
System.out.println( "You slide up to Fast Eddie's card table and plop down your cash." );
System.out.println( "He glances at you out of the corner of his eye and starts shuffling." );
System.out.println( "He lays down three cards." );
System.out.println();
System.out.println( "Which one is the ace?" );
System.out.println();
System.out.println( "\t## ## ##" );
System.out.println( "\t## ## ##" );
System.out.println( "\t1 2 3" );
System.out.print( "\n> " );
int guess = keyboard.nextInt();
System.out.println();
if ( guess == ace )
System.out.println( "You nailed it! Fast Eddie reluctantly hands over your winnings, scowling." );
else
System.out.printf( "Ha! Fast Eddie wins again! The ace was card number %d.\n", ace );
System.out.println();
if ( ace == 1 ) {
System.out.println( "\tAA ## ##" );
System.out.println( "\tAA ## ##" );
System.out.println( "\t1 2 3" );
} else if ( ace == 2 ) {
System.out.println( "\t## AA ##" );
System.out.println( "\t## AA ##" );
System.out.println( "\t1 2 3" );
} else {
System.out.println( "\t## ## AA" );
System.out.println( "\t## ## AA" );
System.out.println( "\t1 2 3" );
}
}
}
75. Baby Blackjack - Link
Write a program that allows a human user to play a single hand of “blackjack” against a dealer.
- Pick two values from 1-10 for the player. These are the player’s “cards”.
- Pick two more values from 1-10 for the dealer.
- Whoever has the highest total is the winner.
- There is no betting, no busting, and no hitting. Save that for real blackjack.
Sample Output :Baby Blackjack!
You drew 6 and 5.
Your total is 11.
The dealer has 7 and 3.
Dealer's total is 10.
YOU WIN!
import java.util.Random;
public class BabyBlackjack
{
public static void main ( String[] args )
{
Random r = new Random();
int player1 = 1 + r.nextInt(10);
int player2 = 1 + r.nextInt(10);
int dealer1 = 1 + r.nextInt(10);
int dealer2 = 1 + r.nextInt(10);
int playerTotal = player1 + player2;
int dealerTotal = dealer1 + dealer2;
System.out.println( "Baby Blackjack!\n" );
System.out.printf( "You drew %d and %d.\n", player1, player2 );
System.out.printf( "Your total is %d.\n\n", playerTotal );
System.out.printf( "The dealer has %d and %d.\n", dealer1, dealer2 );
System.out.printf( "Dealer's total is %d.\n\n", dealerTotal );
if ( playerTotal > dealerTotal )
System.out.println( "YOU WIN!" );
else
System.out.println( "DEALER WINS!" );
}
}