Presentation is loading. Please wait.

Presentation is loading. Please wait.

Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#

Similar presentations


Presentation on theme: "Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#"— Presentation transcript:

1 Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#

2 “AP Computer Science with.NET and J#” Lecture 12: Extending the Marine Biology Simulation Case Study Microsoft.NET Workshops for Teachers

3 12-3 MicrosoftAP Computer Science with.NET and J# Workshop Track LectureTopic 7Algorithms and Algorithm Analysis 8Debugging and Exception Handling 9Applying Object-oriented Design Principles in Your Classes 10Using Inheritance to Design a Set of Classes 11MBS CS: the Marine Biology Simulation Case Study 12Extending the MBS CS 13Interfaces and Interface-based Programming 14Extending the MBS CS, Part 2 15Stacks, Queues and Invariants 16Linked Data Structures and the Linked-list 17Trees and Recursion 18Collection Classes and Iteration 19Additional Resources and Ideas

4 12-4 MicrosoftAP Computer Science with.NET and J# Lecture — Objectives “Part 2 of our discussion of the Marine Biology Simulation Case Study...” Topics: –Learning your way around –Modifying the Fish class

5 12-5 MicrosoftAP Computer Science with.NET and J# Part 1 Learning Your Way Around

6 12-6 MicrosoftAP Computer Science with.NET and J# Core Classes Here's the design of the core classes: SquareEnvironment BoundedEnvUnboundedEnvSimulation extends DarterFish Fish SlowFish extends depends on

7 12-7 MicrosoftAP Computer Science with.NET and J# Recall What the Core Classes Do… Fish classes: –Fish are the entities that move around in the environment –Fish are uniquely identified by an ID –Fish also have a location, a direction and a color SquareEnvironment classes: –An environment has a data structure to keep track of the fish Simulation class: –Contains the code to drive the simulation

8 12-8 MicrosoftAP Computer Science with.NET and J# Navigating Around There are various ways to learn your way around the MBS… Read the JavaDocs –HTML-based docs in the Documentation sub-folder Browse the code: –skim the source code files –use Visual Studio’s Object Browser : Let’s you view all the components, classes, interfaces, etc. View menu, Object Browser –use Visual Studio’s Class View Let’s you navigate source code by class & method (vs. by file) View menu, Class View –use Visual Studio’s Class Designer (if available in your version) Let’s you view a UML-like diagram of the code Project menu, Add New Item…, Class Diagram

9 12-9 MicrosoftAP Computer Science with.NET and J# Example: Class View Easier to navigate classes in class view… –controlled via Solution Explorer window –View menu, Class View (or click tab) –double-click method to jump to code

10 12-10 MicrosoftAP Computer Science with.NET and J# Demo! Navigating the MBS CS…

11 12-11 MicrosoftAP Computer Science with.NET and J# Part 2 Modifying the Fish Class

12 12-12 MicrosoftAP Computer Science with.NET and J# The Basics of the Simulation Each step of the simulation gets the fish to "act"… public class Simulation { private Environment theEnv; private EnvDisplay theDisplay;. public void step() { Locatable[] fish = theEnv.allObjects(); // fish in env… for (int i=0; i<fish.length; i++) // for each fish, act… { Fish f = (Fish) fish[i]; f.act(); // fish may move, eat, breed, die, do nothing, … } theDisplay.showEnv(); // now draw result of fish acting up! } } //class public class Simulation { private Environment theEnv; private EnvDisplay theDisplay;. public void step() { Locatable[] fish = theEnv.allObjects(); // fish in env… for (int i=0; i<fish.length; i++) // for each fish, act… { Fish f = (Fish) fish[i]; f.act(); // fish may move, eat, breed, die, do nothing, … } theDisplay.showEnv(); // now draw result of fish acting up! } } //class

13 12-13 MicrosoftAP Computer Science with.NET and J# The Fish Class The Fish class is the base class for the Fish hierarchy –All fish inherit common behavior like the ability to “act” (e.g. move) public class Fish { private int myId; private Location myLoc; private Direction myDir;. public void act() { this.move(); } protected void move() { Location nextLoc = this.nextLocation(); // at random… if (nextLoc.equals(this.myLoc)) ; // no sense moving if locations are the same else // move to new location… this.changeLocation(nextLoc); } public class Fish { private int myId; private Location myLoc; private Direction myDir;. public void act() { this.move(); } protected void move() { Location nextLoc = this.nextLocation(); // at random… if (nextLoc.equals(this.myLoc)) ; // no sense moving if locations are the same else // move to new location… this.changeLocation(nextLoc); }

