Skip to topic | Skip to bottom
Minfo.SeTD1y1314r1.2 - 10 Oct 2013 - 14:19 - PhilippeCollettopic end

Start of topic | Skip to actions

Lab 1 : Reflexivity in Java, hands on the running project

PhilippeCollet (with elements from Michel Buffa and Filip Krikava)

Before Starting

To be installed:

If you're using the linux desktop in the Lucioles building:

  • a JDK 6 and Eclipse 3.7.2 are installed (this is completely usable for all labs).
  • ALERT! Do not use the default gnu JDK (gcj / open jdk) in your PATH, this does not work with Eclipse!
  • setup your environment as follows:
  export PATH=/usr/local/java/jdk1.6.0_21/bin:$PATH
  • start Eclipse like this:
  /usr/local/eclipse/eclipse-3.7.2/eclipse &
  • In case of crash with "OutOfMemory" error, create an alias or start Eclipse as follows:
  /usr/local/eclipse/eclipse-3.7.2/eclipse -vmargs -Xmx512M &


In this lab we will overview:

  • some of the basic principles of reflection in Java
  • the running example application that will be used through the course (and some coding with it).

Exercise 1 : A class analyzer

Start by copying the following source into your new Eclipse project.

Write a simple class analyzer. Such an analyzer expects a name of a class and it will print to the standard output its interface: its declaration, declared fields, constructors and methods.

Here is a sample output given java.util.ArrayList as an input:

For start do not worry about generics. There is a bonus exercise for you to do if you have enough time (meaning you have completed all the other exercises) or to do at home.

ALERT! stop this exercise when you have completed the first part of the analyzer (class header, inheritance clause, fields).

Exercise 2 : Array Grow

Once you complete the method and run, you should see following output:

[1, 2, 3, 0, 0, 0]
[Maurice Chombier, Francois Pignon, null, null]
The following call will generate an exception.
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ltp2.ArrayGrowTest2$Personne;

The nulls on the first line are fine - proving the the array has grown

  • Look at the methods from the java.util.Arrays class.

Exercise: Getting started with the creature simulator

The initial code is provided in the archive.

This code contains an application that creates an little framework for simulating creatures. A creature is basically just an object that has some properties (like position, direction, speed, color, ...) and behavior. It can act - change its properties and it knows how to render itself on a graphical canvas. In this lab version, a creature has also a vision. It can see other creatures within certain field of view and a certain distance.

The framework is built on the top of Java Swing library.

Starting with the creature simulator

  1. Create a new Java project in Eclipse
  2. Correctly place the given source files
  3. Make sure you have no errors in the Problems view
  4. Go over the classes to get the notion about how it works

Exercise 3: A generic toString() method

In the creature simulator's AbstractCreature class from the previous lab write a generic toString() method:

public String toString() {...}

which will output all of the creature's attributes (fields). It has to be generic method since subclasses of the AbstractCreature can define their own attributes this class is not aware of.

  • Think what is the difference between getDeclaredFields() and getFields()
  • To test it, you can add some more attributes to the SmartCreature

Exercise 4: Smart creature

The goal of this exercise is to create a new kind of creature is a bit smarter than the stupid one. It should have following behavior:

  1. It should try to align its speed with the speed of the creatures around.
  2. It should go in the same direction as the creatures around.
  3. It should maintain some minimal distance from the creatures around.

Exercise: Code analysis of the creature simulator

The given code is not well written. Now you will play a role of a senior software developer whose task is to review a code that has been produce by some junior. Try to come up with as many improvements as possible. For example:

  • What to do with some of the utility functions?
  • What to do with the fact that logic of the Environment and the visualization are kept together?
  • ...


Coordinates and Angles

Overview of the coordinate system. We use the common Euclidian two-dimensional geometry with the coordinate system origin in the middle of the plane.


Method directionFromAPoint() in AbstractCreature

Explanation of the directionFromAPoint() method.


Method: creaturesAround() in Environment

What is considered to be a creature near by.


Method: act() in SmartCreature

The position change based on computed speed and direction.


-- PhilippeCollet - 10 Oct 2013

to top

I Attachment sort Action Size Date Who Comment
increment.png manage 8.5 K 09 Oct 2013 - 21:10 PhilippeCollet  
creaturesAround.png manage 16.1 K 09 Oct 2013 - 21:10 PhilippeCollet  
directionFromAPoint.png manage 23.8 K 09 Oct 2013 - 21:10 PhilippeCollet  
graphicalSystem.png manage 8.9 K 09 Oct 2013 - 21:10 PhilippeCollet manage 7.0 K 10 Oct 2013 - 14:15 PhilippeCollet  

You are here: Minfo > SeTD1y1314

to top

Copyright © 1999-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding WIKIDeptinfo? Send feedback