Below is the final. If you want your graded final (the paper itself) send me an email and I'll tell you when you can drop by and pick it up. I will email everyone their final grades when I'm done grading.
- (10%) Implement a method that takes as an argument an
integer
n
and then prints out the sum from 0 to 1, from 0 to 2,..., from 0 to n. For example, if you are given 10 then you will print out:1 3 6 10 15 21 28 36 45 55
Answer:public static void gauss(int n) { int sum = 0; for (int i=1; i <= n; i++) { sum += i; System.out.println(sum); } }
- (10%) Given the
Player
class below:public class Player { public int strength; public int defense; public boolean magic; }
implement a methodpublic boolean winsAgainst(Player other)
which returns true if the player can beat or tieother
in a fight, false otherwise. The way you determine who wins in a fight is by following these rules:- If a player has
magic
and the other player does not then the player withmagic
wins if hisstrength
+defense
is greater than the other player'sdefense
. - If both have
magic
then the one with the highestdefense
wins. - If neither has
magic
then the one with the highestdefense
+magic
wins
Answer:public boolean winsAgainst(Player other) { if (magic && other.magic) { return defense >= other.defense; } if (!magic && !other.magic) { return (strength + defense) >= (other.defense + other.strength); } //exactly one of us has magic if (magic) { //he does not have magic return strength + defense >= other.defense; } //he has magic, I don't return defense >= other.strength + other.defense; }
- If a player has
- (10%) What does the following program print out?
public static void flurberg(int x) { if (x < 0) { System.out.println("Turing"); return; } try { int y = 10 / x; System.out.println("VonNeuman"); } catch (ArithmeticException e) { //division by 0 error System.out.println("Babbage"); } finally { System.out.println("Lovelace"); } } public static void main(String[] args) { flurberg(0); flurberg(-1); flurberg(1); }
Answer:Babbage Lovelace Turing VonNeuman Lovelace
- (15%) In this problem you will implement a small class
hierarchy to demonstrate that you understand OOP concepts. You
will
- Implement a class called
Present
that has a protected property calledname
, which is a String, andshippingWeight
, which is an integer. - Implement a constructor for the
Present
which takes 2 arguments:name
and shippingWeight, and sets the appropriate data members. - Implement a class called
BoardGame
which extendsPresent
and has a property callednumPlayers
which is an integer and is protected. - Implement a 2-argument constructor for
Boardgame
with argumentsname
andnumPlayers
. All board games have ashippingWeight = 16
. The constructor should call the parent constructor and set all property values appropriatedly. - Implement a class called
Clothing
which extendsPresent
and has the propertysize
which is a String.
Answer:public class Present { protected String name; /** * in ounces */ protected int shippingWeight; public Present(String name, int shippingWeight) { this.name = name; this.shippingWeight = shippingWeight; } } public class BoardGame extends Present { protected int numPlayers; public BoardGame(String name, int numPlayers) { super(name, 16); this.numPlayers = numPlayers; } } public class Clothing extends Present { protected String size; public Clothing(String name, int shippingWeight, String size) { super(name,shippingWeight); this.size = size; } }
- Implement a class called
- (15%) Implement a method that takes as input an array
of Strings and returns the number of "sheep" in the array that
appear before "zzzzz". For example:
- Given the array {"sheep", "fox", "sheep", "zzzzz"}, you return 2.
- Given the array {"fox", "fox", "zzzzz", "sheep"}, you return 0.
- Given the array {"sheep", "sheep", "sheep", "sheep"}, you return 4.
Answer:public static int countSheepUntilDone(String[] words) { int numSheep = 0; for (String w : words) { if (w.equals("sheep")) numSheep++; if (w.equals("zzzzz")) return numSheep; } return numSheep; }
- (10%) The program shown below has one, and only one,
compile-time error (which prevents this program from
compiling). Write down which line has the error and explain why
this is an error.
public class FinalTest { private String name; private static int grade; private static final int numQuestions = 101; public FinalTest(String name) { this.name = name; FinalTest.grade = 0; } public String toString() { return name + " " + FinalTest.numQuestions; } public static void setName(String name) { this.name = name; } public static void main(String[] args) { FinalTest NoNewbies = new FinalTest("neo"); System.out.println(NoNewbies); NoNewbies.name = "Trinity"; } }
Answer: Thepublic static void setName(String name) { this.name = name; }
is an error because a static method cannot access instance variables. I also accepted the answer that one could not set .name to "Trinity" because it is private. That answer is wrong because main is a method of FinalTest, but that is a more tricky question than I wanted to ask. I should have made name public. - (15%) The class
public class Folder { public Folder [] subFolders; public int numFiles; }
represents the folder hierachy in a computer filesystem. Where a folder can contain a number of files (given bynumFiles
) as well as a number of sub folders (stored in thesubFolders
) array. You will implement a recursive method for theFolder
which returns the total number of files in the folder and all its sub-folders, sub-sub-folders, sub-sub-sub-folders, etc. For example, if the folder has numFiles=5 and has 2 subFolders each with numFiles=2 and no subFolders, then you will return 9 (that is, 5+2+2).Answer:public int totalFiles() { if (subFolders == null || subFolders.length == 0) { return numFiles; } int numFilesSubfolders = 0; for (Folder f : subFolders) { numFilesSubfolders += f.totalFiles(); } return numFiles + numFilesSubfolders; }
- (15%) Implement the method
public static int countRepeats(int[] values)
which returns the number of numbers invalues
that appear more than once. Assume that all numbers invalues
are in the range 0 to 99 (inclusive). For example:- Given the input array
values = {4,4,4,4,4,4}
you will return 1, as the number 4 is repeated more than once. - Given
{1,2,3,4,95}
you will return 0 as no number is repeated. - Given
{1,3,3,1,5}
you will return 2 as the numbers 1 and 3 are repeated.
Answer:/** * Counts the number of items in 'values' that appear more than once in values. * Assume that 0 <= values[x] < 100 * @param values * @return the number of */ public static int countRepeats(int[] values) { int[] count = new int[100];//all set to 0 by default for (int val : values) { count[val]++; } int result = 0; for (int val : count) { if (val > 1) { result++; } } return result; }
- Given the input array
No comments:
Post a Comment