Last active
August 29, 2015 14:19
-
-
Save shurane/eb23c98d6fb48f16d0a9 to your computer and use it in GitHub Desktop.
Revised assessment test for AccessCode -- now converted into a solution.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nyc.c4q; | |
import java.io.FileReader; | |
import java.util.HashMap; | |
public class Unit0Exercises { | |
/* | |
* Name of the method implies what the method should do. | |
* Some of these methods require that the method return signature change. | |
* For example, `returnPrimitiveBooleanTrue()` should return `boolean`, not `Object`. | |
* additional information in the comments of each method | |
*/ | |
public static void main (String args[]) { | |
// Use main to test your methods | |
print(); | |
} | |
public static void print() { | |
// print out "I'm sorry Dave, I'm afraid I can't let you do that." | |
System.out.println("I'm sorry Dave, I'm afraid I can't let you do that."); | |
} | |
public static boolean returnPrimitiveBooleanFalse() { | |
return false; | |
} | |
public static int returnPrimitiveInt55Plus44() { | |
return 55+44; | |
} | |
public static double returnPrimitiveDouble2Point718() { | |
// This is the start of a famous mathematical constant. | |
// What's that constant? | |
return 2.718; | |
} | |
public static char returnPrimitiveCharASCII35() { | |
// What's the character for the ASCII number 35? | |
// Look at an ASCII table and return that character. | |
// You can look at the ASCII table at http://www.asciitable.com/ | |
return (char) 35; | |
} | |
public static boolean isEven(int n) { | |
return n % 2 == 0; | |
} | |
public static boolean isMultipleOfX(int n, int x) { | |
// 0 is a multiple of every number (except 0) | |
// For example: | |
// isMultipleOfX(0, 3) == true | |
// isMultipleOfX(1, 3) == false | |
// isMultipleOfX(3, 3) == true | |
// isMultipleOfX(10, 3) == false | |
// isMultipleOfX(28, 7) == true | |
// isMultipleOfX(-14, 7) == true | |
// Don't worry about when x==0 | |
return n % x == 0; | |
} | |
public static int returnSumOfOddNumbersBetween1And100() { | |
// Starts with [1, 3, 5, ...] | |
// Ends with [..., 95, 97, 99] | |
int total = 0; | |
int current = 1; | |
while (current < 100) { | |
total += current; | |
current += 2; | |
} | |
return total; | |
} | |
public static int returnSumOfTheFirst200MultiplesOf3() { | |
// Starts with [0, 3, 6, ...] | |
// Ends with [..., 591, 594, 597] | |
int total = 0; | |
int current = 0; | |
while (current < 200) { | |
total += current * 3; | |
current += 1; | |
} | |
return total; | |
} | |
public static boolean isEmptyString(String s) { | |
return s.equals(""); | |
} | |
public static String alternateS1AndS2Xtimes(String s1, String s2, int x) { | |
// Given string `s1`, `s2` and a positive integer `x`, | |
// return a string that is equal to s1 + s2 repeated x times. | |
// If x is 0 or negative negative, return an empty string `""`. | |
// For example: | |
// alternateS1AndS2Xtimes("abra", "cadabra", 0).equals("") == true | |
// alternateS1AndS2Xtimes("abra", "cadabra", 1).equals("abracadabra") == true | |
// alternateS1AndS2Xtimes("abra", "cadabra", 2).equals("abracadabraabracadabra") == true | |
// alternateS1AndS2Xtimes("abra", "cadabra", 3).equals("abracadabraabracadabraabracadabra") == true | |
String newString = ""; | |
for (int i=0; i<x; i++){ | |
newString += s1; | |
newString += s2; | |
} | |
return newString; | |
} | |
public static String stringSplit(String s, String splitOn) { | |
// Given string s, return a string that stops at the first occurrence of "split". | |
// For example: | |
// stringSplit("ubiquitous", "q").equals("ubi") == true | |
// stringSplit("hellogoodbye", "good").equals("hello") == true | |
// stringSplit("fancy seeing you", "there").equals("") == true | |
// If the string does not contain split, then return the empty string "". | |
String[] results = s.split(splitOn); | |
if (results.length == 1) // couldn't split, so keeps the original string | |
return ""; | |
else | |
return results[0]; | |
} | |
public static HashMap<String, Singer> returnSingers() { | |
// should return a HashMap of the following singers: | |
// key="Beyonce", Singer(name="Beyonce", location="USA") | |
// key="Jay-Z", Singer(name="Jay-Z", location="USA") | |
// key="Bieber", Singer(name="Bieber", location="Canada") | |
// key="Drake", Singer(name="Drake", location="Canada") | |
// key="Jepsen", Singer(name="Jepsen", location="Canada") | |
HashMap<String, Singer> people = new HashMap<>(); | |
people.put("Beyonce", new Singer("Beyonce", "USA")); | |
people.put("Jay-Z", new Singer("Jay-Z", "USA")); | |
people.put("Bieber", new Singer("Bieber", "Canada")); | |
people.put("Drake", new Singer("Drake", "Canada")); | |
people.put("Jepsen", new Singer("Jepsen", "Canada")); | |
return people; | |
} | |
public static boolean isFromCanada(Singer person) { | |
return person.getLocation().equals("Canada"); | |
} | |
public static void changeJayZsLocationToLosAngeles(HashMap<String, Singer> people) { | |
// people will have the values of the `returnSingers()` method you filled in previously. | |
// the city should be set to "Los Angeles" | |
// old value of Jay-Z: | |
// Singer(name="Jay-Z", location="USA") | |
// new value of Jay-Z: | |
// Singer(name="Jay-Z", location="Los Angeles") | |
people.get("Jay-Z").setLocation("Los Angeles"); | |
} | |
public static void removeJepsenFromSingers(HashMap<String, Singer> people){ | |
// people will have the values of the `returnSingers()` method you filled in previously. | |
// old keys in `people`: | |
// ["Beyonce", "Jay-Z", "Bieber", "Drake", "Jepsen"] | |
// new keys in `people`: | |
// ["Beyonce", "Jay-Z", "Bieber", "Drake"] | |
people.remove("Jepsen"); | |
} | |
// Bonus Problems | |
public static void readFileAndOnlyPrintCanadianCelebrities() throws Exception { | |
// TODO Use Apache Commons CSV and provide boilerplate for reading the file | |
// https://commons.apache.org/proper/commons-csv/ | |
FileReader fr = new FileReader("celebrities.csv"); | |
} | |
public static void readFileAndPrintCelebrityAges(){ | |
// TODO similar as above | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment