Question: Create a new project/folder named CPS151_Lab . Then, download the CPS151_Lab_Project.txt (below) source code file into your project/folder: Then: Type your name after the @author

Create a new project/folder named CPS151_Lab. Then, download the CPS151_Lab_Project.txt (below) source code file into your project/folder:

Then:

  1. Type your name after the @author tag in the comment block before the class definition; i.e.:

    /** * CPS 151, Section 1 * * Lab Project 2 * * @author add your name here */

  2. Complete the program (main method) definition by adding the required code, incidated by the tag *** ADD CODE HERE ***
  3. Complete the definitions for the writeNumbers and addTwoInts method, indicated by the tag *** ADD METHOD DEFINITION BELOW THE NEXT LINE ***.
  4. Compile and run the program, making sure that a random sum is generated each time. You can also confirm the program execution by navigating to your project folder in Windows Explorer Mac OS Finder and opening the file with a text editor to see the two random numbers generated by the last execution.
  5. As a challenge step, modify the program so that it uses a user-input file name (instead of the FILENAME named constant).

TXT FILE BELOW

/* * CPS151_Lab3 */ import java.io.*; import java.util.*; /** * CPS 151, Section 1 * * Lab Project 2 * * @author */ public class CPS151_LabProject2 { static Scanner fileIn; static PrintWriter fileOut; /** * CPS151_LabProject2 : int, int ; int * * Program: * * 1. opens a file for writing * 2. prints two random numbers between 1 and 120 to the file * 3. closes the file * 4. reopens the file for reading * 5. reads back the two numbers from the file * 6. calculates their sum by calling the addTwoInts method * 7. outputs the sum of the two numbers to the screen */ public static void main(String[] args) throws IOException { final String FILENAME = "numbers.txt"; int number1; int number2; int sum; // other variables Random generator = new Random(); // create the PrintWriter fileOut for the FILENAME // *** ADD CODE HERE *** // using the Random generator, assign two random integers, // each between 1 and 120, to the number1 and number2 // variables // NOTE: the Random method nextInt(n) returns a random // integer between 0 and n-1 // *** ADD CODE HERE *** // write the values of number1 and number2 to the // PrintWriter fileOut by calling the // writeNumbers method // *** ADD CODE HERE *** // close fileOut fileOut.close(); // create the Scanner fileIn (with a File object) for the FILENAME // *** ADD CODE HERE *** // using fileIn, read the numbers (ints) previously written to the file // into the input variables number1 and number2 // *** ADD CODE HERE *** // close the Scanner fileIn // *** ADD CODE HERE *** // calculate the sum of the two numbers sum = addTwoInts(number1, number2); // output the sum to the display // *** ADD CODE HERE *** } // end main method /** * writeNumbers(int, int, PrintWriter) * * method writes the two given integers to * the file using the given PrintWriter * * *** ADD METHOD DEFINITION BELOW THE NEXT LINE *** */ /** * addTwoInts(int, int) -> int * * method consumes two integers method calculates and returns their sum * * *** ADD METHOD DEFINITION BELOW THE NEXT LINE *** */ } // end class CPS151_LabProject2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!