Chapter 6 Exercises
Exercise 1. Create a program that calculates the roll of twenty dice and the sum of the results.
Solution:
public class Main {
public static void main(String[] args) {
int sum = 0;
//First we'll create a loop for the 20 dice.
//This way we won't have to copy and paste the same line 20 times.
for (int i=0; i<20;i++) {
//We use Math.random to generate random numbers.
//In this case we want the maximum to be six. That's why the multiplication.
//When multiplying by 6 we get numbers between 0 and 5.99. So we add 1.
//Now we have numbers between 1 and 6.99 but we must transform them into integers.
//This is done by converting the whole expression to integer with (int). As seen below.
int a =(int)(Math.random()*6 + 1);
sum = sum+a;
}
System.out.println("The sum of the 20 dice is " + sum);
}
}
Exercise 2. Java program that randomly shows 100 cards from a Spanish deck.
Solution:
public class Main {
public static void main(String[] args) {
String suit = "";
String figures = "";
//We create a for loop to show 100 cards
for (int i = 0; i <= 100; i++) {
//We create a random number from 1 to 4 to generate the suits.
int a = (int) (Math.random() * 4 + 1);
//Depending on which value comes up, we assign a suit
if (a == 1) {
suit = " coins ";
}
if (a == 2) {
suit = " swords ";
}
if (a == 3) {
suit = " cups ";
}
if (a == 4) {
suit = " clubs ";
}
//Now we create random numbers between 1 and 10.
//From 1 to 7 will correspond to normal cards
// 8 to jack, 9 to knight, and 10 to king.
int b = (int) (Math.random() * 10 + 1);
//We assign value to the figures variable as follows.
if (b==8) {
figures = "Jack";
}
if (b==9) {
figures = "Knight";
}
if (b==10) {
figures = "King";
}
//We show the results on the screen using conditionals.
if (b==1) {
System.out.println("Ace of" + suit);
}
if (b>=2&&b<=7) {
System.out.println(b + " of" + suit);
}
if (b>=8&&b<=10) {
System.out.println(figures + " of" + suit);
}
}
}
}
Exercise 3. Java program that shows 20 random numbers between 0 and 100 on the same line.
Solution:
public class Main {
public static void main(String[] args) {
for (int i=0;i<=20;i++){
int a = (int)(Math.random()*100);
System.out.print(" " + a );
}
}
}
π‘ Understanding the Range: To generate a random number between two specific values, use the formula Math.random() * (max – min) + min. Make sure you understand that Math.random() generates a number between 0.0 and 1.0, so multiplying by (max – min) and then adding min correctly adjusts the range.
Exercise 4. Java program that shows 10 random numbers between 100 and 199 and then displays which was the highest and the lowest.
Solution:
public class Main {
public static void main(String[] args) {
//We create the variables you see below.
//The smallest number we can find is 0.
//The largest number is 199.
//The idea is that when a higher or lower number comes up, it replaces the ones we have.
int highest = 0;
int lowest = 199;
for (int i=0;i<=10;i++){
int a = (int)(Math.random()*99+100);
//If the number that came up is higher than the current highest
//It becomes the new highest
if (a>highest) {
highest = a;
}
//If the number that came up is lower than the current lowest
//It becomes the new lowest
if (a<lowest) {
lowest = a;
}
}
System.out.println("The highest number is: " + highest);
System.out.println("The lowest number is: " + lowest);
}
}
Exercise 5. Create a program that generates random numbers until 24 comes up. Then, it will count all the numbers that have come up.
Solution:
public class Main {
public static void main(String[] args) {
int a = 0;
int count = 0;
while (a!=24) {
a=(int)(Math.random()*100);
count++;
}
System.out.println("The total count of numbers is: " + count);
}
}
Exercise 6. Create a program that randomly generates 20 grades and classifies them into sufficient, pass, good, and excellent.
Solution:
public class Main {
public static void main(String[] args) {
String grade = "";
for (int i=0;i<=20;i++) {
double a =(Math.random() * 10);
String result = String.format("%.2f", a);
if (a<5) {
grade = "Fail";
}
if (a>=5&&a<7) {
grade = "Pass";
}
if (a>=7&&a<9) {
grade = "Good";
}
if (a>=9) {
grade = "Excellent";
}
System.out.println(result + " ----> " + grade);
}
}
}
Exercise 7. Create a program that shows the roll of two dice until both show the same result.
Solution:
public class Main {
public static void main(String[] args) {
int a = 0;
int b = 1;
while (a!=b) {
a = (int)(Math.random() * 6+1);
b = (int)(Math.random() * 6+1);
System.out.println("The dice roll is " + a + " " + b);
}
}
π‘ Using Integers: To get integer numbers in a specific range, convert to integer with (int). Additionally, adjust the range by adding 1 to the formula: (int)(Math.random() * (max – min + 1) + min). This includes both the minimum and maximum values in the range of possible results.
Exercise 8. Java program in which we have five attempts to guess a randomly created number.
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner exercise = new Scanner(System.in);
int attempts = 5;
//We create a four-digit password. Between 1000 and 9999.
int password = (int)(Math.random()*9000+1000);
System.out.println("The password is " + password);
while (attempts >0) {
attempts--;
System.out.println("Enter a password ");
int guess = exercise.nextInt();
if (guess==password) {
System.out.println("You've guessed correctly");
break;
}
if (attempts ==0) {
System.out.println("No more attempts");
}
}
}
}
Exercise 9. We create a program that simulates a soccer pool of 9 matches. Three columns will be shown where 1, X or 2 will appear marked.
Solution:
public class Main {
public static void main(String[] args) {
System.out.println("The result of the soccer pool is the following:");
System.out.println(" ");
for (int i =1;i<=9;i++) {
int result = (int)(Math.random()*3+1);
if (result==1) {
System.out.println("Match " + i + " = " + " 1 | |");
}
if (result==2) {
System.out.println("Match " + i + " = " + " | X |");
}
if (result==3) {
System.out.println("Match " + i + " = " + " | X | 2");
}
}
}
}
π‘ Avoid Undesired Values: If you need to exclude certain values from the range, use conditionals to regenerate the random number in case an undesired value is obtained. For example, if you generate a random number and need to exclude 100, you can use a while loop to repeat the generation until a valid value is obtained.
Exercise 10. Java exercise on random numbers, but in this case using a char variable. Create a program that randomly displays the following symbols five times: &^%$.*.
Solution:
public class Main {
public static void main(String[] args) {
char a= ' ';
for (int i=1;i<=5;i++) {
int b = (int)(Math.random()*5+1);
if (b==1) {
a = '&';
}
if (b==2) {
a = '*';
}
if (b==3) {
a = '^';
}
if (b==4) {
a = '%';
}
if (b==5) {
a = '$';
}
System.out.println(a);
}
}
}