Skip to content

Instantly share code, notes, and snippets.

@YoniTsafir
Created June 23, 2012 08:08
Show Gist options
  • Save YoniTsafir/2977502 to your computer and use it in GitHub Desktop.
Save YoniTsafir/2977502 to your computer and use it in GitHub Desktop.
Does it compile? questions in Java III
public class Main {
public static void main(String[] args) {
doSomething(7);
}
private static void doSomething(int num) throws Exception {
if (num > 10) {
throw new Exception("abcd");
} else {
System.out.println(num);
}
}
}
public class Main {
public static void main(String[] args) {
try {
doSomething(12);
System.out.println("Got here 1");
} catch (Exception e) {
System.out.println("Got here 2");
}
}
private static void doSomething(int num) throws Exception {
if (num > 10) {
throw new Exception("abcd");
} else {
System.out.println(num);
}
}
}
class A {
}
class B extends A {
}
public class Main {
public static void main(String[] args) {
A a1 = new A();
B b1 = new B();
A a2 = b1;
B b2 = a2;
System.out.println(a2.getClass().getName());
}
}
class A {
}
class B extends A {
private static int fooCount = 1;
public void foo() {
System.out.println("fooCount: " + fooCount++);
}
}
public class Main {
public static void main(String[] args) {
A a1 = new A();
B b1 = new B();
A a2 = b1;
A[] arr = {b1, a2, a1};
for (int i = 0; i < arr.length; i++) {
((B)arr[i]).foo();
}
}
}
import java.util.Arrays;
enum CompareType { ALWAYS_SMALLER, ALWAYS_BIGGER, BY_VALUE };
class StupidComparable implements Comparable<StupidComparable> {
private CompareType type;
private int value;
public StupidComparable(CompareType type, int value) {
this.type = type;
this.value = value;
}
public int compareTo(StupidComparable other) {
switch(type) {
case ALWAYS_SMALLER:
return -1;
case ALWAYS_BIGGER:
return 1;
default:
switch(other.type) {
case ALWAYS_SMALLER:
return 1;
case ALWAYS_BIGGER:
return -1;
default:
return value - other.value;
}
}
}
public String toString() {
return String.valueOf(value);
}
}
public class Main {
public static void main(String[] args) {
StupidComparable[] arr = new StupidComparable[4];
arr[0] = new StupidComparable(CompareType.ALWAYS_SMALLER, 100);
arr[1] = new StupidComparable(CompareType.ALWAYS_BIGGER, 1);
arr[2] = new StupidComparable(CompareType.BY_VALUE, 60);
arr[3] = new StupidComparable(CompareType.BY_VALUE, 40);
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
import java.util.Map;
import java.util.LinkedHashMap;
class MyCounter {
private Map<String, Integer> counter;
public MyCounter() {
counter = new LinkedHashMap<String, Integer>();
}
public void addString(String s) {
counter.put(s, countString(s) + 1);
}
public int countString(String s) {
// hint hint hint:
return counter.get(s);
}
}
public class Main {
public static void main(String[] args) {
MyCounter mc = new MyCounter();
for (int i = 0; i < 15; i++) {
mc.addString("hello");
}
for (int i = 0; i < 10; i++) {
mc.addString("goodbye");
}
System.out.println(mc.countString("hello"));
System.out.println(mc.countString("goodbye"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment