37
Aquarium Lab Series Developed by Alyce Brady of Kalamazoo College

Aquarium Lab Series

  • Upload
    helia

  • View
    22

  • Download
    0

Embed Size (px)

DESCRIPTION

Aquarium Lab Series. Developed by Alyce Brady of Kalamazoo College. Why Use This?. Review of concepts Declaring object variables, creating objects using constructors, invoking methods Conditional Execution (ifs) Iteration (for loops) One-dimensional Arrays - PowerPoint PPT Presentation

Citation preview

Page 1: Aquarium Lab Series

Aquarium Lab Series

Developed by Alyce Brady

of Kalamazoo College

Page 2: Aquarium Lab Series

Why Use This?

• Review of concepts– Declaring object variables, creating objects

using constructors, invoking methods– Conditional Execution (ifs)– Iteration (for loops)– One-dimensional Arrays– Encapulating behavior (classes)

• Intro to Marine Biology Case Study

Page 3: Aquarium Lab Series

Getting Started

• Instructions are at: – http://max.cs.kzoo.edu/patterns/JavaPatternLabs/Aqu

ariumLabSeries/

• Download the zip file and unzip it– AquariumLabSeries.zip

• Add full path to jpt.jar to the classpath– This holds the user interface classes

• Compile all source – Don’t forget the files in the BlackBoxClasses directory

Page 4: Aquarium Lab Series

First View of the Aquarium

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/ObjConstLabs.shtml#start

• Run the main in AquaSimApplication– Click on the Start button– You will see the initial empty aquarium– Click the top right X to close the window

Page 5: Aquarium Lab Series

First View of the Aquarium

Page 6: Aquarium Lab Series

Add 3 Fish

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/ObjConstLabs.shtml#construct

• Edit the main in AquaSimApplication.java– Create 3 AquaFish objects

• After the start button is pushed• Before the user interface is asked to show the

aquarium

– Still no fish!

Page 7: Aquarium Lab Series

Add 3 Fish

Page 8: Aquarium Lab Series

Show the 3 Fish

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/ObjConstLabs.shtml#invoke

• Edit the main in AquaSimApplication.java– Check the documentation on AquaSimGUI for

how to draw fish (cause them to show up)– Add calls to this method for each of the 3 fish

in the main • After you create the fish and draw the aquarium• Before you repaint to show the result

Page 9: Aquarium Lab Series

Show The 3 Fish

Page 10: Aquarium Lab Series

Make the Fish Move

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/ObjConstLabs.shtml#invoke

• Check the AquaFish documentation to find the method to move the fish– Add calls to this method for each of the 3 fish in the

main after you have displayed them• After the last code you added to the main to draw the fish

– Add the code to draw the aquarium and fish again and repaint to show the changes

Page 11: Aquarium Lab Series

Possible Problems

• If you forget to redraw the aquarium– you will see both locations of the fish

• If you don’t show the fish again– You won’t see them move

• If you don’t repaint after showing the fish again– You won’t see them move

• They display and move so fast– You won’t see them move

Page 12: Aquarium Lab Series

Pause Between Movements

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/ObjConstLabs.shtml#invoke

• Check the documentation for the AquaSimGUI class – To see how to pause

• Add a call to this method – Before you move the fish

Page 13: Aquarium Lab Series

Reverse at Wall

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/ConditionalLabs.shtml#if

• Fish get stuck when they hit the wall– Find a method in AquaFish that returns true when the

fish is at the wall– Find the method that reverses the fish

• Add code in the main to test if the fish is at the wall and if so reverse it– After you ask the fish to move

Page 14: Aquarium Lab Series

Reverse at Wall

Page 15: Aquarium Lab Series

Red or Blue Fish

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/ConditionalLabs.shtml#else

• Check the documentation for RandNumGenerator for how to get an object of the class Random– Put this before you create the fish in the main

• Declare a variable to hold the fish color– Color fishColor;

• Find how to get 0 or 1 from Random– If 0 set the fish color to Color.RED– Else set the fish color to Color.BLUE

• Specify the color when you create the fish

Page 16: Aquarium Lab Series

Red or Blue Fish

Page 17: Aquarium Lab Series

