






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Main points of this past exam paper are: Alper Method, Problems Deal, Taking Advantage, Expandableset, Inserted Larger, Argument Constructor, Nonnegative Ints, Specified Maxelement, Protected Boolean, New Boolean
Typology: Exams
1 / 11
This page cannot be seen from the preview
Don't miss anything!







You have 110 minutes to finish this test. Your exam should contain 6 problems (numbered 0-5). This is an open-book test. You may consult any books, notes, or other paper-based inanimate objects available to you. Read the problems carefully. If you find it hard to understand a problem, please ask a question. Please write your answers in the spaces provided in the test; if you need to use the back of a page make sure to clearly tell us so on the front of the page.
Good Luck!
Question 1 (2 points) Below is a class that creates a geometric shape. Draw a picture of what memory looks like at the spot indicated in the main method. Note: The Point class has instance variables x and y of type int.
import java.awt.Point;
public class GeometricShape { public Point[] vertices; int sides; public GeometricShape(int[] xValues, int [] yValues) { sides = xValues.length; vertices = new Point[6]; for (int i = 0; i < sides; i ++){ vertices[i] = new Point(xValues[i], yValues[i]); } } public static void main(String [] args){ int[] myXs = {0, 2, 2, 0}; int[] myYs = {0, 0, 5, 5}; GeometricShape shape = new GeometricShape (myXs, myYs); // DRAW A PICTURE OF WHAT MEMORY LOOKS LIKE HERE!!! } }
contains[k] = true; } public void remove(int k) { contains[k] = false; } public boolean member(int k) { return contains[k]; } }
/*
static void assertTrue(boolean b)
static void assertFalse(boolean b)
static void assertEquals(String message, Object expected, Object actual)
static void fail()
Please write your solution to Question 2 here:
CORRECT ANSWER (multiple correct solutions possible):
public class ExpandableSet extends Set{
public ExpandableSet() { super (1); } public void insert( int k){ if (k >= contains.length) { int origSize = contains.length; int newSize = origSize * 2; while (newSize <= k) { newSize = newSize * 2; } boolean [] biggerVersion = new boolean [newSize]; for ( int i = 0; i < origSize; i++) { biggerVersion[i] = contains[i]; } contains = biggerVersion; } super .insert(k); } } public void insert( int k){
if (k >= contains.length) { boolean [] biggerVersion = new boolean [contains.length * 2]; for ( int i = 0; i < contains.length; i++) { biggerVersion[i] = contains[i]; } contains = biggerVersion; insert(k); } super .insert(k); }
RUBRIC:
CODE Deduction Description A -1 Not including the line^ public^ class^ ExpandableSet^ extends^ Set{
B -
Not including a constructor that calls super(1); We take off this point for explicitly saying this.contains = new boolean[1]; b/c the compiler forces you to call super in this case b/c there is not a no argument constructor provided. C -1 Not including the insert method header (written exactly as: insert( int k))^ public^ void
D -0.5 Not calling super.insert(k) when k < contains.length (assuming that they dosomething like contains[k] = true; E -1 Forgetting to actually insert the value into the array F -1 Off by one error in checking k and contains.length. (If k == contains.length theyneed to expand).
G -2 Writing to contains.length (which is not modifiable). For example: contains.length = contains.length * 2;
They don’t copy values from the old array into the new array. For example if they don’t include something like this: for ( int i = 0; i < contains.length; i++) { biggerVersion[i] = contains[i]; } OR if they write over contains before they have copied the values. For example. contains = new boolean[contains.length * 2]; I -2 Tries to write to this. J -2 Don’t point contains to the newly constructed array K -3 Doesn’t correctly increase the size by^ a factor of^ 2.
They don’t handle the case where the array needs to expand to more than 2 times the size. In the first example solution this is done with the following loop: while (newSize <= k) { newSize = newSize * 2; } In the second example this is done by recursively calling insert. M -3 Tries to implicitly write over this by calling super within the insert method, this sometimes overlaps with H. Don’t double count., calls this() on insert N -1 Provided additional unnecessary methods P -3 Treating an expandable set as an array – type mismatch issues etc.
try { es.member(expectedSize – 1) } catch (ArrayOutOfBoundsException e) { return false; } try { es.member(expectedSize ) return false; } catch (ArrayOutOfBoundsException e) { return true; } 1 point for checking that it is big enough, 1 point for checking that it isn’t too big. Errors: A – you only check if it is big enough B – you only check if it is too big.
public class ExpandableSetTest extends TestCase { (^) Description of what this tests public void test1() } public void test 2 () {
} public void test 3 () {
}
Solution
// Test that the size doesn’t increase unnecessarily ExpandableSet e = new ExpandableSet(); e.insert(0); assertTrue (sizeCheckHelper(e, 1));
// ONE // Test that the size doubles when necessary ExpandableSet e = new ExpandableSet(); e.insert(1); assertTrue (sizeCheckHelper(e, 2));
// MANY // Test that the size is 8 times as large when necessary. ExpandableSet e = new ExpandableSet(); e.insert(4); assertTrue (sizeCheckHelper(e, 8));
CODE Deduction Description A +1 Provides^ no expansion case B +1 Provides single expansion case C +1 Provides multiple expansion case D +1 Provides a check that the value actually gets set by insert E -1 Doesn’t create the objects needed, for exampleExpandableSet();^ –^ contains no calls to new F -1 If they used print statements instead of using JUnit asserts. G -0.5 SizeCheck helper with incorrect arguments H -0.5 Contains[k] check (where k is an incorrect position) I -1 Not using sizecheckHelper and using something that doesn’t work J -1 Treating expandable set as an array
We didn’t take off points if people created a constructor that took in a size and used it in their tests.
Question 5 (9 points)
This question is based upon Project 1. Relevant details of the Picture class and Pixel class are provided on the next few pages. The goal is to iterate over all Pixels in a Picture that are within a Circle. The order in which Pixels are returned doesn’t matter, but all Pixels within the circle should be returned. For example, we want to be able to run the following method in the Picture class:
// Colors the picture with a black circle of radius 10, // centered at (50, 50)
// the comment specifies. // // This method should return the next unexplored Pixel based upon the // argument lastPixel. Unexplored Pixels should be returned regardless of // whether they are within the circle. This method does not modify // nextPixel. The method returns the next Pixel to be explored or null // if no unexplored Pixels remain. public Pixel getUnexploredPixel(Pixel lastPixel) { }
CODE Deduction Description A - 4 Missing pixels in the traversal (major) B - 2 Bad math indexing picture, for example by accessing a pixel that may be outside of the circle (getPixel(x-radius, y-radius) etc. C - 1 Off by 1 in accessing all of the pixels D - 2 Really pretty close to all of the pixels but more than an off by 1 error
CODE Deduction Description E - 4 Invariant not maintained F - 2 Restore invariant does not restore the invariant G - 3 Wrong invariant
CODE Deduction Description I - 1 Calls new Pixel(…) instead of getPixel(…) J - 1 Next() doesn’t return a pixel/or calls restore invariant after the return. K - 2 Not constructing a pixel L - 1 Makes a new picture object
CODE Deduction Description Y - 1 Doesn’t save instance variables midX, midY and radius
Z - 1
Doesn’t make sure that the invariant is true at the end of the call to initIterator, examples of this include not initializing the pixel, another is calling restore invariant that may cause the initial pixel ( 0 , 0 ) to never be returned by next. // The circle is defined by a radius circleRadius and is centered at // coordinates (centerX, centerY). // This method will be called before the first call to hasNext() or next() SOLUTION public void initIterator(int centerX, int centerY, int circleRadius) {
CODE Deduction Description A Doesn’t save instance variables midX, midY and radius B Doesn’t make sure that the invariant is true at the end of the call to initIterator. }
// return successive Pixels within the circle specified in initIterator