1. Homepage
  2. Programming
  3. [2022] INF1120 Praktische Informatik 2: Imperative und objektorientierte Programmierung - Exercise Sheet6 - Moth Simulator

[2022] INF1120 Praktische Informatik 2: Imperative und objektorientierte Programmierung - Exercise Sheet6 - Moth Simulator

Engage in a Conversation
Eberhard Karls University of TübingenTübingenINF1120Praktische Informatik 2Imperative und objektorientierte Programmierung

6.1 Moth Simulator

  CourseNana.COM

Using Object Oriented Programming, we can represent real-world objects and their interactions as program code. And what could be more realistic than moths? We will now develop a simulation containing thousands of Moths and several Lamps to fly towards. While Lamps will be stationary objects, Moths will fly around and we need to simulate their behavior for each time step. CourseNana.COM

  CourseNana.COM

To encapsulate the entire simulation, the class MothSimulation is already given. It contains arrays of all the Moths and all the Lamps in existence. To draw the current state of the MothSimulation, we use the class MothSimulationCanvas. It also allows users to interactively place additional Moths and Lamps. CourseNana.COM

  CourseNana.COM

6.1.1 Understand the Given Tasks and Code (priceless) CourseNana.COM

  CourseNana.COM

Have a thorough look at the entire exersice sheet and the given given code before you start programming anything yourself. Make sure you understand how the different classes interact with each other. If anything remains unclear, ask. Do not skip this step! CourseNana.COM

  CourseNana.COM

MothSimulationCanvas, is only used to draw the current simulation state and to allow you to add more objects. As you implement the classes required by the following tasks, you will have to uncomment some code in Task_6_1.java, Moth.java and MothSimulationCanvas.java to see something. Note that the GUI is only there for your enjoyment and will not be tested or graded :). CourseNana.COM

  CourseNana.COM

6.1.2 Set up the Simulation (5 P) CourseNana.COM

All simulated objects are contained by the class MothSimulation. Finish its implementation by implementing the following functions: CourseNana.COM

  CourseNana.COM

a) A getter for the moths array with name getMoths() and public visibility. CourseNana.COM

b) A getter for the lamps array with name getLamps() and public visibility. CourseNana.COM

The remaining parts of this class are already implemented and should not be modified to ensure that everything  runs smoothly. (The GUI code runs in parallel and requires a consistent state at all times.) CourseNana.COM

  CourseNana.COM

After implementing the getters, you can start uncommenting some code in Moth.java and MothSimulationCanvas.java. (See the // TODO comments in the code.) CourseNana.COM

  CourseNana.COM

6.1.3 Light up the Scene (14 + 16 P) CourseNana.COM

(a) StaticLamp (b) PulsatingLamp (c) FlickeringLamp CourseNana.COM

Figure 1: The types of Lamps, we wish to simulate. CourseNana.COM

  CourseNana.COM

To get some variety in our simulation, we use three types of Lamps (see Figure 1). The shared logic of all Lamps such as their ability to attract Mohts with a certain force is already implemented. The function CourseNana.COM

  CourseNana.COM

 public final Vec2D computeAttraction(Moth m) CourseNana.COM

  CourseNana.COM

determines the attraction force that the given Lamp inflicts on the given Moth. The getColor() function determines the color in which the lamp will be drawn on the screen and the abstract getBrightness() function needs to be implemented by each derived class to determine how bright the Lamp shines at every single moment. The default for this should be the defaultBrightness

CourseNana.COM

a) Implement the three Lamp classes, where each class shall be public, each class extends the Lamp class, and each class implements a public constructor which takes the parameters float x and float y for the Lamp’s  position and passes them through to the super class’s constructor CourseNana.COM

Lamp(float x, float y). CourseNana.COM

  CourseNana.COM

For the FlickeringLamp, add a private instance of the Flickerer class: private final Flickerer flickerer; and initialize it in your constructor using the Lamp’s position. CourseNana.COM

  CourseNana.COM

b) Implement the public float getBrightness() function for all three classes. CourseNana.COM

The StaticLamp shines with a constant brightness: CourseNana.COM

Simply return the defaultBrightness shared by all Lamps. CourseNana.COM

The PulsatingLamp smoothly goes up and down in brightness: CourseNana.COM

Call the protected function pulseStrength() of the class Lamp and use the result to scale the defaultBrightness. return the result of their product. CourseNana.COM

The FlickeringLamp turns on and off at seemingly random times: CourseNana.COM

Ask your flickerer instance if the Lamp isOn() and return the defaultBrightness or 0 based on the result. CourseNana.COM

  CourseNana.COM

