ITI0011:praktikum 5 N14

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti

Tagasi ITI0011 lehele.

Üldine

Siin on praktikumi, mis toimub neljapäeval, 2. oktoobril kell 14, info.

Praktikumis käsitletavad teemad:

  • klass vs objekt
  • pärimine
  • klass kui tüüp

Koodinäited

Main.java

<source lang="java"> import java.util.ArrayList; import java.util.List;

public class Main {

public static void main(String[] args) { Vehicle v = new Vehicle("blah"); v.model = "paat"; v.speed = 90;

Motorbike m = new Motorbike("yamaha", true);

Train t = new Train("train");

Bus b = new Bus("ikarus", 36);

m.move();

Bus[] buses = new Bus[10]; buses[0] = b;

/*for (int i = 0; i < buses.length; i++) { Bus bus = buses[i]; }*/

for (Bus bus : buses) { if (bus != null) { bus.move(); } }

Vehicle v2 = new Train("rong"); Train t2 = (Train)v2; System.out.println(((Train)v2).cars);

ArrayList<Vehicle> vehicles = new ArrayList<Vehicle>();

vehicles.add(m); vehicles.add(b); vehicles.add(t);

for (Vehicle vv : vehicles) { vv.move(); if (vv instanceof Motorbike) { System.out.println(((Motorbike)vv).hasSidecar); } }

/*for (int i = 0; i < vehicles.size(); i++) { Vehicle vv = vehicles.get(i); }*/

}

} </source>

Vehicle.java <source lang="java"> public class Vehicle { public String model; public int speed;

public Vehicle(String model) { this.model = model; // register model in DB }

public void move() { System.out.println("cannot move!"); } } </source>


Bus.java <source lang="java"> public class Bus extends Vehicle { public int lineNumber;

public Bus(String model, int number) { super(model); lineNumber = number; }

public void move() { System.out.println("bus nr " + lineNumber + " is moving"); } } </source>


Train.java <source lang="java"> public class Train extends Vehicle { public int cars;

public Train(String model) { super(model); }

public void move() { System.out.println("train is moving"); } } </source>


Motorbike.java <source lang="java"> public class Motorbike extends Vehicle { public boolean hasSidecar = false;

/*public Motorbike() { super(""); }*/ public Motorbike(String model) { super(model); }

public Motorbike(String model, boolean hasSidecar) { // constructor super(model); this.hasSidecar = hasSidecar;

} public void move() { System.out.println("motorbike is moving"); } } </source>

Exercise

Write three classes: RobotSlow, RobotFast, RobotJumpHigh. Each must be a subclass of class Robot.

Every robot has a name, starting position (just a number, starting from 0) and movement speed.

Every robot has a method "move()", which should be implemented differently for different subclasses.

Properties and methods which every robot has, should be put into superclass (Robot).

RobotSlow has a fixed movement speed of 1 unit (the unit doesn't matter as all the values are measured in the same units). Its move() method just increases the robot's position by the value of movement speed (position = position + movement speed).

RobotFast has an additional property: acceleration. Its move() method increases the robot's position by movements speed + acceleration (position = position + movement speed + acceleration). Also, movements speed increases by acceleration (movement speed = movements speed + acceleration). So, if the movement speed is 10 and acceleration is 5, then after the first move() call, the movement speed is 15. After the second call the speed will be 20 etc.

RobotJumpHigh has additional properties: height - the maximum height the robot can jump - and wind speed. Its move() method increases the robot's position so that in addition to the movement speed, some additional distance is passed because of jumping. You should calculate a random value in the range [0, max height]. Now, for every height unit, the wind carries the robot further. So, the jump height you just got (a random number) will be multiplied by the wind speed. position = position + movement speed + jump height * wind speed.

Write a class for every robot and for the superclass. Write a program, which creates a list of (different type of) robots. For every robot in the list, call the move() method, which changes positions. If all the robots are looped through, print out the positions of the robots. Every instance has its own position (every robot has its own state) which changes.

Make a loop, which makes for example 5 turns like that (so, the loop for every robot should go inside another loop, which just repeats everything). Note that the positions should not be set to zero after turns. Only in the very beginning, the positions should be 0.