Phys/CSci 135: Assignments
Home Syllabus Assignments Samples

Lab 5. Object-Oriented Programming

ForwardMove (page 37)

import lejos.nxt.*;

public class ForwardMove implements Action {
    public void act() {
        Motor.B.forward();
        Motor.C.forward();
    }

    public static void main(String[] args) {
        Action mover = new ForwardMove();
        mover.act();
        while (Button.ESCAPE.isPressed() == false) {}
    }
}

WaitOnEscape (page 38)

import lejos.nxt.*;

public class WaitOnEscape implements Action {
    public void act() {
        while (Button.ESCAPE.isPressed() == false) {}
    }

    public static void main(String[] args) {
        Action waiter = new WaitOnEscape();
        Motor.B.forward();
        waiter.act();
    }
}

PathPlan1 (page 38)

public class PathPlan1 {
    public static void main(String[] args) {
        Action[] plan =
            new Action[] {
                new ForwardMove(),
                new WaitOnLeft(),
                new TurnLeftMove(),
                new WaitOnRight(),
                new ForwardMove(),
                new WaitOnEscape()};

        for (Action a: plan) {a.act();}
    }
}

Action (page 39)

public interface Action {
    public void act();
}

WaitOnButton (page 40)

import lejos.nxt.*;

public class WaitOnButton implements Action {
    private Button waitingOn;

    public WaitOnButton(Button b) {
        waitingOn = b;
    }

    public void act() {
        while (waitingOn.isPressed() == false) {}
    }

    public static void main(String[] args) {
        Action waiter = new WaitOnButton(Button.ESCAPE);
        Motor.B.forward();
        waiter.act();
    }
}

RotationDelay (page 41)

import lejos.nxt.*;

public class RotationDelay implements Action {
    private int numCounts;
    private Motor tracked;

    public RotationDelay(Motor tr, int nc) {
        numCounts = nc;
        tracked = tr;
    }

    public void act() {
        while (Math.abs(tracked.getTachoCount()) < numCounts) {}
    }

    public static void main(String[] args) {
        Action delay = new RotationDelay(Motor.B, 200);

        Motor.B.forward();
        Motor.C.forward();
        delay.act();
    }
}

Escape (page 42)

import lejos.nxt.*;

public class Escape implements Action {
    private TouchSensor leftBump, rightBump;
    private Action reactToLeft, reactToRight;

    public Escape(TouchSensor lft, TouchSensor rgt,
                  Action lBumped, Action rBumped) {
        leftBump = lft;
        rightBump = rgt;
        reactToLeft = lBumped;
        reactToRight = rBumped;
    }

    public void act() {
        while (Button.ESCAPE.isPressed() == false) {
            if (leftBump.isPressed() == true) {
                reactToLeft.act();
            } else if (rightBump.isPressed() == true) {
                reactToRight.act();
            } else {
                Motor.B.stop();
                Motor.C.stop();
            }
        }
    }
}

TurnEscape1 (page 43)

import lejos.nxt.*;

public class TurnEscape1 {
    public static void main(String[] args) {
        Escape esc = new Escape(new TouchSensor(SensorPort.S1),
                                new TouchSensor(SensorPort.S2),
                                new TurnLeftMove(),
                                new TurnRightMove());
        esc.act();
    }
}

StopGo (page 44)

import lejos.nxt.*;

public class StopGo implements Action {
    private Condition stopWhen;

    public StopGo(Condition cond) {
        stopWhen = cond;
    }

    public void act() {
        while (Button.ESCAPE.isPressed() == false) {
            if (stopWhen.conditionMet() == true) {
                Motor.B.stop();
                Motor.C.stop();
            } else {
                Motor.B.forward();
                Motor.C.forward();
            }
        }
    }
}

Condition (page 45)

public interface Condition {
    public boolean conditionMet();
}

Dark (page 45)

import lejos.nxt.*;

public class Dark implements Condition {
    private LightSensor light;

    public Dark(LightSensor ls) {
        light = ls;
    }

    public boolean conditionMet() {
        return light.readValue() < 40;
    }
}

StopGoDark (page 45)

import lejos.nxt.*;

public class StopGoDark {
    public static void main(String[] args) {
        LightSensor light = new LightSensor(SensorPort.S3);
        Condition dark = new Dark(light);
        Action a = new StopGo(dark);
        a.act();
    }
}