CSC 216 F09/JavaBall
JavaBall
The problem
In this activity, we want to teach students the topic on exceptions so that they can utilize this important concept into their programs.
Participants and props
The whole class will participate in this activity. The class will be divided into teams. Number of teams depends on how large the class is. For this write-up, assume there are 50 students in the class, so there are 5 teams of 10 students. The instructor needs to prepare a ball, preferably a ball smaller than a basketball that can fit into a trashcan and that can also bounce. A trashcan or a basket is also required. Tape is required to designate lines onto the floor that will represent a point system.
Setup
The instructor must set up a point system for this activity. The trashcan/basket should be placed in the front of the classroom, and starting from about 7 feet away, place lines of tape onto the floor with the number of points written on the tape. The intervals should be about 1-1.5 ft apart. The first line can be 1 pt, and second line can be 2 pts, etc. The instructor may decide how many lines he wants, 3 lines are preferred.
There is one "ultimate shot" area, which could be looked as a "full-court shot". The instructor can decide where this area will be, preferably across the other side of the room. Since this is the toughest shot in the game, many points are awarded if someone makes this shot. For this write-up, we will assume this shot will be worth 10 points.
Rules
1. The instructor will give a question, and each team has about 1-2 minutes to answer the question.
2. Each team should whisper so that they don't give away the answer to other groups.
3. The answer should be written on a piece of paper, or can be submitted via google doc, if the instructor has prepared a spreadsheet.
4. After time is up, the instructor will check the answers (if the team has answered the question correctly, they are given 1 point; if the team has answered incorrectly, no points are allotted).
5. When a team has answered a question correctly, they are given the opportunity to score more points by shooting a basket in front of the class.
6. The shooter can decide which line they want to shoot from.
7. If the shooter makes the shot, he/she is allotted that number of points written on the piece of tape.
- BONUS POINT: If shooter can bounce the ball on the floor and make the basket, an extra point will be given.
8. No student can shoot again until all members of the team have shot at least once.
9. The team with the most accumulated points at the end wins.
Questions
Questions regarding exceptions can vary. The questions we came up with are of several types:
1. Code is provided, students decide which exception will be thrown.
private int r = 7;
private int zero = 0;
private int newNumber;
public static void main(String args[]){
newNumber = r/zero;
System.out.println(newNumber);
}
Which exception would be thrown in this code?
Answer: ArithmeticException
2. Exception will be provided, students determine what the exception does exactly and when its thrown.
ClassCast Exception -Definition: Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.
3. A description of an exception is provided, students determine the exception.
What Runtime Exception is thrown when an application attempts to use null in a case where an object is required? --NullPointer Exception
List of Questions
1.) What Runtime Exception is thrown when an application attempts to use null in a case where an object is required? --NullPointer Exception
2.) What Runtime Exception is thrown when a number is divided by 0? --Arithmetic Exception
3.) When would an IndexOutOfBounds Exception be thrown? --During Runtime
4.) When trying to execute a program, the application attempts to access a file (in.txt) though the Scanner object but you forgot to place the in.txt file in the correct directory. What Java IOException do you expect to be thrown? --FileNotFound Exception
5.) In your program you have a Iterator that gets the next element in a list. If you don't test the list for hasNext() and there is no next element, when the program attempts to run the code, what exception should be thrown? --NoSuchElement Exception
6.)private Card[] cards = new Cards[5];
public static void main(String args[]){ System.out.println(cards[5]); }
What exception would be thrown in this code? --IndexOutOfBounds
7.) private int r = 7;
private int zero = 0; private int newNumber;
public static void main(String args[]){ newNumber = r/zero; System.out.println(newNumber); }
What exception would be thrown in this code? --Arithmetic Exception
8.) private Card[] cards;
public static void main(String args[]){ cards[0].getRank();
}
What exception would be thrown in this code? --NullPointerException
9.) What types of Exceptions are thrown for Input and Output Handling? --IO Exceptions
10.) private StringTokenizer name;
private String in = "001010011011"; private int bit, bit2; public static void main(String args[]){ name = new StringTokenizer(in); bit = name.nextToken(); bit2 = name.nextToken();
}
What exception would be thrown in this code? --NoSuchElement Exception
ClassCast Exception -Definition: Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. -Code: Object x = new Integer(0);
System.out.println((String)x);
IllegalArgument Exception -Definition: Thrown to indicate that a method has been passed an illegal or inappropriate argument.
IllegalState Exception -Definition: Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.
EOFException -Definition: Signals that an end of file or end of stream has been reached unexpectedly during input.
MalformedURL Exception -Definition: Thrown to indicate that a malformed URL has occurred. Either no legal protocol could be found in a specification string or the string could not be parsed.
UnknownHost Exception -Definition: Thrown to indicate that the IP address of a host could not be determined.
NegativeArraySize Exception -Definition: Thrown if an application tries to create an array with negative size. -Code: int grades[] = new int[-100];
EmptyStack Exception -Definition: Thrown by methods in the Stack class to indicate that the stack is empty.
Acknowledgments
This activity was created by David Chung and Manpreet Singh.