info0062 - object-oriented programming - exercise session ... · the abstract keyword exercises...

27
INFO0062 - Object-Oriented Programming Exercise session #6 - Transformations Jean-François Grailet University of Liège Faculty of Applied Sciences Academic Year 2018 - 2019

Upload: others

Post on 31-Oct-2019

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

INFO0062 - Object-Oriented ProgrammingExercise session #6 - Transformations

Jean-François Grailet

University of Liège

Faculty of Applied Sciences

Academic Year 2018 - 2019

Page 2: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword

Page 3: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

The abstract keyword in practice

You can use abstract for a class or a method.

Using abstract on a class

• The class won’t be instantiable, but can be extended.

Using abstract on a method

• The method will be defined without an implementation.

• Non-abstract child classes will have to implement it.

• When implementing, the signature must be the same minus the abstract keyword.

N.B.: you can’t create abstract methods in a non-abstract class.

• But you can create an abstract class without abstract methods.

1 / 23

Page 4: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

The abstract keyword in practice (II)

Example of abstract class with abstract methods.

public abstract class Quadrilateral{

protected int s1, s2, s3, s4; // Length of each sideprotected double a1, a2, a3, a4; // Angles between sides

public abstract int computeArea();public abstract int computePerimeter();

}

2 / 23

Page 5: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

The abstract keyword in practice (III)

Example of a class inheriting Quadrilateral.

public class Square extends Quadrilateral{

// ... (includes constructor)

public int computeArea(){

return s1 * s1;}

public int computePerimeter(){

return s1 * 4;}

}

3 / 23

Page 6: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

The abstract keyword in practice (IV)

Example of abstract class without abstract methods.

public abstract class AbstractPoint{

public double[] c; // Coordinates

public AbstractPoint(){

c = null; // To be set by constructors of child classes}

public double distance(AbstractPoint p){

double sum = 0.0;for(int i = 0; i < c.length; i++)

sum += (p.c[i] - c[i]) * (p.c[i] - c[i]);return Math.sqrt(sum); // Euclidian distance

}}

4 / 23

Page 7: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

The abstract keyword in practice (V)

Example of a class inheriting AbstractPoint.

public class Point2D extends AbstractPoint{

public Point2D(double x, double y){

c = new double[2];c[0] = x;c[1] = y;

}}

Remark: ideally, distance() in AbstractPoint should check that both points havethe same amount of coordinates.

5 / 23

Page 8: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

Exercises

Page 9: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Today’s menu

Regular exercises: transformations

6 / 23

Page 10: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Today’s menu (II)

Advanced exercise: shoot’em up draft (part 2)

7 / 23

Page 11: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Our tools for today

We will re-use again the Curve, Segment and Point classes.

You can get their lattest versions on the dedicated website in fractals.zip.

You can also re-use the classes providing main() methods in the same archive.

8 / 23

Page 12: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Our tools for today (II)

For some exercise, you might want to create and initialize arrays at once.

You can do it at declaration of a variable, like this:

double angles[] = {-Math.PI / 3, Math.PI / 3, 0};

But if the variable angles already exists somewhere, you should do this:

angles = new double[] {-Math.PI / 3, Math.PI / 3, 0};

9 / 23

Page 13: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Exercise 1

During session #5, we reviewed variants of Koch’s transformation and used inheritanceto implement 3 of them (including the original transformation). However, it might havecome to your attention that the operations in each transformation are always the same.From these observations, design an abstract class for implementing transformations.

Tip: consider using somewhere a method which receives a Segment to transform it.

public Segment transform(Segment initial){

// Code to transform "initial"}

10 / 23

Page 14: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Exercise 2

Taking advantage of the class you designed in the previous exercise, re-implementrespectively Koch’s transformation, its square variant and Minkowski’s transformation.Re-use a main() from last session and adapt it if necessary to test your solution.

Next slides will remind you how such transformations work.

11 / 23

Page 15: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Exercise 2 (II)

Original Koch’s transformation (repeated)

12 / 23

Page 16: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Exercise 2 (III)

First variant: Koch’s transformation with a square

Second variant: Minkowski’s transformation

13 / 23

Page 17: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Exercise 3

As a final exercise, experiment with transformations by creating an initial curve andtransforming it several times before drawing it. But instead of always using the sametransformation, change of transformation at specific or all iterations.

Next slides show examples of figures you can obtain.

14 / 23

Page 18: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Exercise 3 (II)

Triangle transformed with: Koch, square variant, Koch and square variant.

15 / 23

Page 19: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Exercise 3 (III)

Square transformed with Koch then 3 times with the square variant.

16 / 23

Page 20: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Exercise 3 (IV)

Square transformed with: square variant, Minkowski, square variant and Minkowski.

17 / 23

Page 21: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Exercise 3 (V)

Triangle transformed with: Koch, Minkowski and 2 more times with Koch.

18 / 23

Page 22: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

Advanced exercise

Page 23: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Shoot’em up draft, part 2

Re-using the solution for the advanced exercise of session #5 (either your own, eitherthe suggested solution) as a basis, your task is to further develop the game by includ-ing enemy mobiles the player can take down with his/her missiles. Ideally, the sameenemies should be able to fire at the player as well, the player disappearing from thescreen when hit.

To simplify a bit the problem, you can follow these specifications:

Player’s ship and enemies don’t move in the same screen areas.

• I.e., they can’t collide with each other.

You can restrict yourself to one enemy type.

To detect collisions, you can check if the boundaries of each item overlap.

You can spawn1 the same enemy or enemies every X seconds.

1FR: faire apparaître19 / 23

Page 24: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Shoot’em up draft, part 2 (II)

20 / 23

Page 25: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Some advice

In order to correctly deal with the collisions between each mobile and missiles whileupdating their positions on the screen accurately, you will need to order soundly theoperations carried out during your game loop.2 In this specific context, you can go withthe following order:

Step 1: updating the positions of both the mobiles and the missiles

Step 2: detecting collisions between missiles and mobiles

• Note that you can simplify this step with good design.

• For instance, a missile from an enemy can only collide with the player.

Step 3: checking if any mobile fired new missiles

Step 4: spawning new enemies (if it’s time to)

Step 5: clearing elements that are out of the game and draw everything

2If you start with the suggested solution for part 1, it’s in the play() method of GameWindow.21 / 23

Page 26: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Some advice (II)

It is also strongly recommended to take advantage of inheritance in this context. Hereare a few questions to guide you.

Besides graphics, are player’s and enemies’ missiles any different ?

Do the player’s ship and enemies share similarities ?

Is it possible to use a single method for collision detection ?

22 / 23

Page 27: INFO0062 - Object-Oriented Programming - Exercise session ... · The abstract keyword Exercises Advanced exercise The abstract keyword in practice You can use abstract for a class

The abstract keyword Exercises Advanced exercise

Some advice (III)

Again, it will be useful to implement and test your solution step by step.

First, create enemies that don’t fire anything and simply move.

Second, implement collision detection between player’s missiles and enemies.

Third, allow enemies to fire bullets/missiles and hit the player.

If you want to go a bit further with this exercise, you can do any of the following.

Implement a death animation when any mobile is hit.

• E.g., you can make a mobile blink3 on screen before completely disappearing.

Add HP (i.e., Health Points) to any mobile to resist a few hits.

When the player dies, show a Game Over screen.

Have fun !3FR: cligner

23 / 23