Scientific Programming II

Programming in Java

Unit 2 - Assignment


Total Points: 20

Notes


Problem 1

Your program will ask the user for the number of input values. The program will then accept user input values one-by-one (see example below). The input values will be distinct and >=2. The digits within an input value need not be distinct.

For each input value:

  1. Your program has to compute the count of its non-prime digits. If a non-prime digit repeats in the input value, the repeated digit is also counted towards the count (see example below). 1 and 0 are considered as non-prime digits.
  2. The program should print the count that was computed in 1.

Finally, your program has to print the input value that has the maximum value for count. If none of the input values contain non-prime digits within them, print a line stating, “No non-prime digits in the input values”.

Output 1
How many input values
> 5
Input Value#1
> 121
121 has 2 non-prime digit(s)
Input Value#2
> 231
231 has 1 non-prime digit(s)
Input Value#3
> 678
678 has 2 non-prime digit(s)
Input Value#4
> 345
345 has 1 non-prime digit(s)
Input Value#5
> 999
999 has 3 non-prime digit(s)

999 has the maximum number of non-prime digits
Output 2
How many input values
> 3
Input Value#1
> 325
325 has 0 non-prime digit(s)
Input Value#2
> 237
237 has 0 non-prime digit(s)
Input Value#3
> 777
777 has 0 non-prime digit(s)

No non-prime digits in the input values

Problem 2

Player A and Player B play the game of multiplication as specified by the rules below:

  1. A random integer value, N in [2, 1000] is selected.
  2. An integer variable, P is assigned the value of 1.
  3. Player A always starts first.
  4. Player A randomly generates an integer value in [2, 9] and multiplies the value with P.
  5. If the value of P exceeds N, Player A wins. Game over. If the value of P is less than or equal to N, proceed to Step 6.
  6. Player B randomly generates an integer value in [2, 9] and multiplies the value with the value of P computed in Step 4.

Write a program to implement the multiplication game as per the above rules.

import java.util.Random;
 
public class Guessing_Game {
    public static void main(String[] args) {
        Random random = new Random();
        int limit = 2 + random.nextInt(999);
        System.out.println("N = " + limit);
        
        // Your code here
    }
}
Output
N = 25
A randomly selects 3.
P = 3
B randomly selects 2
P = 6.
A randomly selects 6
P = 36.
A wins!

Problem 3

In this program, you will simulate the rolling of two dice. Use the random number generator to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Your program should roll the dice 10000 times. Use an integer array of the appropriate size to tally the number of times each possible sum appears. Display the results.

Output
2 occured  269 times
3 occured  541 times
4 occured  818 times
5 occured  1079 times
6 occured  1446 times
7 occured  1648 times
8 occured  1468 times
9 occured  1109 times
10 occured  800 times
11 occured  552 times
12 occured  270 times

Problem 4

Your program should declare an integer array of input size and take distinct integer input values into it. Finally, the program should output the contents of the array in random order.

Output 1
Input the size of the array:
> 7
Input Array: 
> 1 
> 2 
> 3 
> 4 
> 5 
> 6 
> 7
Random Output: 7 2 3 6 1 5 4
Output 2
Input the size of the array:
> 7
Input Array: 
> 1 
> 2 
> 3 
> 4 
> 5 
> 6 
> 7
Random Output: 1 5 6 3 7 4 2