The links to the resources on this page were verified on July 18, 2018. Let Jesper know if you find any of them broken!
The course book is Horstmann: Java for everyone, 2nd Edition. Reading instructions for the course book can be found in the lectures table.
The Java JDK and the IntelliJ IDE are installed on the lab computers. If you want to install them on your own computer, you can use the links below:
The following links are not required reading, but it may be helpful to have a look at the same concepts from a different perspective.
SwapSlides.java
: An example related to lab session 2. You need the classes from the lab session as well as this file with the starting configuration.
import javax.swing.*;
import java.util.*;
public class Rektangel {
public static void main(String[] args) {
boolean cont = false;
int rader = 0;
int kolumner = 0;
// Get and check indata
String input = JOptionPane.showInputDialog(null, "Ge r och k: ");
if (input != null) {
Scanner sc = new Scanner(input);
if (sc.hasNextInt()) {
rader = sc.nextInt();
if (sc.hasNextInt()) {
kolumner = sc.nextInt();
cont = true; // korrekt indata, vi kan fortsätta
}
}
}
// Beräkning
if (cont) {
for (int i = 0; i < rader; i = i + 1) {
for (int j = 0; j < kolumner; j = j + 1) {
System.out.print("*");
}
System.out.println();
}
}
}
}
class Tone {
private int pitch;
private double duration;
Tone(int pitch, double duration) {
this.pitch = pitch;
this.duration = duration;
}
public int getPitch() {
return pitch;
}
public double getDuration() {
return duration;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
public class Lambda {
public static int[][] blackAndWhite(int[][] pixels) {
int[][] result = new int[pixels.length][pixels[0].length];
for (int i = 0; i < pixels.length; i++) {
for (int j = 0; j < pixels[i].length; j++) {
result[i][j] = pixels[i][j] > 128 ? 255 : 0;
}
}
return result;
}
public static int[][] sharpen(int[][] pixels) {
int[][] result = new int[pixels.length][pixels[0].length];
for (int i = 0; i < pixels.length; i++) {
for (int j = 0; j < pixels[i].length; j++) {
result[i][j] = pixels[i][j] > 128 ? Math.min(255, pixels[i][j] + 20)
: Math.max(0, pixels[i][j] - 20);
}
}
return result;
}
public static int[][] map(Function<Integer, Integer> f, int[][] matrix) {
int[][] result = new int[matrix.length][matrix[0].length];
for (int i = 0; i < matrix.length; i++)
for (int j = 0; j < matrix[i].length; j++)
result[i][j] = f.apply(matrix[i][j]);
return result;
}
public static int[][] blackNWhite(int[][] pixels) {
return map(Demo::borw, pixels);
}
public static int[][] zjarpen(int[][] pixels) {
return map(p -> p > 128 ? Math.min(255, p + 20) : Math.max(0, p - 20), pixels);
}
private static int borw(int pixel) {
return pixel > 128 ? 255 : 0;
}
public static int sum(List<Integer> xs) {
int res = 0;
for (int x : xs)
res += x;
return res;
}
public static int product(List<Integer> xs) {
int res = 1;
for (int x : xs)
res *= x;
return res;
}
public static <A, B> B foldr(BiFunction<A, B, B> op, B base, List<A> xs) {
if (xs.isEmpty())
return base;
else {
A x = xs.remove(0);
return op.apply(x, foldr(op, base, xs));
}
}
public static int summ(List<Integer> xs) {
return foldr((x, acc) -> x + acc, 0, xs);
}
public static int productt(List<Integer> xs) {
return foldr((x, acc) -> x * acc, 1, xs);
}
public static void main(String[] args) {
List<Integer> xs = new ArrayList<>();
xs.add(1); xs.add(2); xs.add(3); xs.add(4);
System.out.println(productt(xs));
}
}