Question

Write a script that works in vim: We are given a Java program RandomTest.java that generates...

Write a script that works in vim:

We are given a Java program RandomTest.java that generates specified number of random integers in the range [0...99]. The program prints FAIL if either 0 or 99 is generated (along with a count of how many times). Otherwise it prints PASS. The program takes the number of random integers to generate as a command line argument as shown below. Note that the given results each time is is run.

[an@localhost ~] java Random Test

Usage: java RandomTest<numRandoms>

[an@localhost ~] java RandomTest 10

PASS

[an@localhost ~] java RandomTest 10

Fail 2

[an@localhost ~] java RandomTest 10

FAIL 1

Write a shell script named tester.sh that compiles RandomTest.java and then runs RandomTest a hundred times, with numRandoms=10 each time, and keeps track of the number of times it passes. (Hint: use a for loop) The output looks like as follows (where the pass rate varies because of random numbers):

[an@localhost ~] ./tester.sh

For numRandoms = 10 pass rate = 79

Modify your shell script such that it runs the tests for numRandoms= 10, 20, 30, and 40. For each number of randoms, it prints out he pass rate (Hint: use a nested for loop). For example, the output may now look like as shown below:

[an@localhost ~] ./tester.sh

For num Randoms = 10 pass rate = 82

For num Randoms = 20 pass rate = 56

For num Randoms = 30 pass rate = 60

For num Randoms = 40 pass rate = 42

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This seems to be a very interesting Integration problem where you need to invoke your java program through a shell script.

PART ONE: JAVA PROGRAM

Consider the following java program to understand the basic working of how to generate random numbers in java:

******************************START OF PROGRAM*********************************

import java.util.*;
class RandomTest{
public static void main(String[] args) {
int count_0_99=0; //Counter to hold count of 0 and 99
Random rnum = new Random();
for (int counter = 0; counter <= Integer.parseInt(args[0]); counter++) {
int a = rnum.nextInt(100);
       if (a==99 || a==0)
           {

count_0_99++; //Incrementing Counter
           }
         
}
  
   if(count_0_99 == 0)
       System.out.println("PASS");
   else
       System.out.println("FAIL " + count_0_99);   
      
}
}

********************END OF JAVA PROGRAM**************************

PART TWO: WRITING A SHELL SCRIPT

Shell scripts are also called as bash scripts. They have extension of .sh or .bash. They are an abbreviation of Bourne-Again Shell.

* Writing a shell script is a very handy tool and it helps us to do numerous operations like searching, storing, printing etc.

* Now we will write a shell script to run the above java program 100 times for generating 10 integers and take actions accordingly.

* Make sure to read comments inside the code at all the important places

**************************BASH SCRIPT*************************************

#!/bin/bash
counter = 0
for i in {1..100}
do
javac RandomTest.java                  #COMPILING JAVA PROGRAM
returnValue = $(java RandomTest 10)    #RUNNING JAVA PROGRAM FOR 10 INTEGERS and STORING ITS OUTPUT            
if [[ $returnValue = *PASS* ]]         #Searching inside the variable to detect "PASS" or "FAIL"
then
    let "counter+=1"                   #PASS COUNTER
fi
done

echo "For numRandoms=10 pass rate=$counter

*************************END OF BASH SCRIPT***********************************

PART THREE: MODIFICATION TO SHELL SCRIPT

This part contains the modification to shell script to run for 10,20,30,40 integers the same test that we carried above.

* The idea here is we are going to use nesting of loops.

* The First loop will iterate from 10,20,30 and 40 whereas the second loop will do the same as above. The only difference is the arguments passed in the above will now change. e.g. For the first iteration, 10 random numbers will be processed, for 2nd iteration 20 random numbers will be processed and so on.

* Please read the comments at all important sections of code.

* HAPPY CODING :)

*************************START*************************

#!/bin/bash

for j in {10..40..10}                      #LOOP TO ITERATE OVER 10,20,30,40
do
    counter = 0                            #COUNTER FOR HOLDING PASS 
    for i in {1..100}
    do
    javac RandomTest.java                  #COMPILING JAVA PROGRAM
    returnValue = $(java RandomTest $j)    #RUNNING JAVA PROGRAM and STORING ITS OUTPUT IN A VARIABLE
    if [[ $returnValue = *PASS* ]]         #Searching inside the variable to detect "PASS" or "FAIL"
    then
        let "counter+=1"                   #INCREMENTING PASS COUNTER
    fi
    done
    echo "For numRandoms=$j pass rate=$counter"
