DOC PREVIEW
GT LCC 6310 - LCC 6310 Lecture6

This preview shows page 1-2-3-4 out of 13 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

LCC 6310 Computation as an Expressive MediumOutlineRevisit our exampleThe Java SDKArrayListParents and childrenUsing ArrayList.add()Getting things out of an ArrayListNeed to cast back from ObjectPushing collision detection into the AsteroidDestroying asteroidsSuper and thisSummaryLCC 6310Computation as an Expressive MediumLCC 6310Computation as an Expressive MediumLecture 6Lecture 6OutlineOutline•Programming conceptsProgramming concepts•Programming questions related to Project 2?•Super and this•Java SDK classes•Lists•Reading the Java docs•Discuss readingDiscuss reading•Happenings in the New York Scene•The Cut-up Method of Brion Gysin•Six Selections of the OulipoRevisit our exampleRevisit our example•So far we have a rocket that flies around So far we have a rocket that flies around in a field of asteroids and firesin a field of asteroids and fires•Now we want our missiles to blow up Now we want our missiles to blow up asteroidsasteroids•This means we need a variable number of asteroids. •How do we do this with an array?•Use an ArrayList!•Also need to figure out when we have a collisionThe Java SDK The Java SDK •Java comes with thousands of classes Java comes with thousands of classes in the Java Platform APIin the Java Platform API•Documentation is available on Sun’s Documentation is available on Sun’s website website •You may want to download the documentation…•Let’s look at ArrayListLet’s look at ArrayListArrayList ArrayList •It’s a resizeable listIt’s a resizeable list•Can add and delete things without worrying about declaring the size•The main methods we care about are The main methods we care about are add()add(), , get()get(), and , and remove(), and size()remove(), and size()•Steps in using ArrayListSteps in using ArrayList•Declare a variable of type ArrayList•Create a new ArrayList and assign it to the variable•Call add(), get() and remove() and size() on ArrayList as you need themParents and childrenParents and children•Remember that we declared a child class ArmedRocket whose parent was Remember that we declared a child class ArmedRocket whose parent was RocketRocket•Remember that classes are typesRemember that classes are types•So ArmedRocket is a type and Rocket is a type•So, here are some legal assignmentsSo, here are some legal assignments•ArmedRocket r1 = new ArmedRocket(50, 60, 0);•Rocket r2 = new Rocket(50, 60, 0); • Rocket r3 = new ArmedRocket(50, 60, 0);•But this is illegalBut this is illegal•ArmedRocket r4 = new Rocket(50, 60, 0);•Same goes for method arguments as well…Same goes for method arguments as well…Using ArrayList.add()Using ArrayList.add()•The argument type of the add method is The argument type of the add method is ObjectObject•Object is the parent class of all classes•With an object argument type, you can pass in an object of any class•So, to initialize our asteroids…So, to initialize our asteroids… asteroids = new ArrayList();asteroids = new ArrayList(); for(int i = 0; i < numAsteroids; i++) for(int i = 0; i < numAsteroids; i++) asteroids.add(new Asteroid());asteroids.add(new Asteroid());Getting things out of an ArrayListGetting things out of an ArrayList•ArrayList.get(int i) – returns the ith ArrayList.get(int i) – returns the ith object (starting with 0)object (starting with 0)•But this doesn’t work!But this doesn’t work!•asteroids.get(i).draw();•Why?Need to cast back from ObjectNeed to cast back from Object•Since things are put in an ArrayList as Since things are put in an ArrayList as Object, they come back out as objectObject, they come back out as object•It’s like they forget their more detailed type•So, when using ArrayList (or any container class), need to cast back to the more detailed type Asteroid asteroid = (Asteroid)asteroids.get(i);Asteroid asteroid = (Asteroid)asteroids.get(i); if (!asteroid.collision(r1))if (!asteroid.collision(r1)) asteroid.draw();asteroid.draw();Pushing collision detection into the AsteroidPushing collision detection into the Asteroid•In the current code, detecting collision takes place in loop()In the current code, detecting collision takes place in loop()•But it is cleaner (more object-oriented) if Asteroid itself But it is cleaner (more object-oriented) if Asteroid itself knows how to detect collisionknows how to detect collision•Detecting collision depends on knowing the boundaries of the asteroid, which properly belongs in the asteroid class boolean collision(Rocket r) {boolean collision(Rocket r) { if ((r.xPos >= xPos - 26 && r.xPos <= xPos + 22) &&if ((r.xPos >= xPos - 26 && r.xPos <= xPos + 22) && (r.yPos >= yPos - 24 && r.yPos <= yPos + 26))(r.yPos >= yPos - 24 && r.yPos <= yPos + 26)) return true;return true; elseelse return false;return false; }}Destroying asteroidsDestroying asteroids•When a missile hits an Asteroid, we need to destroy When a missile hits an Asteroid, we need to destroy itit•This was the whole reason for using ArrayList•Big asteroids turn into two small asteroids•Small asteroids disappear void destroy(ArrayList asteroids) {void destroy(ArrayList asteroids) { asteroids.remove(this);asteroids.remove(this); if (large) {if (large) { asteroids.add(new Asteroid(false, xPos, yPos, lastDrawMillis));asteroids.add(new Asteroid(false, xPos, yPos, lastDrawMillis)); asteroids.add(new Asteroid(false, xPos, yPos, lastDrawMillis));asteroids.add(new Asteroid(false, xPos, yPos, lastDrawMillis)); }} }}Super and thisSuper and this•thisthis is a special variable that always refers to the is a special variable that always refers to the current instance (object)current instance (object)•Useful in methods to refer to yourself•this.method() – calls a method on yourself (but normally you just directly call the method)•this() – calls a constructor on yourself (useful for one version of a constructor to call another)•supersuper is a special variable that always refers to the is a special variable that always refers to the superclass portion of an object (the object cast into superclass portion of an object (the object cast into it’s superclass)it’s superclass)•super.method() – calls a method on the superclass•super() – calls a constructor on the superclassSummarySummary•Learned how to use ArrayList, our first Java Platform Learned how to use ArrayList, our first Java Platform classclass•Learned about super and


View Full Document

GT LCC 6310 - LCC 6310 Lecture6

Documents in this Course
Load more
Download LCC 6310 Lecture6
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LCC 6310 Lecture6 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LCC 6310 Lecture6 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?