Java Reference
In-Depth Information
18. Consider the statements:
double x = 75.3987;
double y = 982.89764;
What is the output of the following statements?
a. System.out.printf("%.2f %n", x);
b. System.out.printf("%.2f %n", y);
c. System.out.printf("%.3f %n", x);
d. System.out.printf("%.3f %n", y);
19. Consider the statements:
int x, y;
char ch;
and the input:
46 A 49
Write the Java statements that would store 46 into x , 'A' into ch ,and 49 into y .
20. The following program is supposed to read two numbers from a file named
Ex20Input.txt , and write the sum of the numbers to a file named
Ex20Output.dat .However,itfailstodoso.Rewritetheprogramsothatit
performs correctly. (You may assume that both numbers are on the same line.)
import java.util.*;
public class Ch3Ex20
{
public static void main(String[] args)
{
Scanner inFile ΒΌ
new Scanner ( new FileReader("Ex20Input.txt"));
int num1, num2;
num1 = inFile.nextInt();
num2 = inFile.nextInt();
outFile.println("Sum = " + (num1 + num2));
outFile.close();
}
}
21. Which package must be imported to use the class PrintWriter ?
22.
Suppose that infile is a Scanner object and employee.dat is a file that
contains employees' information. Write the Java statement that opens this
file using the variable infile .
Search WWH ::




Custom Search