




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Final Exam Practice Quiz โ Will be discussed Monday, May 11 in lab section โ answers will be posted at that time
Implement method public static Cat[][] upsideDown(Cat[][] source){ int numRows = source.length; Cat[][] temp = new Cat[numRows][]; int currRow = numRows-1; for (int row = 0; row < numRows; row++, currRow--){ int numCols = source[row].length; temp[currRow] = new Cat[numCols]; int currCol = numCols-1; for (int col = 0; col < numCols; col++, currCol--){ temp[currRow][currCol] = newCat(source[row][col]); } } return temp; }
Determine the output a) f("Hello");
b) f("");
c) f(null);
switch (value){ case 3: str = "x"; break; case 0 : case 1: case 2: case 4: str = "A"; break; default: str = "B";
break; }
Give results: a. Mammal p = (Mammal) c; Works Fine b. Dog q = (Dog) a; Compiles, but Throws an Exception c. Mammal r = c; Works Fine d. Dog s = a; Will NOT compile e. Dog t = (Dog) x; Works Fine f. Dog u = x; Will NOT compile g. For this part, assume that the Dog class has an instance method called โbarkโ, which is not implemented in the Mammal class. x.bark(); Will NOT compile h. Suppose you add an instance method to the Mammal class called grow() and that you override that method in the Dog class. Consider the code below. Which grow method will be called? x.grow(); CIRCLE ONE: Mammal / (Dog)
Indicate what is on the stack and what is on the heap after each of the following statements (draw a picture): Integer x = new Integer(5); // x is on the stack pointing at an integer object that contains a 5 int a = 7; // a is put on the stack with the value 7 on the stack Integer[] b = {new Integer(1), new Integer(5)}; // b is put on the stack with a reference to a 2 element array on the heap // the first element of that array is a reference to another space on the heap //that is an Integer that contains a 1 // the second element of that array is a reference to another space on the heap // that is an Integer that contains a 5 Integer[] c = b; // c is put on the stack โ it is a reference pointing to the same space on the heap // as where b is pointing to Integer[] d = new Integer[b.length]; // d is put on the stack pointing to a 2 element array (distinct from b's) // the elements of the array are null for (int i = 0; i < b.length; i++) { d[i] = b[i]; } // the elements of the array pointed to by d now point to the elements of the // array pointed to by b // they are distinct arrays but share the values int[] e = {8, 9}; // e comes on to the stack pointing to a two element array on the heap // the 8 and 9 are directly in that array (not pointed to by it)
Assume you have the class MyClass as defined below. public class MyClass{ private Obj1[][] list1; private ArrayList
d) Write a method which returns the largest int (based on the individual getValues) in the ArrayList (list2) (call it largestInList). Return a -1 if there is nothing in the ArrayList. public int largestInList(){ if (list2.size() == 0){ return -1; } int largestSoFar = -1; for (Obj2 temp: list2){ if (temp.getValue() > largestSoFar){ largestSoFar = temp.getValue(); } } return largestSoFar; } e) Write a method that uses the two previous methods to return a single integer which is the largest of the whole class. Returns a -1 if and only if they are both empty. public int largestWhole(){ return (largestInArray() > largestInList()? largestInArray():largestInList()); } f) Write a sum method that returns the sum of all getValues (in both data members) public int sumAll(){ int sum = 0; for (int row = 0; row < list1.length; row++){ for (int col = 0; col < list1[row].length; col++){ sum += list1[row][col].getValue(); } } for (Obj2 temp: list2){ sum+= temp.getValue(); } return sum; } g) Write a toString method that returns the first value (the [0][0] element) of the array followed by the first item in the ArrayList. If either is empty (there are no objects inside) return the string "empty". public String toString(){ if (list2.size() == 0){ return "empty"; } if (list1.length == 0){ return "empty"; } boolean allEmpty = true; for (Obj1[] row:list1){ if (row.length != 0){ allEmpty = false; } } if (allEmpty){ return "empty"; } if (list1[0].length == 0){
return "empty"; } return (list1[0][0] + " & " + list2.get(0)); } h) Write a method that returns a Stack of all objects on the array (order does not matter) but all items present in the array must be on a Stack that is returned as the return value of the method. public Stack
i) Write a method that uses a for loop to increaseValue of all of the objects in list1. Rewrite using for-each.
public void increaseArrFirstWay(){ for (int row = 0; row < other.list1.length; row++){ for (int col = 0; col < other.list1[row].length; col++){ lsit1[row][col].increaseValue(5); } } } public void increaseArrSecondWay(){ for (Obj1[] row : list1){ for (Obj1 temp: row){ temp.increaseValue(5); } } } j) Write a method that uses a for loop to increaseValue of all of the objects in list2. Rewrite with for/each. pubic void increaseListFirstWay(){ for (int i = 0; i < list2.size(); i++){ list2.get(i).increaseValue(5); } } public void increaseListSecondWay(){ for (Obj2 temp: list2){ temp.increaseValue(5); } }
public static void specialPrint() { for (int i = 1; i <= 100; i++){ if (i %2 == 1 && i > 50){ System.out.println("oddbig"); } else if (i %2 == 1){ System.out.println("odd"); } else if (i > 50){ System.out.println("big"); } else{ System.out.println(i); }
} }
public class Fruit { private String fruitType; public Fruit(String fruitType) { this.fruitType = fruitType; } public Fruit(Fruit x) { fruitType = x.fruitType; } public String toString(){ return (โโ+fruitType+โโ); } } public interface hasSeeds { public int getNumSeeds(); } class Apple extends Fruit implements hasSeeds { private int numSeeds; public Apple(int s) { super(โAppleโ); numSeeds = s; } public Apple() { this(13); } public Apple(Apple a) { super(a); numSeeds = a.numSeeds;
public boolean equals(Object x) { if (x == null) { return false; } if (x.getClass() != getClass()) return false; Apple a = (Apple)x; return numSeeds == a.numSeeds; } public String toString() { return super.toString() + โ with โ + numseeds + โ seedsโ; } public int getNumSeeds() { return numSeeds; } }