ITI0011:praktikum 5 N8

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti

Tagasi ITI0011 lehele.

Üldine

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

Praktikumis käsitletavad teemad:

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

Koodinäited

Vehicle.java

<source lang="java"> public class Vehicle { public String model; public int maxSpeed; public int numberOfSeats;


public Vehicle() {

} public Vehicle(String model) { // constructor this.model = model; }

public void move() { System.out.println("vehicle starts moving"); } }

</source>

Boat.java <source lang="java"> public class Boat extends Vehicle { public int maxDepth;

public Boat(String model) { super(model); } } </source>

Car.java <source lang="java"> public class Car extends Vehicle { public int numberOfDoors;

public Car(String model) { super(model); } public Car(String model, int numberOfDoors) { super(model); this.numberOfDoors = numberOfDoors; }

@Override public void move() { // TODO Auto-generated method stub System.out.println(model + " starts moving, number of doors:" + numberOfDoors); } }

</source>

Plane.jave <source lang="java"> public class Plane extends Vehicle { public int maxAltitude;

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

} </source>

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

public class Main { public static void main(String[] args) { Car c = new Car("Audi", 4); Car c2 = new Car("Audi"); Boat b = new Boat("mootorpaat"); Plane p = new Plane("boeing 777"); Vehicle v = new Vehicle("tere");

System.out.println(c.model);

Car[] cars = new Car[10]; cars[0] = c;

for (Car cc : cars) { if (cc != null) { cc.move(); } }

Car ccc = new Car(""); Vehicle vv = new Car("tere"); Car c3 = (Car) vv; System.out.println(c3.numberOfDoors); // ((Car)vv).numberOfDoors System.out.println(((Car)vv).numberOfDoors); /*for (int i = 0; i < cars.length; i++) { Car cc = cars[i]; cc.move(); }*/

List<Vehicle> vehicles = new ArrayList<Vehicle>(); vehicles.add(c); vehicles.add(b); vehicles.add(p); vehicles.add(ccc);

for (Vehicle v2 : vehicles) { /*System.out.println(v2.model); System.out.println("altitude:" + ((Plane)v2).maxAltitude); if (v2 instanceof Plane) { System.out.println("altitude:" + ((Plane)v2).maxAltitude); }*/ v2.move(); } /* for (int i = 0; i < vehicles.size(); i++) { Vehicle v2 = vehicles.get(i); } */

} } </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.

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) 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.

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.