Erinevus lehekülje "ITI0011:praktikum 5 T8" redaktsioonide vahel

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
(Uus lehekülg: 'Praktikumis käsitletavad teemad: * klass vs objekt * pärimine * klass kui tüüp == Exercise == There are three different type of vehicles: car, motorcycle, plane. All the ve...')
 
 
(ei näidata sama kasutaja üht vahepealset redaktsiooni)
1. rida: 1. rida:
 +
Tagasi [[ITI0011]] lehele.
 +
 +
== Üldine ==
 +
 +
Siin on praktikumi, mis toimub teisipäeval, 30. septembril kell 8, info.
 +
 
Praktikumis käsitletavad teemad:
 
Praktikumis käsitletavad teemad:
 
* klass vs objekt
 
* klass vs objekt
 
* pärimine
 
* pärimine
 
* klass kui tüüp
 
* klass kui tüüp
 +
 +
== Koodinäited ==
 +
 +
Main.java:
 +
<source lang="java">
 +
public class Main {
 +
 +
public static void main(String[] args) {
 +
Robot r = new Robot();
 +
Robot r2 = new Robot();
 +
 +
r.name = "Robo";
 +
r2.name = "Cop";
 +
 +
RobotAction ra = new RobotActionMove();
 +
RobotAction ra2 = new RobotActionOpen();
 +
 +
r.addAction(ra);
 +
r.addAction(ra2);
 +
 +
// for (int i = 0; i < r.getActions().length; i++) {
 +
// RobotAction a = r.getActions()[i];
 +
for (RobotAction a : r.getActionList()) {
 +
//for (int i = 0; i < r.actionCount; i++) {
 +
//if (r.actions[i] != null) {
 +
a.run();
 +
//}
 +
}
 +
}
 +
 +
}
 +
</source>
 +
 +
Robot.java:
 +
<source lang="java">
 +
public class Robot {
 +
public String name;
 +
 +
//private RobotAction[] actions = new RobotAction[10];
 +
//private int actionCount = 0;
 +
 +
private ArrayList<RobotAction> actionList = new ArrayList<RobotAction>();
 +
 +
public void addAction(RobotAction action) {
 +
//actions[actionCount++] = action;
 +
actionList.add(action);
 +
}
 +
 +
/*public RobotAction[] getActions() {
 +
return actions;
 +
}*/
 +
public ArrayList<RobotAction> getActionList() {
 +
return actionList;
 +
}
 +
}
 +
 +
</source>
 +
 +
RobotAction.java:
 +
<source lang="java">
 +
 +
public class RobotAction {
 +
public String name;
 +
 +
public void run() {
 +
System.out.println("run()");
 +
}
 +
}
 +
 +
</source>
 +
 +
RobotActionMove.java:
 +
<source lang="java">
 +
public class RobotActionMove extends RobotAction {
 +
@Override
 +
public void run() {
 +
System.out.println("move");
 +
}
 +
}
 +
</source>
 +
 +
RobotAction.java:
 +
<source lang="java">
 +
 +
public class RobotActionOpen extends RobotAction {
 +
@Override
 +
public void run() {
 +
System.out.println("open");
 +
}
 +
}
 +
 +
</source>
  
 
== Exercise ==
 
== Exercise ==

Viimane redaktsioon: 30. september 2014, kell 10:29

Tagasi ITI0011 lehele.

Üldine

Siin on praktikumi, mis toimub teisipäeval, 30. septembril kell 8, info.

Praktikumis käsitletavad teemad:

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

Koodinäited

Main.java: <source lang="java"> public class Main {

public static void main(String[] args) { Robot r = new Robot(); Robot r2 = new Robot();

r.name = "Robo"; r2.name = "Cop";

RobotAction ra = new RobotActionMove(); RobotAction ra2 = new RobotActionOpen();

r.addAction(ra); r.addAction(ra2);

// for (int i = 0; i < r.getActions().length; i++) { // RobotAction a = r.getActions()[i]; for (RobotAction a : r.getActionList()) { //for (int i = 0; i < r.actionCount; i++) { //if (r.actions[i] != null) { a.run(); //} } }

} </source>

Robot.java: <source lang="java"> public class Robot { public String name;

//private RobotAction[] actions = new RobotAction[10]; //private int actionCount = 0;

private ArrayList<RobotAction> actionList = new ArrayList<RobotAction>();

public void addAction(RobotAction action) { //actions[actionCount++] = action; actionList.add(action); }

/*public RobotAction[] getActions() { return actions; }*/ public ArrayList<RobotAction> getActionList() { return actionList; } }

</source>

RobotAction.java: <source lang="java">

public class RobotAction { public String name;

public void run() { System.out.println("run()"); } }

</source>

RobotActionMove.java: <source lang="java"> public class RobotActionMove extends RobotAction { @Override public void run() { System.out.println("move"); } } </source>

RobotAction.java: <source lang="java">

public class RobotActionOpen extends RobotAction { @Override public void run() { System.out.println("open"); } }

</source>

Exercise

There are three different type of vehicles: car, motorcycle, plane. All the vehicles have a method "move()" which will print out the text which which type of vehicle is moving and what is its max speed:

a car has a max speed of 210 km/h
a plane has a max speed of 800 km/h
a car has a max speed of 160 km/h

In addition, every vehicle type has a method "print()" which prints out all the information about the given vehicle:

Car:
name: Audi
max speed: 240 km/h
doors: 5
seats: 2

Plane:
name: boeing
max speed: 842 km/h
seats: 200
max altitude: 13000 m

A car has the following information:

  • number of seats,
  • max speed,
  • number of doors,
  • name.

A motorcycle has the following information:

  • max speed,
  • name,
  • max power (engine).

A plane has the following information:

  • number of seats,
  • max speed,
  • name,
  • max altitude.

In addition to "move()" method, plane should have method "takeOff()" which will print the max altitude of the object:

Taking off to 13000 m.

Write class definitions for all the classes. Try to write a super class which holds common information and behaviour. Every subclass describes only information or behaviour different from the super class.

Write a simple code which will create a collection (array, list, or something else) of vehicles. Add some vehicles to the collection. Loop over the collection and for every object call "move()" and "print()" methods. In case of plane, call "takeOff()" before "move()".