Download Discussion questions for week 13

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Questions for Discussion (Week 13)
For Chapter 14 Exception Handling: Try out the Review Questions on pages 592 – 594 on your
own.
For Chapter 15 Files: Try out the Review Questions 1 – 8 on pages 635 – 636 on your own. (For
Chapter 15, we will focus on text files.)
Question 1.
Consider the following method:
public int dummy() throws java.io.IOException {
int value = 0;
try {
value = 1;
doIt();
value = 2;
}
catch (NullPointerException e) {
value += 10;
}
catch (RuntimeException e) {
value += 20;
}
return value;
}
What value does the method return if doIt() method:
a. throws a NullPointerException?
b. throws an ArithmeticException?
c. throws a java.io.IOException?
d. completes successfully?
- 1 of 4 -
Question 2
[Page 594] Refer to the program below.
/***************************************************
* FantasyFootball.java
* Dean & Dean
* This prints out names of football players.
****************************************************/
import java.util.Scanner;
import java.util.ArrayList;
public class FantasyFootball {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
ArrayList<String> players = new ArrayList<String>();
String indexStr;
int index = 0;
players.add("Peyton Manning");
players.add("Ladanian Tomlinson");
players.add("Reggie Bush");
System.out.print("Enter a number between 1 and 3: ");
indexStr = stdIn.nextLine();
try
{
index = Integer.parseInt(indexStr);
System.out.println("Entered index OK.");
}
catch (NumberFormatException e)
{
System.out.println("Entered index wasn't an integer");
}
try
{
System.out.println(players.get(index - 1));
}
catch (IndexOutOfBoundsException e)
{
System.out.println(
"Can't access players[" + (index - 1) + "]");
}
System.out.println("done");
} // end main
} // end class FantasyFootball
a. What is the output if the user enters 1 in response to the prompt?
b. What is the output if the user enters “one” in response to the prompt?
- 2 of 4 -
Question 3
[Page 595] Add a try-catch structure to the following program to make it compile and
execute correctly, even when the divisor is zero. Note that division by zero throws an
ArithmaticException.
import java.util.Scanner;
public class Division
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
int n, d, q;
System.out.print("Enter numerator: ");
n = stdIn.nextInt();
System.out.print("Enter divisor: ");
d = stdIn.nextInt();
q = n / d;
System.out.println(q);
} // end main
} // end Division class
To help you out, we’ve provided the catch block, below:
catch (ArithmeticException e)
{
System.out.println("Error, but keep going anyway.");
}
Question 4
Refer to the program on the next page.
a. The following are some possible contents in the text file data_file.in. Indicate which of
them can be used by the given program without causing any error. If an error occurs,
write down the name of the exception.
(i)
2 3
1.2 2.3 3.4
5.5 6.6 7.7
(ii)
2 3 1.2 2.3 3.4 5.5 6.6 7.7
(iv)
2 3
1.2
2.3 3.4
5.5 6.6
7.7
(v)
2 3
1.2 2.3 3.4
5.5 6.6 7.7
8.8 9.9
- 3 of 4 -
(vi)
2 3
1.2 2.3 3.4
5.5 6.6
(iii)
2
3
1.2
2.3
3.4
5.5
6.6
7.7
b. Instead of printing the matrix on the screen, change the printMatrix() method so that it
prints the matrix into a text file called matrix.out.
import java.util.*;
import java.io.*;
public class Week13Q4 {
public static void main(String[] args) throws IOException {
Scanner fileIn = new Scanner(new FileReader("data_file.in"));
int numRows = fileIn.nextInt(); // no. of rows of matrix
int numCols = fileIn.nextInt(); // no. of columns of matrix
double[][] matrix = new double[numRows][numCols];
for (int r = 0; r < numRows; r++) {
for (int c = 0; c < numCols; c++) {
matrix[r][c] = fileIn.nextDouble();
}
}
fileIn.close();
printMatrix(matrix);
} // main()
public static void printMatrix(double[][] arr) {
for (int r = 0; r < arr.length; r++) {
for (int c = 0; c < arr[0].length; c++) {
System.out.printf("%8.2f", arr[r][c]);
}
System.out.println();
}
} // printMatrix()
}
- 4 of 4 -