Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1.) Write a class named Uniform with only a main method In main, create a Random

ID: 3629933 • Letter: 1

Question

1.) Write a class named Uniform with only a main method In main, create a Random instance and then write a simple counting loop that will generate 100 ints between 1 and 6. You can just print each int to System.out.

2.) Write a class FairDieTester. In its main, have it read an unlimited number of ints from System.in (similar to to the loop in Upper.java, using hasNextInt() to control the loop). In your main method, keep counts of the number of 1s, 2s, 3s, 4s, 5s, and 6s. After reading in all the numbers, print the tallies for each of the six numbers to examine if the die is fair or not.

Explanation / Answer

1)
import java.util.*;
public class Uniform {

    public static void main (String[] args) {
       
        Random rand = new Random();
       
        for(int i = 0; i < 100; i++){
            System.out.printf("%d ", rand.nextInt(6)+1);
        }
    }
}

2.
import java.util.*;
public class FairDieTester {

    public static void main (String[] args) {
   
        int>         int two= 0;
        int three = 0;
        int four = 0;
        int five = 0;
        int six = 0;
        int input = 0;
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
           
            input = sc.nextInt();
            if (input == 1){
                one ++;
            }else if(input == 2){
                two ++;
            }else if (input == 3){
                three ++;
            }else if (input ==4){
                four++;
            }else if (input ==5){
                five++;
            }else { six ++;}
           
        }
       
        System.out.println("One: " +one);
        System.out.println("Two: " +two);
        System.out.println("Three: " +three);
        System.out.println("Four: " +four);
        System.out.println("Five: " +five);
        System.out.println("Six: " +six);   
    }
}