Anteckningar från föreläsning 2

Detta är den programkod vi skrev -- anteckningarna är gjorda bara för att vara ett stöd för minnet för dem som var där, och därför inte nödvändigtvis fullständiga eller ens sammanhängande. Betydligt mer hjälp finns att hämta i kompendiet.

OH-bilder finns här.

Repetition

import se.lth.cs.pt.io.Keyboard;

class BMI {

    public static void main(String[] args) {
        new BMI().run();
    }

    void run() {
        double weight = Keyboard.nextDouble("Ange vikt: ");
        double height = Keyboard.nextDouble("Ange längd: ");
        double bmi = weight / (height * height);
        System.out.print("BMI blir ");
        System.out.println(bmi);
        if (bmi < 18) {
            System.out.println("Ät mer");
        } else if (bmi <= 25) {
            System.out.println("Jättebra!");
        } else {
            System.out.println("Du är lite för kort");
        }
    }
}

Loopar

class ManyNumbers {

    public static void main(String[] args) {
        new ManyNumbers().run();
    }

    void run() {
        int count = 1;
        while (count <= 20) {
            System.out.println(count);
            count = count + 1;
        }
        System.out.println("Klar");
    }
}

import se.lth.cs.pt.io.Keyboard;

class Gauss {

    public static void main(String[] args) {
        new Gauss().run();
    }

    void run() {
        int limit = Keyboard.nextInt("Ange övre gräns: ");
        int sum = 0;
        int term = 1;
        while (term <= limit) {
            sum = sum + term;
            term = term + 1;
        }
        System.out.print("Summan är ");
        System.out.println(sum);
    }
}

import se.lth.cs.pt.io.Keyboard;

class GaussWithFor {

    public static void main(String[] args) {
        new GaussWithFor().run();
    }

    void run() {
        int limit = Keyboard.nextInt("Ange gräns: ");
        int sum = 0;
        for (int term = 1; term <= limit; term = term + 1) {
            sum = sum + term;
        }
    }
}

class GoodStudent {

    public static void main(String[] args) {
        new GoodStudent().run();
    }

    void run() {
        for (int k = 1; k <= 10; k = k + 1) {
            System.out.println("Jag lovar att göra precis som Christian säger");
        }
    }
}

class Backwards {

    public static void main(String[] args) {
        new Backwards().run();
    }

    void run() {
        for (int count = 10; count >= 1; count = count - 1) {
            System.out.println(count);
        }
    }
}

Objekt och referenser

import se.lth.cs.pt.graphics.basic.*;
import se.lth.cs.pt.turtle.visible.Turtle;

class Walkabout {

    public static void main(String[] args) {
        new Walkabout().run();
    }

    void run() {
        GraphicsWindow w = new GraphicsWindow(600, 500);
        Turtle t = new Turtle(w, 300, 250);
        t.penDown();
        t.forward(100);
        t.left(120);
    }
}

import se.lth.cs.pt.graphics.basic.*;
import se.lth.cs.pt.turtle.visible.Turtle;

class Dating {

    public static void main(String[] args) {
        new Dating().run();
    }

    void run() {
        GraphicsWindow w = new GraphicsWindow(800, 200);
        Turtle t1 = new Turtle(w, 50, 100);
        t1.right(90);
        Turtle t2 = new Turtle(w, 750, 100);
        t2.left(90);
        while (t1.getX() < t2.getX()) {
            t1.forward(10);
            t2.forward(12);
        }
    }
}