- (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...
- (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 exampleadd("5", "3") //returns 8 add("bob", "3") //returns 0 add("bob", "alice") //returns 0 add("1.456", "8") //returns 0
- (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"
- (10%) Given that you have
public enum SmartPhone {iphone3g, iphone4, iphone4s, android};
Implement a method that takesSmartPhone
as an argument and returns its price, as given by the following table
Item Price iphone 3g 300 iphone 4 349 iphone 4s 400 android 200 - (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"); } } } }
- (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()); } } }
- (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 (hasmoney >= 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.
- (10%) Assume that you are given a class called
Person
which has a method calledint distanceTo(Person other)
that returns the distance between where this person lives and whereother
lives.
Implement a methodArrayList<Person> hasFriendsCloserThan(HashMap<Person, HashSet<Person>> socialNet, int n)
that takes as input asocialNet
, 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 thann
away from them.
The other test:
- (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.
- (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
- (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
- (10%) Given that you have
public enum Tablet {ipad, ipad2, newipad, galaxy};
Implement a method that takes aSmartPhone
and returns its price, as given by the following table
Item Price ipad 300 ipad2 400 newipad 600 galaxy 300
- (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"); } } } }
- (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()); } }
- (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 tonull
. For example, if a Person has both mother and father asnull
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.
- (10%) Implement the method
String topPerson(HashMap<String, ArrayList<Double>> gradeMap)
which takes as input agradeMap
, 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.