done

***************************COMPLETE*******************************

All the best and don't forget to thumbs up if it really helped you :)

Add a comment
Know the answer?
Add Answer to:
Write a script that works in vim: We are given a Java program RandomTest.java that generates...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Programming: Make a Java program with two processes, a producer and a consumer. If you...

    Java Programming: Make a Java program with two processes, a producer and a consumer. If you want to use another language, clear it with me first. The producer process consists of a loop that writes the loop count (a value from 0 to 4) into a variable that it shares with the consumer process (this variable is to be initialized to 100). On each pass through the loop, before the producer writes into the shared variable, it does a random...

  • Java: Write a program that prompts the user to enter integers in the range 1 to...

    Java: Write a program that prompts the user to enter integers in the range 1 to 50 and counts the occurrences of each integer. The program should also prompt the user for the number of integers that will be entered. As an example, if the user enters 10 integers (10, 20, 10, 30, 40, 49, 20, 10, 25, 10), the program output would be: 10 occurs 4 times 20 occurs 2 times 25 occurs 1 time 30 occurs 1 time...

  • Write a program in python that generates X random integers Num. Num is a random number...

    Write a program in python that generates X random integers Num. Num is a random number between 20 to 50. X is a random number between 10 to 15. Calculate and show the Smallest, Largest, Sum, and Average of those numbers. You are not allowed to use Python functions sample(), min(), max(), average(), sort(), sorted()!! HINTs: to find Smallest.... 1) X is a random number between 10 to 15... for example 11...this determines how many times the loop will happen...

  • Copy/Paste your script from 5b here. 4. Let's look at a more complicated script executable. a....

    Copy/Paste your script from 5b here. 4. Let's look at a more complicated script executable. a. Write the following as script6.sh and change its permissions to count for num in d do if num eq 100 then count (count+1) done echo The number 10 was found $count times What is the output? Run the script as ./script6.sh 10 20 30 10 40 20 50 10 60 <enter Summarize what the script does r using the read command. The following script...

  • . Write a complete Java program that is to be used for a psychology study into...

    . Write a complete Java program that is to be used for a psychology study into random number choices by a human volunteer. Your program is to operate as follows. (Programs that correctly carry out some of the tasks will receive partial credit. Your program should not be more than 30 lines long.) Ask the user (the volunteer) to repeatedly type 2 digit numbers onto the screen. Read the user input and discard any number that is less than 10...

  • Java Submit a program in a .java file. The program should print a multiplication table for...

    Java Submit a program in a .java file. The program should print a multiplication table for the numbers 1-9. This requires a nested for loop. The outer loop moves from row to row, while the inner loop prints the row. Use tabs to separate the output, such as in the following statement:      System.out.println(“1\t2\t3\t4\t5\t6\t7\t8\t9”); Output should look similar to the following. 1      2     3     4     5     6     7     8     9 ------------------------------------------------------------------ 1       2     3     4     5     6        7     8     9...

  • Please complete the lab following the guidelines using JAVA Activity 1 Write a program called AnalyzeNumbers....

    Please complete the lab following the guidelines using JAVA Activity 1 Write a program called AnalyzeNumbers. This program will have array that holds 10 random integers from 1 to 5. Output the 10 random numbers to the screen using an enhanced for loop. This program will also have a method called frequency that takes an integer array as the parameter and it will return an array that holds the frequency of numbers. Index 0 will hold the frequency of 1,...

  • 5. Write a program that does to perform the following using: Reads a number num that...

    5. Write a program that does to perform the following using: Reads a number num that is in the range from 0 to 999999999 and a digit n that is in the range from 0 to 9 from the user. Write while loop to force user to enter appropriate values. . Finds how many times the digit n occurs in the number num. Prints the output as shown. Eİ CAWindowsisystem32cmdexe Enter number ( to 999999999): 3545592 the digit S appears...

  • For JAVA Please post code in bold. Thank you. Print numbers 0, 1, 2, ..., userNum...

    For JAVA Please post code in bold. Thank you. Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex: userNum = 3 prints: 0 1 2 3 Challenge 4.6.1: Nested loops ndent...

  • 1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The...

    1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The program should use the for loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. If the user enters a zero or negative number, a message should be given and the program should not continue (see Sample Run...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT