Pure Programmer
Blue Matrix


Cluster Map

Functions

L1

This page is under construction. Please come back later.

Functions1.java
public class Functions1 {

	static void printHello() {
		System.out.println("Hello, world!");
	}

	public static void main(String[] args) {
		printHello();
	}
}

Output
$ javac -Xlint Functions1.java $ java -ea Functions1 Hello, world!
Functions2.java
public class Functions2 {

	static void printHello(String name) {
		System.out.println("Hello, " + name + "!");
	}

	public static void main(String[] args) {
		printHello("Fred");
		printHello("Wilma");
		printHello("Barney");
		printHello("Betty");
	}
}

Output
$ javac -Xlint Functions2.java $ java -ea Functions2 Hello, Fred! Hello, Wilma! Hello, Barney! Hello, Betty!
Functions3.java
import org.pureprogrammer.Utils;

public class Functions3 {

	static int squared(int j) {
		return j * j;
	}

	public static void main(String[] args) {
		for (int i = 1; i <= 10; ++i) {
			System.out.println(Utils.format("{0:d}^2 = {1:d}", i, squared(i)));
		}
	}
}

Output
$ javac -Xlint Functions3.java $ java -ea Functions3 1^2 = 1 2^2 = 4 3^2 = 9 4^2 = 16 5^2 = 25 6^2 = 36 7^2 = 49 8^2 = 64 9^2 = 81 10^2 = 100
Functions4.java
import org.pureprogrammer.Utils;

public class Functions4 {

	static double exponentiation(double base, int powerArg) {
		int power = powerArg;
		double result = 1;
		while (power > 0) {
			result *= base;
			--power;
		}
		return result;
	}

	public static void main(String[] args) {
		for (double b = 1.1; b < 2.05; b += 0.1) {
			for (int p = 2; p <= 10; ++p) {
				System.out.println(Utils.format("{0:f} ^ {1:d} = {2:f}", b, p, exponentiation(b, p)));
			}
		}
	}
}

Output
$ javac -Xlint Functions4.java $ java -ea Functions4 1.100000 ^ 2 = 1.210000 1.100000 ^ 3 = 1.331000 1.100000 ^ 4 = 1.464100 1.100000 ^ 5 = 1.610510 1.100000 ^ 6 = 1.771561 1.100000 ^ 7 = 1.948717 1.100000 ^ 8 = 2.143589 1.100000 ^ 9 = 2.357948 1.100000 ^ 10 = 2.593742 1.200000 ^ 2 = 1.440000 1.200000 ^ 3 = 1.728000 1.200000 ^ 4 = 2.073600 1.200000 ^ 5 = 2.488320 1.200000 ^ 6 = 2.985984 1.200000 ^ 7 = 3.583181 1.200000 ^ 8 = 4.299817 1.200000 ^ 9 = 5.159780 1.200000 ^ 10 = 6.191736 1.300000 ^ 2 = 1.690000 1.300000 ^ 3 = 2.197000 1.300000 ^ 4 = 2.856100 1.300000 ^ 5 = 3.712930 1.300000 ^ 6 = 4.826809 1.300000 ^ 7 = 6.274852 1.300000 ^ 8 = 8.157307 1.300000 ^ 9 = 10.604499 1.300000 ^ 10 = 13.785849 1.400000 ^ 2 = 1.960000 1.400000 ^ 3 = 2.744000 1.400000 ^ 4 = 3.841600 1.400000 ^ 5 = 5.378240 1.400000 ^ 6 = 7.529536 1.400000 ^ 7 = 10.541350 1.400000 ^ 8 = 14.757891 1.400000 ^ 9 = 20.661047 1.400000 ^ 10 = 28.925465 1.500000 ^ 2 = 2.250000 1.500000 ^ 3 = 3.375000 1.500000 ^ 4 = 5.062500 1.500000 ^ 5 = 7.593750 1.500000 ^ 6 = 11.390625 1.500000 ^ 7 = 17.085938 1.500000 ^ 8 = 25.628906 1.500000 ^ 9 = 38.443359 1.500000 ^ 10 = 57.665039 1.600000 ^ 2 = 2.560000 1.600000 ^ 3 = 4.096000 1.600000 ^ 4 = 6.553600 1.600000 ^ 5 = 10.485760 1.600000 ^ 6 = 16.777216 1.600000 ^ 7 = 26.843546 1.600000 ^ 8 = 42.949673 1.600000 ^ 9 = 68.719477 1.600000 ^ 10 = 109.951163 1.700000 ^ 2 = 2.890000 1.700000 ^ 3 = 4.913000 1.700000 ^ 4 = 8.352100 1.700000 ^ 5 = 14.198570 1.700000 ^ 6 = 24.137569 1.700000 ^ 7 = 41.033867 1.700000 ^ 8 = 69.757574 1.700000 ^ 9 = 118.587876 1.700000 ^ 10 = 201.599390 1.800000 ^ 2 = 3.240000 1.800000 ^ 3 = 5.832000 1.800000 ^ 4 = 10.497600 1.800000 ^ 5 = 18.895680 1.800000 ^ 6 = 34.012224 1.800000 ^ 7 = 61.222003 1.800000 ^ 8 = 110.199606 1.800000 ^ 9 = 198.359290 1.800000 ^ 10 = 357.046723 1.900000 ^ 2 = 3.610000 1.900000 ^ 3 = 6.859000 1.900000 ^ 4 = 13.032100 1.900000 ^ 5 = 24.760990 1.900000 ^ 6 = 47.045881 1.900000 ^ 7 = 89.387174 1.900000 ^ 8 = 169.835630 1.900000 ^ 9 = 322.687698 1.900000 ^ 10 = 613.106626 2.000000 ^ 2 = 4.000000 2.000000 ^ 3 = 8.000000 2.000000 ^ 4 = 16.000000 2.000000 ^ 5 = 32.000000 2.000000 ^ 6 = 64.000000 2.000000 ^ 7 = 128.000000 2.000000 ^ 8 = 256.000000 2.000000 ^ 9 = 512.000000 2.000000 ^ 10 = 1024.000000
Functions5.java
import org.pureprogrammer.Utils;

