## Saturday, December 11, 2010

I have just sent out the email with your final grades. As I mentioned in class, I gave a break to those who did very well on the final: I bumped them up a grade if they were close to the borderline. The final grade curve is as I described in class. Below is the distribution for the grades on the final test and the class as a whole.

Have a good xmas break! and, keep on programming!

## Tuesday, December 7, 2010

### Final Test

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.

1. (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
```
```public static void gauss(int n) {
int sum = 0;
for (int i=1; i <= n; i++) {
sum += i;
System.out.println(sum);
}
}
```
2. (10%) Given the `Player` class below:
```public class Player {
public int strength;
public int defense;
public boolean magic;
}
```
implement a method ```public boolean winsAgainst(Player other)``` which returns true if the player can beat or tie `other` in a fight, false otherwise. The way you determine who wins in a fight is by following these rules:
1. If a player has `magic` and the other player does not then the player with `magic` wins if his `strength` + `defense` is greater than the other player's `defense`.
2. If both have `magic` then the one with the highest `defense` wins.
3. If neither has `magic` then the one with the highest `defense` + `magic` wins
```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;
}
```
3. (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);
}
```
```Babbage
Lovelace
Turing
VonNeuman
Lovelace
```
4. (15%) In this problem you will implement a small class hierarchy to demonstrate that you understand OOP concepts. You will
1. Implement a class called `Present` that has a protected property called `name`, which is a String, and `shippingWeight`, which is an integer.
2. Implement a constructor for the `Present` which takes 2 arguments: `name` and shippingWeight, and sets the appropriate data members.
3. Implement a class called `BoardGame` which extends `Present` and has a property called `numPlayers` which is an integer and is protected.
4. Implement a 2-argument constructor for `Boardgame` with arguments `name` and `numPlayers`. All board games have a `shippingWeight = 16`. The constructor should call the parent constructor and set all property values appropriatedly.
5. Implement a class called `Clothing` which extends `Present` and has the property `size` which is a String.
```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;
}

}
```
5. (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.
```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;
}
```
6. (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 final int numQuestions = 101;

public FinalTest(String name) {
this.name = name;
}

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";
}
}
```
```   public 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.
7. (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 by `numFiles`) as well as a number of sub folders (stored in the `subFolders`) array. You will implement a recursive method for the `Folder` 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).
```public int totalFiles() {
if (subFolders == null || subFolders.length == 0) {
return numFiles;
}
int numFilesSubfolders = 0;
for (Folder f : subFolders) {
numFilesSubfolders += f.totalFiles();
}
return numFiles + numFilesSubfolders;
}
```
8. (15%) Implement the method `public static int countRepeats(int[] values)` which returns the number of numbers in `values` that appear more than once. Assume that all numbers in `values` 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.
```/**
* 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;
}
```

## Thursday, December 2, 2010

### Lab 23 -- Problem 3 -- Team Selection

```
Hi CSCE-145 Section 5 ... This is your Last Lab ... How about trying to do Problem 3
( TeamSelection Problem ) from the TopCoder's List of Programs.

```