Skip to main content

Posts

Showing posts from January, 2012

Software skills

Iterable interface example (not my writing)

import java.util.Iterator; import java.util.NoSuchElementException; // This class supports iteration of the // characters that comprise a string. class IterableString implements Iterable , Iterator { private String str; private int count = 0; public IterableString(String s) { str = s; } // The next three methods implement Iterator. public boolean hasNext() { if (count < str.length()){ return true; } return false; } public Character next() { if (count == str.length()) throw new NoSuchElementException(); count++; return str.charAt(count - 1); } public void remove() { throw new UnsupportedOperationException(); } // This method implements Iterable. public Iterator iterator() { return this; } } public class MainClass { public static void main(String args[]) { IterableString x = new IterableString("This is a test."); for (char ch : x){ System.out.println(ch); } } }

Enum in Java

http://javarevisited.blogspot.com/2011/08/enum-in-java-example-tutorial.html http://javarevisited.blogspot.com/2011/12/convert-enum-string-java-example.html An enum example private enum Score { MINUS10(-10), MINUS5(-5), ZERO(0), PLUS5(5), PLUS10(10); private final int value; private Score(int value) { this.value = value; } public int getValue() { return value; } static public boolean isValid(int value) { Score[] scores = Score.values(); for (Score aScore : scores) { if (aScore.getValue() == value) { return true; } } return false; } }