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

Java Programming: Problem 3 (name this Lab10_Problem3) Modify the program below

ID: 3763669 • Letter: J

Question

Java Programming:

Problem 3 (name this Lab10_Problem3)

Modify the program below so it runs continuously until the user chooses a value of 0 which is the signal to quit. In other words, the user input can have three outcomes:

An integer between 1 and 21 (good data)

A value of 0 which indicates the program should terminate; the user wants to quit

A value < 0 and >= 22 (bad data)

With your enhancement, the human can keep playing until bored or decides to play a real video game on a computer, XBox, PS4, or—in a very rare occurrence, outdoors.

Next, add a means to log the results to a file on disk, with each log entry showing who won. Create the file using the fully qualified filename c:homestudentlastnameLab10_Problem2_log.txt

Construct the log file to look like this example. Note each line starts with a timestamp followed by the outcome (who won).

Try to get the date and time to look like the example, but don't stress too much—you won't lose points if it is displayed in another style as long as it's recognizable as a date and time to the second. In the style shown in this example, it's using 24-hour time and single-digit days and months are padded with a leading 0. The may be a Java method for that! Explore.

04/17/2014 17:28:15: Computer won
04/17/2014 17:29:13: Human won
04/17/2014 17:31:04: Human won

import java.util.Random;
import java.util.Scanner;

public class Lab10_Problem3 {

   public static void main(String args[]) {

       int input_capture = fiGetNum();
       if (input_capture >= 1 && input_capture <= 21) {
           int guessNo = fiGuess();
           System.out.println("Computer guessed " + guessNo);
           if (guessNo == input_capture) {
               System.out.println("Computer won. It guessed your number.");
           } else {
               System.out.println("Human won! The computer guessed wrong.");
           }

       } else {
           System.out.println("Error: must be from 1 to 21, inclusive");
       }
       System.out
               .println("User input is within range; computer wins; results displayed and program stops:");

   }

   public static int fiGuess() {
       int min = 1, max = 21;
       Random rand = new Random();
       int randomNum = rand.nextInt((max - min) + 1) + min;
       return randomNum;

   }

   public static int fiGetNum() {
       Scanner scanner = new Scanner(System.in);
       System.out.print("Enter a number from 1 to 21:");
       int input_capture = scanner.nextInt();
       return input_capture;

   }
}

Explanation / Answer

Please find the required solution:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;

public class Lab10_Problem3 {
   public static void main ( String args[] ) throws IOException
   {
       // instantiate scanner for reading input from console
       Scanner scannerConsole = new Scanner( System.in );

       // output file and the directory does not exist create them
       File outputFile = new File( "c:/home/student/lastname/Lab10_Problem2_log.txt" );
       if ( !outputFile.exists() )
       {
           if ( outputFile.getParentFile() != null && !outputFile.exists() )
               outputFile.getParentFile().mkdirs();
           outputFile.createNewFile();
       }

       // instantiate scanner for writing result to file
       BufferedWriter writer = new BufferedWriter( new FileWriter( outputFile ) );

       // actual logic goes here
       int input_capture;

       // iterate until user enters input 0
       do
       {
           input_capture = fiGetNum( scannerConsole );
           if ( input_capture >= 1 && input_capture <= 21 )
           {
               int guessNo = fiGuess();

               System.out.println( "Computer guessed " + guessNo );
               if ( guessNo == input_capture )
               {
                   writer.write( new Date().toString() + ",Computer won" );
                   System.out.println( "Computer won. It guessed your number." );
               }
               else
               {
                   writer.write( new Date().toString() + ",Human won" );
                   System.out.println( "Human won! The computer guessed wrong." );
               }
               writer.newLine();
           }
           else if ( input_capture == 0 )
           {
           }
           else
           {
               // error message if the input not in range
               System.out.println( "Error: must be from 1 to 21, inclusive" );
           }
       } while ( input_capture != 0 );

       // close the resources to avoid memory leak
       writer.close();
       scannerConsole.close();
   }

   /* Method that generate the random integer in range 1 and 21 */
   public static int fiGuess ()
   {
       int min = 1, max = 21;
       Random rand = new Random();
       int randomNum = rand.nextInt( (max - min) + 1 ) + min;
       return randomNum;
   }

   /* Method used to read input from console */
   public static int fiGetNum ( Scanner scannerConsole )
   {
       System.out.print( "Enter a number from 1 to 21:" );
       int input_capture = scannerConsole.nextInt();
       return input_capture;
   }
}

Sample output file:c:homestudentlastnameLab10_Problem2_log.txt

Wed Nov 18 18:47:15 IST 2015,Human won
Wed Nov 18 18:47:17 IST 2015,Human won
Wed Nov 18 18:47:20 IST 2015,Human won
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote