Name: ________________________________________________________
Umail Address: __________________________________@ umail.ucsb.edu
Circle Lab section: 4PM 10AM 11AM noon
Please write your name only on this page. That allows me to grade your exams without knowing whose exam I am grading.
This exam is closed book, closed notes, closed mouth, cell phone off,
except for:
There are 100 points worth of questions on the exam, and you have 50 minutes to complete the exam.
A hint for allocating your time:
int x = 5; // line 1
Integer y = 5; // line 2... indicates where other parts of the code listing have been left out for sake of space. ... is not part of the Java code.
interface ShoppingCartItem {
|
|
|
|
... infinite loopcode |
write the output here |
|---|---|
for (int i=0; i<4; i++) System.out.println(i); |
0 |
for (int i=4; i>1; i--) System.out.println(i); |
4 3 2 1 |
for (int i=1; i<3; i--) System.out.println(i); |
1 |
for (int i=1; i>3; i--) System.out.println(i); |
no output |
// (a) allocate the array
ages = new int [30];
ages// @@@ (b) Now add code that adds the age to the listWrite code that adds the age to the list. Also adjust any other instance variable as needed.
ages[count] = age;
count++; // @@@ (d) (optional) add code to keep track of max age
// @@@ (c) add code to throw an exception if there aren't any students // in the list yetAdd code according to the instructions in the comment.
if (count < 1) // or count==0 or count <= 0
throw new Exception("There aren't any students in the list");
// @@@ (d) add code that returns the age of the oldest student
int maxAge = ages[0]; for (int i=1; i<count; i++)
{
if (ages[i] > maxAge) maxAge = ages[i];
}
return maxAge;
getAgeOfOldest()(d), including the ones marked (d) (optional) ... // opening javadoc comments omitted for reasons of space public class StudentAges { private final int MAX = 30; private int [] ages; private int count; //@@@ (d) (optional) You may add instance variables if you need them
/** * Constructor for objects of class StudentAges */ public StudentAges() { // (a) allocate the array count = 0; } /** * Add a student's age to the list * @param age The age to add */ public void add(int age) throws Exception { if (age < 0) throw new IllegalArgumentException("Age can't be negative"); if (count >= MAX) throw new Exception("The list is full!");
// @@@ (b) Now add code that adds the age to the list // @@@ (d) (optional) perhaps also add code to keep track of max age } /** Age of oldest student in the course * @returns age of oldest student in the course * @throws Exception if there is no student in the list yet */ public int getAgeOfOldest() throws Exception { // @@@ (c) add code to throw an exception if there aren't any students in the list yet // @@@ (d) add code that returns the age of the oldest student // If needed, add code to the other places marked (d) (optional) also. // (There is more than one way to do this---depending on how you // tackle the problem, you might not need to add code anywhere else.) } // end method getAgeOfOldest } // end class
public class StudentAgesTest extends junit.framework.TestCase
{ public void testGetAgeOfOldest() { // @@@ fill in a missing line of code StudentAges a = new StudentAges();
try { a.add(18); a.add(19); a.add(20); a.add(17); // @@@ add the missing argument(s) to the method call on the next line of code
assertEquals( 20, a.getAgeOfOldest() ); } catch (Exception e) { fail("I didn't expect an Exception in this test case!"); } } }
java.lang.Exception must be caught or declared to be thrownWhat does this error message mean? Explain it the way you would at a job interview with Mary Pranks (see Question 1).
Total points: ?