Question 23 and 25 Programming Exercises 1 23. Suppose that infile is a Scanner
ID: 3607136 • Letter: Q
Question
Question 23 and 25
Programming Exercises 1 23. Suppose that infile is a Scanner object associated with the file that contains the following data: 27306 savings 7503.35. Write the Java statements that read and store the first input in the int variable acctNum- ber, the second input in the String variable accountType, and the third input in the double variable balance Suppose that you have the following statements: PrintWriter outfile; double distance375; double speed = 58; double travelTime; 24. Write Java statements to do the following: Open the file travel.dat using the variable outfile. Write the values of the variables distance and speed, to two decimal places, in the file travel.dat. Calculate and write the travelTime, to two decimal places, in the file travel.dat. a. b. c. 25. A program reads data from a file called inputFile.dat and, after doing some calculations, writes the results to a file called outFile.dat. Answer the following questions: a. After the program executes, what are the contents of the file inputFile.dat? b. After the program executes, what are the contents of the file outFile.dat if this file was empty before the program executed? .After the program executes, what are the contents of the file outFile.dat if this file contained 100 numbers before the program executed? What would happen if the file outFile.dat did not exist before the program executed? d. PROGRAMMING EXERCISES 1. Consider the following incomplete Java program public class Ch3_PrExercisel public static void main (String[] args)Explanation / Answer
23. int acctNumber = infile.nextInt();
String accountType = infile.next();
double balance = infile.nextDouble();
24. try{
File file = new File("travel.dat");
outfile = new PrintWriter(file);
outfile.println(String.format("%.2f %.2f",distance,speed));
travelTime = distance/speed;
outfile.println(String.format("%.2f",travelTime));
outfile.close();
}
catch (Exception e){
e.printStackTrace();
}
25. a) same as before execution
b) outfile will contain the results of calculation
c) Depends on what mode file is opened.If append mode, cotents will be added
otherwise contins whatever is recently written
d) If file does not exist, file will be created
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.