Date: Thu, 01 May 2003 19:15:15 -0500 (CDT) From: Mark Jacobson To: 810-061-02@uni.edu Subject: It is okay to have infinite loops, sometimes... It is sometimes necessary and useful to have an infinite loop. Since a Lane object has to generate Vehicles FOREVER, as long as the applet applicaton is running, the following would be perfectly okay. while (true) // Lane class suggestions { make a new Vehicle( arguments to constructor ); pause( some amount of time before making next Vehicle for this line ); } A Lane object generates Vehicles again and again and again, forever. Is there no end to this traffic, sayeth the Frog? !! What are the ARGUMENTS to the Vehicle contructor? ** 1. What picture is needed? This Lane is always all Jeeps, or all Vans, or all Taxis or all Buicks. And its always Jeeps driving left to right, or always Jeeps driving right to left. jeep_left.gif or jeep_right.gif would be the image. <------------ -------------> Or van_left.gif might be it. Or oldcar_right.gif might be! <----------- ---------------> ** 2. What speed is the speed ALWAYS for these vehicles for this Lane? It the speed is a negative number, they will be heading from the right edge of the screen to the left. If the speed is positive number, they will appear on the left edge of the screen and they will move toward the right edge of the screen or highway before they disappear. So the speed for ONE of the FOUR Lanes will always be the same. Image used: van_left.gif oldcar_right.gif theSpeed -8 12 Note that -8 requested in a -8 carPic.move(theSpeed, y); means that the x got smaller, which means the car moved toward the LEFT SIDE OF THE applet canvas... 12 carPic.move(theSpeed, 0); on the other hand, would mean that the carPic object moved 12 units to the RIGHT, toward the right side of the screen. ** 3. Where in the world is the Vehicle supposed to come into existence and first appear, before it moves again and again until is passes out of it sight? Seems like a reasonable parameter for the Vehicle constructor, right? The Location or the separated x and y coordinates WHERE THE CAR is to start at. That ONE or those TWO parameters are needed and the Lane always uses the SAME EXACT Location to start new Vehicle at. Go look at the online running applet and SEE this is true. Do you ever see the top lane have a Vehicle start halfway into the Lane? No. Every Jeep always starts at the same place. Either at the very left edge of the highway, in that 1st or 2nd or 3rd or bottom Lane, or at the very right edge of the highway, in the TOP or ONE OF THE MIDDLE or the BOTTOM LANE. That is a fact. Study the online example as you read and reread the assignment description and gradually one thing after another will make sense. --------- DO NOT TRY TO WRITE ALL THE CODE WITHOUT UNDERSTANDING THE PROBLEM OR THE HANDOUT. That is too time consuming, even though you are tempted to just start coding and then try to remove bugs. Its SOOOOOOOOOOOOOOO MUCH BETTER to think it through thoroughly in ENGLISH and write, write, write and diagram and make sure you have captured all of the HINTS in the handout of the assignment. The first step in problem solving is: 1. UNDERSTAND THE PROBLEM - let it talk to you. Rewrite the description of the problem. Draw pictures of the situation. Focus on WHAT the problem is. Focus on WHAT each object does. Focus on WHAT all the parts do and WHAT the interactions are. Who needs to KNOW WHAT? The Vehicle needs to know about the Frog. So it can see if it has hit the Frog. What does it do if it hit the Frog? It kills the Frog by calling it .kill() method. The second step of problem solving is: 2. Develop an algorith or PLAN and outline of HOW to solve the problem. Do this in pseudocode and informal English that is like code. See if your outline of the steps for how to achieve a certain effect, such as moving a car and keeping track of the distance moved and removing the car from the canvas when it has travelled far enough, makes sense. Rewrite the desription of the STEPS of the PLAN again to make it a better, more accurate desription and change the order of the steps as you see they might be wrong, or add steps if they are not enough. Ask if your PLAN or ALGORITHM is a good solution or HOW to solve step by step WHAT you are trying to achieve. ----------------------- RESIST THE URGE TO CODE ----------------------- The third step of problem solving is: 3. Code it in Java. Write the PIVs, the constructor, the method or methods that you just figured out a good step by step PLAN for. You resisted the urge to CODE and write Java and type Java into the computer by spending time in the 1. Understand the problem, WHAT it is phase and the 2. Develop and pseudocode PLAN desription outline of the steps or details needed to achieve it. Now you write the code, but based on a deep understanding and familiarity with the problem and all the objects and the things each object is responsible for and can do. cars need to ask the Frog if they hit it, by sending in their VisibleImage through the froggies overlap method... Lanes need to keep creating cars, periodically, after a suitable pause(). Oh ya, Lanes, are ActiveObjects!!!! ---------------------------- UNDERSTAND FIRST, CODE LATER Resist the urge to code ---------------------------- unless you want to debug for 10 hours instead of debugging for only 1 hour!!! 1. Understand it 2. Plan it 3. Code it <------- This is step number THREE. How many positive integers are there that are less than 3? There are TWO of them. Don't neglect those first two steps. ** 4. How far is a Vehicle supposed to travel? Does the value 600 make any sense here? Look at the Frogger application and see how long the FilledRect is that represents the highway, or rather how WIDE it is, what its width is. We might as well let the Vehicle know how far it has to travel as an ActiveObject so it can remove itself from the canvas when it is gone the distance, right? ** 5. The Frog needs to be passed to the Vehicle that is created? Each van or taxi or oldcar or jeep needs to be checking after every .move() it does, whether it has HIT the FROGGY. So to ask the frog if it is under our van or jeep, we need to be able to say frog.overlaps( myCoolVisibleImage ); and if the answer is TRUE, we frog.kill(); and if the answer is FALSE, the froggy is very relieved, and the game player of frogger is excited he or she is getting that frog across the busy highway, so far!!! ** 6. The DrawingCanvas is an argument to the constructor? You need to save the DrawingCanvas as a PIV inside the Lane class, so it can pass the canvas argument to the Vehicle constructor every time it generated a new jeep or van or oldcar or taxi. Why does the Vehicle have to have the DrawingCanvas? vehicle = new VisibleImage(vehiclePic, whereAt.getX(), whereAt.getY(), canvas); Does the above statement answer your question? In order to put that VisibleImage on the screen before we call the run() method by using the start() statement in the constructor for the Vehicle class, we need to tell the VisibleImage constructor the canvas... ---- ------------------------ ------ tell VisibleImage constructor canvas tell VisibleImage constructor canvas tell VisibleImage constructor canvas tell VisibleImage constructor canvas tell VisibleImage constructor canvas Why do we need to tell it that? Because the only class that directly has knowledge of our run as applet applet window and the canvas is: Frogger class!!! ------- public class Frogger extends WindowController { ---------------- Does Frog say extends WindowController???? NO! Does Lane say extends WindowController???? NO!! Does Vehicle extend WindowController???? NO!!! public class Frog public class Lane extends ActiveObject public class Vehicle extends ActiveObject public class Frogger extends WindowController ---------------- That is all the suggestions I have the energy for now. I wish the questions this afternoon had come yesterday, so I could have talked about this in class today. I know its tough to ask questions during class. I know this is a tough assignment. Good luck on the Frogger assignment. Watch for more hints. I will be flexible on the Friday due date. Mark