Rainbow of Colors

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/ConditionalLabs.shtml#elseif

• Change random.nextInt(int maxVal) to 6 to give you a number from 0 to 5

• Use if, and else if to assign the color– 0 = Color.RED– 1 = Color.ORANGE– 2 = Color.YELLOW– 3 = Color.GREEN– 4 = Color.BLUE– 5 = Color.MAGENTA

Page 18: Aquarium Lab Series

A Better Way

• Fish color shouldn’t be assigned in the main– They should be assigned in the constructor

that doesn’t take a color– But, this means changing the first line which

currently calls the other constructor– Instead pull out the code in the other

constructor and put it in an initialize

Page 19: Aquarium Lab Series

Rainbow of Colors

Page 20: Aquarium Lab Series

Loop 10 Times

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/RepetitionLabs.shtml#for

• Instead of moving forward just once move forward 10 times– Pause to see the previous position– Move the fish– Draw the aquarium and fish – Be sure to repaint the user interface to see the

changes

• Either start at 0 and loop while < 10 or start at 1 and loop while <= 10

Page 21: Aquarium Lab Series

User Specified Number of Steps

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/RepetitionLabs.shtml#input

• You can use the userInterface to ask the user for how many steps to use– Change the constructor parameters– Find the method that return the number of

steps– Change your loop to use this number

Page 22: Aquarium Lab Series

User Specified Number of Steps

Page 23: Aquarium Lab Series

Add ¼ Chance of Reversing

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/RepetitionLabs.shtml#or

• When a fish moves– Still reverse if at the wall– But also reverse if a random value from 0 to 3

is 0 (1/4 chance of reversing)– Do this in one if statement boolean

expression (using ‘or’)

Page 24: Aquarium Lab Series

Add ¼ Chance of Reversing

Page 25: Aquarium Lab Series

Add an Array of Fish

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/MoreFish.shtml#LIDS

• Change the constructor call for AquaSimGUI to ask the user how many fish to use

• Ask the user interface how many to use

• Create an array of AquaFish of that size

Page 26: Aquarium Lab Series

Add an Array of Fish

Page 27: Aquarium Lab Series

Create First and Last Fish

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/MoreFish.shtml#AccessFish

• Create a fish at the first position in the array and at the last and show both as well as the aquarium

Page 28: Aquarium Lab Series

Create First and Last Fish

Page 29: Aquarium Lab Series

Looping Through an Array

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/MoreFish.shtml#ProcessAll

• Use a for loop to create all the fish in the array

• Use another for loop to show them all

Page 30: Aquarium Lab Series

Looping Through an Array

Page 31: Aquarium Lab Series

Moving the Fish Again

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/MoreFish.shtml#School%20in%20Motion

• Add another loop to move each of the fish– You will need a nested loop

• Be sure to use a different index variable

– Don’t forget to show the fish again

Page 32: Aquarium Lab Series

Moving the Fish Again

Page 33: Aquarium Lab Series

Up and Down Fish

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/ImplementingClasses.shtml

• Add ascend and descend methods to AquaFish (by height)

• In the main before the fish moves do:– A fish at the surface has a 2/3 chance of descending and a 1/3

chance of staying at the surface. – A fish at the bottom has a 1/3 chance of ascending and a 2/3

chance of staying at the bottom. – A fish that is neither at the surface nor at the bottom has a 1/3

chance of ascending, a 1/3 chance of descending, and a 1/3 chance of staying at the same depth.

Page 34: Aquarium Lab Series

Up and Down Fish

Page 35: Aquarium Lab Series

AquaFish Move Method

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/ImplementingClasses.shtml

• The main should only create objects and start them doing something – it shouldn’t have behavior in it! – Create a new move method in AquaFish and move all

code related to moving the fish there– Change the main to call the new move method– Test to make sure you get the same behavior

Page 36: Aquarium Lab Series

AquaFish Move Method

Page 37: Aquarium Lab Series

Adding a Simulation Object

• http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/ImplementingClasses.shtml

• Finish the Simulation class– Finish the constructor– Finish the run method– Finish the step method

• In the main method– Create a simulation object

• Tell it to run the user specified number of steps

• Test to see if you get the same results