public class Functions5 {

	static long factorial(int x) {
		if (x <= 1) {
			return 1;
		}
		return x * factorial(x - 1);
	}

	public static void main(String[] args) {
		for (int x = 1; x < 10; ++x) {
			System.out.println(Utils.format("{0:d}! = {1:d}", x, factorial(x)));
		}
	}
}

Output
File not found!: /kunden/homepages/39/d957328751/htdocs/pureprogrammer/java/examples/output/Functions5.out
Lists6.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.pureprogrammer.Utils;

public class Lists6 {

	static List<String> reverseList(final List<String> x) {
		List<String> y = new ArrayList<>();
		for (int i = x.size() - 1; i >= 0; --i) {
			y.add(x.get(i));
		}
		return y;
	}

	public static void main(String[] args) {
		List<String> names = Utils.listFromStrings(new String[]{"Fred", "Wilma", "Barney", "Betty"});

		for (String name : names) {
			System.out.println("Hello, " + name + "!");
		}

		names = reverseList(names);
		for (String name : names) {
			System.out.println("Hello, " + name + "!");
		}

		System.out.println(Utils.listToString(names));
	}
}

Output
$ javac -Xlint Lists6.java $ java -ea Lists6 Hello, Fred! Hello, Wilma! Hello, Barney! Hello, Betty! Hello, Betty! Hello, Barney! Hello, Wilma! Hello, Fred! ["Betty", "Barney", "Wilma", "Fred"]
Maps3.java
import java.util.HashMap;
import java.util.Map;
import org.pureprogrammer.Utils;

public class Maps3 {

	static Map<String, Integer> convertKMtoMiles(final Map<String, Integer> x) {
		Map<String, Integer> y = new HashMap<>();
		for (String planet : x.keySet()) {
			y.put(planet, (int)(x.get(planet) * 0.621371 + 0.5));
		}
		return y;
	}

	public static void main(String[] args) {
		Map<String, Integer> planetDiametersInKM = Utils.map(new Object[][] {
			{"Mercury", 4879},
			{"Venus", 12103},
			{"Earth", 12756},
			{"Mars", 6794},
			{"Jupiter", 142985},
			{"Saturn", 120534},
			{"Uranus", 51115},
			{"Neptune", 49534},
			{"Pluto", 2374},
			{"Ceres", 946},
			{"Eris", 2326},
			{"Makemake", 1430}
		});

		Map<String, Integer> planetDiametersInMiles = convertKMtoMiles(planetDiametersInKM);
		for (String planet : planetDiametersInMiles.keySet()) {
			System.out.println(planet + " has a diameter of " + planetDiametersInMiles.get(planet) + " miles");
		}
	}
}

Output
$ javac -Xlint Maps3.java $ java -ea Maps3 Earth has a diameter of 7926 miles Mars has a diameter of 4222 miles Neptune has a diameter of 30779 miles Ceres has a diameter of 588 miles Jupiter has a diameter of 88847 miles Makemake has a diameter of 889 miles Saturn has a diameter of 74896 miles Venus has a diameter of 7520 miles Eris has a diameter of 1445 miles Uranus has a diameter of 31761 miles Mercury has a diameter of 3032 miles Pluto has a diameter of 1475 miles
Tuples3.java
import org.pureprogrammer.Tuple;
import org.pureprogrammer.Utils;

public class Tuples3 {

	static Tuple.T2<Integer, String> swap(final Tuple.T2<String, Integer> x) {
		Tuple.T2<Integer, String> y;
		y = Tuple.makeTuple(x.second(), x.first());
		return y;
	}

	public static void main(String[] args) {
		Tuple.T2<String, Integer> pair1 = Tuple.makeTuple("Hello", 5);
		Tuple.T2<Integer, String> pair2;
		pair2 = swap(pair1);
		System.out.println(Utils.tupleToString(pair1) + " becomes " + Utils.tupleToString(pair2));
	}
}

Output
$ javac -Xlint Tuples3.java $ java -ea Tuples3 <"Hello", 5> becomes <5, "Hello">
java

Questions

Projects

More ★'s indicate higher difficulty level.

References