Pure Programmer
Blue Matrix


Cluster Map

Selection

L1

This page is under construction. Please come back later.

Selection1.java
public class Selection1 {

	public static void main(String[] args) {
		System.out.println("Start...");
		if (args.length != 2) {
			System.out.println("You need at least two command line arguments!");
		}
		System.out.println("Finish...");
	}
}

Output
$ javac -Xlint Selection1.java $ java -ea Selection1 abc Start... You need at least two command line arguments! Finish... $ javac -Xlint Selection1.java $ java -ea Selection1 abc 123 Start... Finish...
Selection2.java
import org.pureprogrammer.Utils;

public class Selection2 {

	public static void main(String[] args) {
		final int x = Utils.stoiWithDefault(args[0], 0);
		System.out.println("Start...");
		if (x > 10) {
			System.out.println("Greater than 10");
		} else {
			System.out.println("Less than or equal to 10");
		}
		System.out.println("Finish...");
	}
}

Output
$ javac -Xlint Selection2.java $ java -ea Selection2 8 Start... Less than or equal to 10 Finish... $ javac -Xlint Selection2.java $ java -ea Selection2 12 Start... Greater than 10 Finish...
Selection3.java
import org.pureprogrammer.Utils;

public class Selection3 {

	public static void main(String[] args) {
		final int x = Utils.stoiWithDefault(args[0], 0);
		System.out.println("Start...");
		if (x > 1000) {
			System.out.println("Greater than 1000");
		} else if (x > 100) {
			System.out.println("Greater than 100 but less than or equal to 1000");
		} else {
			System.out.println("Less than or equal to 100");
		}
		System.out.println("Finish...");
	}
}

Output
$ javac -Xlint Selection3.java $ java -ea Selection3 12 Start... Less than or equal to 100 Finish... $ javac -Xlint Selection3.java $ java -ea Selection3 120 Start... Greater than 100 but less than or equal to 1000 Finish... $ javac -Xlint Selection3.java $ java -ea Selection3 1200 Start... Greater than 1000 Finish...
Selection4.java
import org.pureprogrammer.Utils;

public class Selection4 {

	public static void main(String[] args) {
		final int x = Utils.stoiWithDefault(args[0], 0);
		switch (x) {
			case 1:
				System.out.println("One");
				break;
			case 2:
				System.out.println("Two");
				break;
			case 3:
			case 4:
				System.out.println("Three or Four");
				break;
			default:
				System.out.println("Just don't know!");
				break;
		}
	}
}

Output
$ javac -Xlint Selection4.java $ java -ea Selection4 0 Just don't know! $ javac -Xlint Selection4.java $ java -ea Selection4 1 One $ javac -Xlint Selection4.java $ java -ea Selection4 2 Two $ javac -Xlint Selection4.java $ java -ea Selection4 3 Three or Four $ javac -Xlint Selection4.java $ java -ea Selection4 4 Three or Four
Selection5.java
import org.pureprogrammer.Utils;

public class Selection5 {

	public static void main(String[] args) {
		String x = Utils.uniAt(args[0], 0);
		x = x.toLowerCase();
		switch (x) {
			case "a":
			case "e":
			case "i":
			case "o":
			case "u":
				System.out.println(Utils.format("{0:c} is a vowel", x.codePointAt(0)));
				break;
			case "y":
				System.out.println("y is sometimes a vowel");
				break;
			default:
				System.out.println(Utils.format("{0:c} is a consonant", x.codePointAt(0)));
				break;
		}
	}
}

Output
$ javac -Xlint Selection5.java $ java -ea Selection5 a a is a vowel $ javac -Xlint Selection5.java $ java -ea Selection5 g g is a consonant $ javac -Xlint Selection5.java $ java -ea Selection5 I i is a vowel $ javac -Xlint Selection5.java $ java -ea Selection5 T t is a consonant $ javac -Xlint Selection5.java $ java -ea Selection5 y y is sometimes a vowel
java

Questions

Projects

More ★'s indicate higher difficulty level.

References