From FANG
|
01 packagepackage is used to name the directory or folder a class is in scg.ch12;
02
03 importimport means to make the classes and/or packages available in this program java.io.File;
04 importimport means to make the classes and/or packages available in this program java.io.FileNotFoundException;
05 importimport means to make the classes and/or packages available in this program java.io.PrintWriter;
06 importimport means to make the classes and/or packages available in this program java.util.Scanner;
07
08 /**
09 * Prompt user forfor is a looping structure for repeatedly executing a block of code two integers. Print the sum of the two integers.
10 * Program then halts.
11 */
12 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects FileTwoNumbersRight {open braces start code blocks and must be matched with a close brace
13 /**
14 * The mainThe main method is the place where applications begin executing. program. Prompt user forfor is a looping structure for repeatedly executing a block of code two integers and add them
15 * together.
16 *
17 * @paramthis is the Javadoc tag for documenting the purpose of parameters args command-line arguments; ignored by thisthis means the current object (the implicit parameter) program
18 */
19 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) voidvoid means the method does not return a value mainThe main method is the place where applications begin executing.(String[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays args) {open braces start code blocks and must be matched with a close brace
20 System.out.println("FileTwoNumbersRight:");
21 Scanner keyboard =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Scanner(System.in);
22
23 System.out.print("Results file name: ");
24 String fname =this assignment operator makes the left side equal to the right side keyboard.nextLine();
25 File outFile =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor File(fname);
26 trytry is for executing a code block that may experience exceptions (errors) {open braces start code blocks and must be matched with a close brace
27 PrintWriter out =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor PrintWriter(outFile);
28
29 System.out.print("Number: ");
30 intint is the type for whole numbers and it is short for integer first =this assignment operator makes the left side equal to the right side keyboard.nextInt();
31
32 System.out.print("Number: ");
33 intint is the type for whole numbers and it is short for integer second =this assignment operator makes the left side equal to the right side keyboard.nextInt();
34
35 // Print results in the file
36 out.println("Sum of " +adds two numbers together or concatenates Strings together first +adds two numbers together or concatenates Strings together " + " +adds two numbers together or concatenates Strings together second +adds two numbers together or concatenates Strings together " = " +adds two numbers together or concatenates Strings together
37 (first +adds two numbers together or concatenates Strings together second));
38 out.close();
39 }close braces end code blocks and must match an earlier open brace catchcatch means to handle an exception that may occur (FileNotFoundException e) {open braces start code blocks and must be matched with a close brace
40 e.printStackTrace();
41 System.err.println(
42 "Program cannot make useful progress: terminating.");
43 System.exit(1);
44 }close braces end code blocks and must match an earlier open brace
45 }close braces end code blocks and must match an earlier open brace
46 }close braces end code blocks and must match an earlier open brace
47
48 //Uploaded on Mon Mar 29 21:38:38 EDT 2010
|
Download/View scg/ch12/FileTwoNumbersRight.java