LCC 6310 Computation as an Expressive Medium Lecture 6.

Slides:



Advertisements
Similar presentations
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Advertisements

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Sep 26, Fall 2006IAT 800 Lecture 6 Methods and Classes.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
IAT 800 Lecture 4. Sept 18, Fall 2006IAT 8002 Outline  Programming concepts –Methods –Classes  Talk about project 1  Reading: Read Chapters 1-4 of.
CPSC150 Abstract Classes and Interfaces Chapter 10.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
Oct 17, Fall 2006IAT 800 ArrayList, Text, and more Image Manipulation.
Abstract Classes and Interfaces
IAT 800 Lecture Oct, Fall 2006IAT 8002 Outline  Programming concepts –Super and this –Java SDK classes Lists Reading the Java docs –Programming.
IAT 355 Lecture 4 Computer Graphics: Rocket. May 9, 2014IAT 3552 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Classes / Objects An introduction to object-oriented programming.
CSC 212 – Data Structures Lecture 12: Java Review.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
PART 3. You have been using static methods in your programs before and methods are the building blocks of procedural programming. In this part of the.
CIT 590 Intro to Programming First lecture on Java.
Computation as an Expressive Medium Lab 3: (Methods and) Classes Jason Alderman.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/23 Outline Creating Subclasses Overriding Methods Class Hierarchies.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue.
Constructors & Garbage Collection Ch. 9 – Head First Java.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
OO terminology & objectives §Encapsulation l don’t touch my private data l get/set it using my public/package methods §Inheritance l a parent provides.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
IAT 265 Lecture 7 Objects and ArrayList. May 19, 2016IAT 2652 Outline  Programming concepts –Super and this –Java SDK classes Lists Reading the Java.
LCC 6310 Computation as an Expressive Medium
Computation as an Expressive Medium
LCC 6310 Computation as an Expressive Medium
Programming Language Concepts (CIS 635)
CS2102: Lecture on Abstract Classes and Inheritance
Object Oriented Programming (OOP) LAB # 8
Java – Inheritance.
Core Concepts.
Inheritance and Polymorphism
Lecture 6 Methods and Classes
Chapter 8 Inheritance Part 2.
More on Creating Classes part 3
Lecture 6 Methods, Classes, Inheritance
Presentation transcript:

LCC 6310 Computation as an Expressive Medium Lecture 6

Outline Programming concepts Programming concepts Programming questions related to Project 2? Super and this Java SDK classes Lists Reading the Java docs

Revisit our example So far we have a rocket that flies around in a field of asteroids and fires So far we have a rocket that flies around in a field of asteroids and fires Now we want our missiles to blow up asteroids Now we want our missiles to blow up asteroids 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 collision

The Java SDK Java comes with thousands of classes in the Java Platform API Java comes with thousands of classes in the Java Platform API Documentation is available on Sun’s website Documentation is available on Sun’s website You may want to download the documentation… Let’s look at ArrayList Let’s look at ArrayList

ArrayList It’s a resizeable list It’s a resizeable list Can add and delete things without worrying about declaring the size The main methods we care about are add(), get(), and remove(), and size() The main methods we care about are add(), get(), and remove(), and size() Steps in using ArrayList Steps 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 them

Parents and children Remember that we declared a child class ArmedRocket whose parent was Rocket Remember that we declared a child class ArmedRocket whose parent was Rocket Remember that classes are types Remember that classes are types So ArmedRocket is a type and Rocket is a type So, here are some legal assignments So, 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 illegal But 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() The argument type of the add method is Object The argument type of the add method is Object 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 ArrayList ArrayList.get(int i) – returns the ith object (starting with 0) ArrayList.get(int i) – returns the ith object (starting with 0) But this doesn’t work! But this doesn’t work! asteroids.get(i).draw(); Why?

Need to cast back from Object Since things are put in an ArrayList as Object, they come back out as object Since things are put in an ArrayList as Object, 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 Asteroid In the current code, detecting collision takes place in draw() In the current code, detecting collision takes place in draw() But it is cleaner (more object-oriented) if Asteroid itself knows how to detect collision But it is cleaner (more object-oriented) if Asteroid itself knows 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 - 26 && r.xPos <= xPos + 22) && (r.yPos >= yPos - 24 && r.yPos = yPos - 24 && r.yPos <= yPos + 26)) return true; return true; else else return false; return false; }

Destroying asteroids When a missile hits an Asteroid, we need to destroy it When a missile hits an Asteroid, we need to destroy it 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)); } }

Super and this this is a special variable that always refers to the current instance (object) this is a special variable that always refers to the 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) super is a special variable that always refers to the superclass portion of an object (the object cast into it’s superclass) super is a special variable that always refers to the superclass portion of an object (the object cast into it’s superclass) super.method() – calls a method on the superclass super() – calls a constructor on the superclass

Summary Learned how to use ArrayList, our first Java Platform class Learned how to use ArrayList, our first Java Platform class Learned about super and subclasses as types Learned about super and subclasses as types Any instance of a subclass is an instance of the superclass, but not visa-versa Can cast more abstract classes (parents) into more concrete classes (children) The Java keywords super and this The Java keywords super and this Special variables that can be used within a method to refer to yourself (the superclass portion of yourself and all of yourself)