14 12-14 MicrosoftAP Computer Science with.NET and J# Defining New Fish Defining new fish is surprisingly easy given MBS's OO design –and the polymorphic programming style used within MBS Idea behind defining new fish: –simulation treats all fish the same — just calls act() –new types of fish need only change act() ’s behavior! public class Simulation { public void step() { Locatable[] fish = theEnv.allObjects(); for (int i=0; i<fish.length; i++) { Fish f = (Fish) fish[i]; f.act(); }. public class Simulation { public void step() { Locatable[] fish = theEnv.allObjects(); for (int i=0; i<fish.length; i++) { Fish f = (Fish) fish[i]; f.act(); }.

15 12-15 MicrosoftAP Computer Science with.NET and J# Example #1 Short-lived fish… –Fish live only 7 time steps, then die Extending app so that Fish are short-lived: 1.add a private instance field called age to all fish objects 2.make sure age is properly initialized to 0 (use initialize( ) method) 3.increment age in act() 4.act() then checks if fish has reached 8, and if so calls die( ) –code follows on the next slide…

16 12-16 MicrosoftAP Computer Science with.NET and J# Short-lived Fish public class Fish { protected int age;. private void initialize(...) { age = 0;. } public void act() { age++; if ( isInEnv() ) // then fish is still alive & part of environment if (age == 8) environment().remove(this); // remove THIS fish from env… else move(); } //act public class Fish { protected int age;. private void initialize(...) { age = 0;. } public void act() { age++; if ( isInEnv() ) // then fish is still alive & part of environment if (age == 8) environment().remove(this); // remove THIS fish from env… else move(); } //act

17 12-17 MicrosoftAP Computer Science with.NET and J# Build, Run & Test Build, run, create an environment, add some fish, & step…

18 12-18 MicrosoftAP Computer Science with.NET and J# Demo! Defining a short-lived fish…

19 12-19 MicrosoftAP Computer Science with.NET and J# Example #2 A better design would allow fish’s life-time to be overriden –i.e., allow new types of fish to control their own destiny –lots of ways to do this… One approach: –recode act( ) to call another method M to see if fish has died –if so, fish is removed from environment –if not, fish lives on –method M can be overriden by sub-classes to control life-time

20 12-20 MicrosoftAP Computer Science with.NET and J# Redesigned Fish Class By default, fish live forever –sub-classes override died( ) if they want to change this behavior… public class Fish {. public void act() { age++; if ( isInEnv() ) { if (died()) environment().remove(this); else move(); } // sub-classes can override to control life-time protected boolean died() { // by default, fish never die... return false; } public class Fish {. public void act() { age++; if ( isInEnv() ) { if (died()) environment().remove(this); else move(); } // sub-classes can override to control life-time protected boolean died() { // by default, fish never die... return false; }

21 12-21 MicrosoftAP Computer Science with.NET and J# Demo! Short-lived fish revisited…

22 12-22 MicrosoftAP Computer Science with.NET and J# Other Examples MBS CS includes other kinds of fish: –DarterFish dart ahead or turn around –SlowFish move sloooowly… –just need to update GUIForm.jsl to use these classes: a)view code behind GUIForm.jsl (within MBSGUI component) b)locate main( ) method that starts up MBS… c)uncomment code that associates a DisplayMap with DarterFish and SlowFish d)uncomment code to add “DarterFish“ and “SlowFish” to FishNameArray e)uncomment code to add images for DarterFish and SlowFish to FishImageArray f)run!

23 12-23 MicrosoftAP Computer Science with.NET and J# Demo! Adding DarterFish & SlowFish to the GUI…

24 12-24 MicrosoftAP Computer Science with.NET and J# Summary Case studies are an important tool for learning AP Marine Biology Simulation is a good app to study –component-based –elegant object-oriented design –large and complex enough to yield non-trivial study –lots of room for exploration & extension.NET and J# are a good vehicle for working with AP MBS –J# supports Java language & required subset of Java Class Library –.NET provides rich GUI –Visual Studio 2005 offers excellent programming environment

25 12-25 MicrosoftAP Computer Science with.NET and J# Resources Web site for slides, demos, associated lab exercises: –http://blogs.msdn.com/daryllmc/default.aspxhttp://blogs.msdn.com/daryllmc/default.aspx –http://www.lakeforest.edu/~hummel/workshops-HS.htmhttp://www.lakeforest.edu/~hummel/workshops-HS.htm –https://www.mainfunction.com/home/training/https://www.mainfunction.com/home/training/

26 12-26 MicrosoftAP Computer Science with.NET and J# That’s it! Next up: LectureTopic.. 13Interfaces and Interface-based Programming......

27


Download ppt "Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#"

Similar presentations


Ads by Google