Monday, April 30, 2012

Final Tests

These are the final tests. If you want your graded final just send me an email and I will tell you when I'll be in my office so you can pick it up.

1. (10%) Implement a method that prints out all the numbers between 1 and 10,000,000 except that if the number is a multiple of 3 you will print "GO", if it is a multiple of 7 you print "Cocky" and if its a multiple of both 3 and 7 you print "Go Gamecocks". That is, the first part of the output looks like
```1
2
GO
4
5
GO
Cocky
8
GO
10
11
GO
13
Cocky
GO
16
17
GO
19
20
Go Gamecocks
22
...and so on...```
2. (10%) Implement the method `int add(String a, String b)` (notice, a and b are String) which adds the two arguments and returns the sum, but only if the two are integers. If they are not then it returns 0. For example
```add("5", "3") //returns 8
```
3. (10%) Implement a method `String myCode(boolean[] msg)` which translates the boolean array into a string using the following
code:
```true → E
false true → T
false false true → A
false false false → b```
For example,
```boolean[] m1 = {true};
System.out.println(myCode(m1)); //prints "E"

boolean[] m2 = {false, true, false, false, false};
System.out.println(myCode(m2)); // "Tb"

boolean[] m3 = {true, false, false, true, false, false, false};
System.out.println(myCode(m3)); // "EAb"

boolean[] m4 = {false, false, false, false, false, false, false, true};
System.out.println(myCode(m4)); // "bbT"
```
4. (10%) Given that you have
```public enum SmartPhone {iphone3g, iphone4, iphone4s, android};
```
Implement a method that takes `SmartPhone` as an argument and returns its price, as given by the following table

ItemPrice
iphone 3g300
iphone 4349
iphone 4s400
android200
5. (10%) What does the following program printout?
```public class PrintoutExceptions {

public static String xkcd(int x) throws Exception{
if (x < 0){
return "Magnets";
}
else if (x < 5){
return "Sticks";
}
else if (x % 2 == 1){
return "Number:" + Integer.toString(x);
}
else throw new Exception("Caramba");
}

public static void main(String[] args) {

int[] a = {-5,0,1,3,7,8};
for (int i = 0; i < a.length; i++) {
try {
System.out.println(i + "-" +  xkcd( a[i] ));
} catch (Exception e) {
System.out.println("Ooops");
}
}
}
}```
6. (10%) Does the program below run or crash? If it runs, show what it prints out when it runs. If it crashes, explain why.
```public class A {

public String toString() {
return "I am A";
}
}

public class B extends A {

public String toString() {
return "I am B";
}
}

public class C extends A {

public String toString(){
return "I am C";
}
}

public class Q6 {

public static void main(String[] args) {
A[] as = new A[3];
as[0] = new A();
as[1] = new B();
as[2] = new C();
for (A a : as) {
System.out.println(a.toString());
}
}
} ```
7. (10%) Given the following code:
```public class Person {

private int money;

private Person[] friends;

public int minDistanceToMillionaire(){
//TODO: implement this method
}
}```
implement the missing method using recursion, which will return the distance to the closest millionaire. For example, if the person is a millionaire (has `money >= 1,000,000`) then it will return 0. Otherwise, if the person has a friend who is a millionaire then it will return 1. Otherwise, if the person has a friend who has a friend that is a millionaire then it will return 2, and so on.
You can assume that the graph does not have loops in it. That is, you will never see the same Person twice.

8. (10%) Assume that you are given a class called `Person` which has a method called `int distanceTo(Person other)` that returns the distance between where this person lives and where `other` lives.
Implement a method
`ArrayList<Person> hasFriendsCloserThan(HashMap<Person, HashSet<Person>> socialNet, int n) `
that takes as input a `socialNet`, which is a mapping between a Person and his friends, and returns the list of all the people who have at least one friend that lives a distance of less than `n` away from them.

The other test:

1. (10%)Implement a method that returns the sum of all the odd numbers between 1 and 1,000,000 except that those that are multiples of both 3 and 5 count double, while those that are multiples of either just 3 or just 3 don't get counted. For example, if the sum was just between 1 and 20 then you would return 98 because that is the sum of 1 + 7 + 11 + 13 + (15*2) + 17 + 19.
2. (10%) Implement a method `int ssnToInt(String ssn)` which, when given a a String represenation of a social security number that looks like "123-45-6789" returns the integer that represents that number, namely 123456789. If given a String that does not match then it returns -1. Specifically,
```ssnToInt("123-45-6789") //returns 123456789
ssnToInt("123456789") //returns -1
ssnToInt("12304506789") //returns -1
ssnToInt("12389") //returns -1
ssnToInt("7aa-90-879B") //returns -1
```
3. (10%) Implement a method `String compress(String dna)` which takes as input a String that contains only the characters A,C,G, and T, and then compresses the string by counting how many times each one appears consecutively. If a character appears more than once in a row the whole sequence is replaced by the character followed by the number of times it appeared in a row. For example:
```String d1 = "ACGT";
System.out.println(compress(d1)); //prints ACGT

String d2 = "AAAAACGT";
System.out.println(compress(d2)); //prints A5CGT

String d3 = "CCCTTTTAGGGGG";
System.out.println(compress(d3)); //prints C3T4AG5

String d4 = "AAAAAA";
System.out.println(compress(d4)); //prints A6
```
4. (10%) Given that you have
`public enum Tablet {ipad, ipad2, newipad, galaxy}; `
Implement a method that takes a `SmartPhone` and returns its price, as given by the following table

ItemPrice
galaxy300

5. (10%) What does the following program printout?
```public class PrintoutExceptions {

public static String onion(int x) throws Exception{
if (x % 2 == 1){
return "Carrot";
}
else if (x <= 0){
return "Sticks";
}
else if (x < 5){
return "Number:" + Integer.toString(x);
}
else throw new Exception("Caramba");
}

public static void main(String[] args) {

int[] a = {-5,0,1,3,7,8};
for (int i = 0; i < a.length; i++) {
try {
System.out.println(i + "-" +  onion( a[i] ));
} catch (Exception e) {
System.out.println("Ooops");
}
}
}
}```
6. (10%) Does the program below run or crash? If it runs, show what it prints out when it runs. If it crashes, explain why.
```public class A {

public String toString() {
return "I am A";
}
}

public class B extends A {

public String toString() {
return "I am B";
}
}

public class C extends A {

public String toString(){
return "I am C";
}
}

public class Q6 {
static void foo(A a){
System.out.println(a.toString());
}

public static void main(String[] args) {
foo(new A());
foo(new B());
foo(new C());
}
} ```
7. (10%) Given the following code:
```public class Person {

private Person mother;

private Person father;

public int generationsToOldestKnownAncestor(){
//TODO: implement this method
}

}```
implement the missing method using recursion, which will return the distance to the oldest known ancestor. If we don't know a Person's mother or father we set those properties to `null`. For example, if a Person has both mother and father as `null` then the method should return 0. But, if the person has only a mother, who in turn has both mother and father set to null then it should return 1, and so on.
You can assume that the graph does not have loops in it. That is, you will never see the same Person twice.

8. (10%) Implement the method
`String topPerson(HashMap<String, ArrayList<Double>> gradeMap)`
which takes as input a `gradeMap`, which is a mapping from a student's name to his list of grades, and which returns the name of the student with the highest grade. For example, if Alice has grades of 10, 20, 88, and Bob has grades of 77, 55, then it would return Alice because of her 88.