Download the replacement CPS150_Lab10.java file
into the project source folder (e.g.,
Documents\NetBeansProjects\CPS150_Lab10\src\cps150_lab10)
In the Project explorer, right-click on the downloaded
CPS150_Lab7.java and select the Refactor >
Move... menu option
When the confirmation dialog appears, click the Refactor
button
Then:
Type your name after the @author tag
in the comment block before the class definition; i.e.:
/**
* CPS 150, Section add your section number
here
*
* Lab Project 10
*
* @author add your name here
*/
Complete the program (main method) definition
by adding the required code, incidated by the tag *** ADD
CODE HERE ***
Complete the definition for the addTwoInts
method, indicated by the tag *** ADD METHOD DEFINITION HERE
***, so that the method consumes two integers and
calculates and produces their integer sum
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 the Windows Explorer (or the Mac OS Finder) and
opening the file with a text editor (NotePad, NotePad++, WordPad,
etc.) to see the two random numbers generated by the last
execution.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
/**
* CPS 150, Section add your section number here
*
* Lab Project 10
*
* @author Chirag Tagadiya
*/
public class CPS150_lab10 {
private static final String FILENAME =
"C:\\Users\\cr\\Desktop\\random.txt";
public static int addTwoInt(int num1,int num2){
return num1+num2;
}
public static void main(String[] args) {
int c;
Random t=new Random();
int number1=t.nextInt(100);
int number2=t.nextInt(100);
System.out.println(number1);
System.out.println(number2);
int sum=addTwoInt(number1,number2);
BufferedWriter bw = null;
FileWriter fw = null;
try {
File f=new File(FILENAME);
fw = new
FileWriter(f.getAbsoluteFile(),true);
bw = new BufferedWriter(fw);
bw.write(number1+" "+number2+"
"+sum);
bw.newLine();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw !=
null)
bw.close();
if (fw !=
null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Get Answers For Free
Most questions answered within 1 hours.