Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/* Program: * * 1. opens a file for writing * 2. prints two random numbers between 1 and 120 to the file * 3.

/* 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 *** */ }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced MySQL 8 Discover The Full Potential Of MySQL And Ensure High Performance Of Your Database

Authors: Eric Vanier ,Birju Shah ,Tejaswi Malepati

1st Edition

1788834445, 978-1788834445

More Books

Students also viewed these Databases questions

Question

Strengthen your personal presence.

Answered: 1 week ago

Question

What is the Big Bang Theory?

Answered: 1 week ago