You can now uncomment some code in MothSimulationCanvas.java (search for // TODO), to enable placing new Lamps into the simulation with the buttons provided by the GUI.

CourseNana.COM

6.1.4 Become Moth (10 + 5 + 5 + 10 + 15 P) CourseNana.COM

  CourseNana.COM

We wish to simulate several types of Moths, as shown in Figure 2. CourseNana.COM

  CourseNana.COM

Again, the abstract base class shared by all types of Moths is given. In addition to an x and y position contained in the Vec2D it derives from, it contains its flying direction (describing the rotation in radians where 0 is flying upwards) and speed. For the animation of flapping wings, it also contains a flapCounter, which you don’t need to bother with. CourseNana.COM

  CourseNana.COM

As each Moth is unique, its constructor only takes two floats describing its position in the 2D space and initializes all other class members randomly, including the maxSpeed and maxAcceleration. The maximum CourseNana.COM

speed and acceleration are constant per Moth and enforced by the given final updatePosition() CourseNana.COM

function. CourseNana.COM

  CourseNana.COM

Since Moths are also aware of their surroundings, they have the option to search for neighboring Moths using the lookupAccelerator. By default, only a simple brute-force search is performed, which computes the distance to all other Moths in existence. For now, we are satisfied with that and we will look at a better performing alternative in the following task. CourseNana.COM

  CourseNana.COM

a) Implement the three Moth classes. For now, implement the public constructor, passing through the x and y coordinates to the Moth constructor. Also implement the updateForces function as an empty function (for now). We will deal with the specifics of each Moth type shortly. CourseNana.COM

b) @Override the getColor() function to Color the DeadMoth GRAY and the LampMoth ORANGE, so we can distinguish them more easily. The BoidMoth should already be BLACK by default. CourseNana.COM

c) Make sure that the DeadMoth does not fly, i.e., it does not move at all. Only modify the DeadMoth class to achieve this. CourseNana.COM

d) The LampMoth flies towards the Lamp with the strongest attraction force. In the updateForces function, compute the attraction of each Lamp on the LampMoth (use public final Vec2D CourseNana.COM

  CourseNana.COM

Lamp.computeAttraction(Moth m)) and set the LampMoth’s acceleration to the strongest attraction force (the one with the maximum lengthSquared()). If there are no Lamps, there should be no acceleration. CourseNana.COM

e) The BoidMoth uses Reynold’s Algorithm [?] to simulate swarm behavior as in a flock of birds. CourseNana.COM

We consider thee forces: CourseNana.COM

cohesion makes the BoidMoth fly towards nearby Moths. CourseNana.COM

alignment makes the BoidMoth fly into the same direction as nearby Moths. CourseNana.COM

separation makes the BoidMoth fly away from other Moths which are too close. CourseNana.COM

All forces apply only within a limited search radius. The separation force applies within a much smaller search radius. Moths within this radius do not influence the cohesion and alignment forces. CourseNana.COM

  CourseNana.COM

Use the protected function computeBoidForces to compute these forces and also add the attraction CourseNana.COM

force of every Lamp in the simulation to compute the acceleration of the BoidMoth. (Make sure to check how the functions of the Vec2D class are implemented.) CourseNana.COM

  CourseNana.COM

You can now uncomment some code in MothSimulationCanvas.java (search for // TODO), to enable CourseNana.COM

placing new Moths into the simulation with the buttons provided by the GUI. CourseNana.COM

Get in Touch with Our Experts

WeChat WeChat
Whatsapp WhatsApp
Eberhard Karls University of Tübingen代写,Tübingen代写,INF1120代写,Praktische Informatik 2代写,Imperative und objektorientierte Programmierung代写,Eberhard Karls University of Tübingen代编,Tübingen代编,INF1120代编,Praktische Informatik 2代编,Imperative und objektorientierte Programmierung代编,Eberhard Karls University of Tübingen代考,Tübingen代考,INF1120代考,Praktische Informatik 2代考,Imperative und objektorientierte Programmierung代考,Eberhard Karls University of Tübingenhelp,Tübingenhelp,INF1120help,Praktische Informatik 2help,Imperative und objektorientierte Programmierunghelp,Eberhard Karls University of Tübingen作业代写,Tübingen作业代写,INF1120作业代写,Praktische Informatik 2作业代写,Imperative und objektorientierte Programmierung作业代写,Eberhard Karls University of Tübingen编程代写,Tübingen编程代写,INF1120编程代写,Praktische Informatik 2编程代写,Imperative und objektorientierte Programmierung编程代写,Eberhard Karls University of Tübingenprogramming help,Tübingenprogramming help,INF1120programming help,Praktische Informatik 2programming help,Imperative und objektorientierte Programmierungprogramming help,Eberhard Karls University of Tübingenassignment help,Tübingenassignment help,INF1120assignment help,Praktische Informatik 2assignment help,Imperative und objektorientierte Programmierungassignment help,Eberhard Karls University of Tübingensolution,Tübingensolution,INF1120solution,Praktische Informatik 2solution,Imperative und objektorientierte Programmierungsolution,