:::::::::::::: Triangle.java :::::::::::::: class Experiment { public static void main(String[] args) { Triangle a = new Triangle(new Point(0, 3), new Point(4, 0), Point.origin); System.out.println(a.area()); // should be 6.0 } } class Triangle { Line a, b, c; Triangle (Point a, Point b, Point c) { this.a = new Line(a, b); this.b = new Line(a, c); this.c = new Line(b, c); } double area() { double s = (this.a.length() + this.b.length() + this.c.length()) / 2; return Math.sqrt(s * (s - a.length()) * (s - b.length()) * (s - c.length())); } } class Line { Point a, b; double length() { return a.distanceTo(b); } Line(Point u1, Point u2) { this.a = u1; this.b = u2; } } class Point { double x, y; Point(double x, double y) { this.x = x; this.y = y; } Point() { this(0, 0); } static Point origin = new Point(); double distanceTo(Point other) { double dx = this.x - other.x; double dy = this.y - other.y; return Math.sqrt(dx * dx + dy * dy); } } :::::::::::::: Clock.java :::::::::::::: class One { public static void main(String[] args) { Clock one = new Clock("2350"); for (int i = 0; i < 20; i++) { one.tick(); System.out.println(one.report()); } } } class Clock { int hours, minutes; Clock(String time) { hours = Integer.parseInt(time.substring(0, 2)); minutes = Integer.parseInt(time.substring(2)); } void tick() { minutes += 1; if (minutes == 60) { minutes = 0; hours += 1; } if (hours == 24) { hours = 0; } } String report() { String hours = "00" + this.hours; String minutes = "00" + this.minutes; return hours.substring(hours.length() - 2) + ":" + minutes.substring(minutes.length() - 2); } } :::::::::::::: Line.java :::::::::::::: class One { public static void main(String[] args) { Line a = new Line(new Point(0, 0), new Point (1, 1)); System.out.println(a.length()); } } class Line { Point a, b; Line(Point a, Point b) { this.a = a; this.b = b; } double length() { return (this.a).distanceTo(this.b); } } class Point { double x, y; Point(double x, double y) { this.x = x; this.y = y; } double distanceTo(Point other) { double dX = this.x - other.x, dY = this.y - other.y; return Math.sqrt(dX * dX + dY * dY); } } :::::::::::::: Paper.java :::::::::::::: class One { public static void main(String[] args) { Player bonaparte, wellington; bonaparte = new Player(); wellington = new Player(); System.out.println("Let the game begin!"); bonaparte.makeGuess(); wellington.makeGuess(); System.out.println("The guesses have been made: "); System.out.println(" Bonaparte has chosen .... " + bonaparte.report()); System.out.println(" Wellington has chosen ... " + wellington.report()); if (bonaparte.strongerThan(wellington)) System.out.println("Bonaparte wins!"); else if (wellington.strongerThan(bonaparte)) System.out.println("Wellington wins!"); else System.out.println("It's a draw..."); } } class Player { String guess; String makeGuess() { int value = (int) (Math.random() * 3); if (value == 0) this.guess = "paper"; if (value == 1) this.guess = "rock"; if (value == 2) this.guess = "scissors"; return this.guess; } boolean strongerThan(Player other) { if (this.guess.equals("paper") && other.guess.equals("rock" ) || this.guess.equals("rock" ) && other.guess.equals("scissors") || this.guess.equals("scissors") && other.guess.equals("paper")) return true; else return false; } String report() { return guess; } } :::::::::::::: Tigger.java :::::::::::::: class Tigger { String x, y; Tigger(int x, int y) { this.x = x + ""; this.y = y + ""; } void bounce() { int a = calculate(x), b = calculate(y); this.x = a + ""; this.y = b + ""; } int calculate(String a) { int sum = 0; for (int i = 0; i < a.length(); i++) { sum += (a.charAt(i) - '0') * (a.charAt(i) - '0'); } return sum; } String report() { String x = " " + this.x, y = " " + this.y; return "Tigger just bounced to (" + x.substring(x.length() - 3) + ", " + y.substring(y.length() - 3) + ")"; } } :::::::::::::: Elevator.java :::::::::::::: class One { public static void main(String[] args) { Elevator e = new Elevator(20); e.up(26); e.down(14); e.up(10); e.down(30); e.up(e.currentFloor() + 3); } } class Elevator { int floor; Elevator(int floor) { this.floor = floor; } void up(int to) { if (this.floor >= to) { System.out.println("Sorry, from floor " + this.floor + " we can't go up to floor " + to); } else { System.out.println("Elevator going up (" + this.floor + " --> " + to + ")"); for (int i = this.floor; i <= to; i++) { this.floor = i; this.report(); } System.out.println("Elevator now on floor: " + this.floor); } } void down(int to) { if (this.floor <= to) { System.out.println("Sorry, from floor " + this.floor + " we can't go down to floor " + to); } else { System.out.println("Elevator going down: (" + this.floor + " --> " + to + ")"); for (int i = this.floor; i >= to; i--) { this.floor = i; this.report(); } System.out.println("Elevator now on floor: " + this.floor); } } void report() { System.out.println(" The elevator is now on floor " + this.floor); } int currentFloor() { return this.floor; } } :::::::::::::: Robot.java :::::::::::::: class Robot { String direction; int x, y; String name; Robot(String n, int x, int y, String dir) { name = n; this.x = x; this.y = y; direction = dir; } void moveForward() { System.out.println("Robot " + name + " now moves forward."); if (direction.equals("North")) { y -= 1; } else if (direction.equals("South")) { y += 1; } else if (direction.equals("East")) { x += 1; } else if (direction.equals("West")) { x -= 1; } else { // nothing } } void turnLeft() { System.out.println("Robot " + name + " now turns left."); if (direction.equals("North")) { direction = "West"; } else if (direction.equals("South")) { direction = "East"; } else if (direction.equals("East")) { direction = "North"; } else if (direction.equals("West")) { direction = "South"; } else { // nothing } } int getX() { return x; } int getY() { return y; } String direction() { return direction; } void report() { System.out.println( "Robot " + name + " located at (" + x + ", " + y + ") facing " + direction ); } } So here's an example of using this class: class Walk { public static void main(String[] args) { Robot a = new Robot("Alice", 2, 3, "North"); a.report(); Robot q = new Robot("Queen", -4, -1, "West"); q.report(); a.turnLeft(); a.report(); a.moveForward(); a.report(); a.turnLeft(); a.report(); a.moveForward(); a.report(); a.moveForward(); a.report(); a.moveForward(); a.report(); q.moveForward(); q.report(); q.turnLeft(); q.report(); } }