Kursliteratur och övriga resurser

The links to the resources on this page were verified on March 18, 2015. Let Alex know if you find any of them broken!

Kursboken

Kursens bok är Horstmann: Java for everyone, 2nd Edition. Läsanvisningar till kursboken finner du på föreläsningar tabellen.

Länkar till diverse information om Java

Några exempel

Exempel på att kolla indata

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();
      }
    }
  }
}

En klass exempel (till lab 3)

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;
  }
}

Några lambda expressions och higher-order funktioner i Java

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));
   }
}

Software tools

Det finns många tools för Java utveckling. I den här kursen använder vi IntelliJ. Den finns installerat på labbdatorerna.

Menu