LCC 6310 The Computer as an Expressive Medium. Lecture 9

Similar documents
JAVA MOCK TEST JAVA MOCK TEST II

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Collections, Maps and Generics

Super-Classes and sub-classes

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Inheritance. COMP Week 12

What is a variable? a named location in the computer s memory. mousex mousey. width height. fontcolor. username

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

CSE 113 A. Announcements - Lab

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Chapter 6 Introduction to Defining Classes

CS 231 Data Structures and Algorithms, Fall 2016

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CMSC 132: Object-Oriented Programming II

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

C# Programming for Developers Course Labs Contents

CS 112 Introduction to Programming

CS 177 Week 15 Recitation Slides. Review

Introduction to Programming Using Java (98-388)

JAVA: A Primer. By: Amrita Rajagopal

CS61B Lecture #12. Today: Various odds and ends in support of abstraction.

Declarations and Access Control SCJP tips

Lecture Set 4: More About Methods and More About Operators

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

1 Shyam sir JAVA Notes

Lab Lab 4 : Pretty, Pretty Pretty, Pretty Pictures Picture Joshua Cuneo

Compilers CS S-10 Object Oriented Extensions

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

Garbage Collection (1)

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Computer Science II (20073) Week 1: Review and Inheritance

Inheritance. Transitivity

Object-Oriented Programming in Processing

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Rules and syntax for inheritance. The boring stuff

ECE 122. Engineering Problem Solving with Java

CS260 Intro to Java & Android 03.Java Language Basics

CS 201 Advanced Object-Oriented Programming Lab 3, Asteroids Part 1 Due: February 17/18, 11:30 PM

Final Exam Winter 2013

QUIZ. What is wrong with this code that uses default arguments?

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner

Inheritance and Interfaces

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

Java Object Oriented Design. CSC207 Fall 2014

Lecture Set 4: More About Methods and More About Operators

COMP 250 Fall inheritance Nov. 17, 2017

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Computer Science II (20082) Week 1: Review and Inheritance

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CMSC 341 Lecture 10 Binary Search Trees

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

INHERITANCE. Spring 2019

Week. Lecture Topic day (including assignment/test) 1 st 1 st Introduction to Module 1 st. Practical

ICS 4U. Introduction to Programming in Java. Chapter 10 Notes

CSE 431S Type Checking. Washington University Spring 2013

OOP Subtyping. Lecture 20

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

G Programming Languages - Fall 2012

Chapter 3: Inheritance and Polymorphism

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

VARIABLES AND TYPES CITS1001

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

CS 251 Intermediate Programming Inheritance

The class Object. Lecture CS1122 Summer 2008

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

McGill University School of Computer Science COMP-202A Introduction to Computing 1

CS/ENGRD 2110 SPRING 2018

CSC Inheritance. Fall 2009

Big software. code reuse: The practice of writing program code once and using it in many contexts.

ITI Introduction to Computing II

CS1020: DATA STRUCTURES AND ALGORITHMS I

Inheritance, part 2: Subclassing. COMP 401, Spring 2015 Lecture 8 2/3/2015

Chapter 5. Condi.onals

Project. C++: Inheritance III. Plan. Project. Before we begin. The final exam. Advanced Topics. Project. This week in the home stretch

What are the characteristics of Object Oriented programming language?

int[] a; a = new int[8]; a[0] = 1; a[1] = 1; for (int i = 2; i < 8; i++) a[i] = a[i-1] - a[i-2];

C++ Important Questions with Answers

Semantic Analysis. Lecture 9. February 7, 2018

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

COP 3330 Final Exam Review

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

ITI Introduction to Computing II

CS2102: Lecture on Abstract Classes and Inheritance. Kathi Fisler

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Arrays Classes & Methods, Inheritance

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Transcription:

LCC 6310 The Computer as an Expressive Medium Lecture 9

Overview Course pace Programming questions related to project 2? (due Friday 5pm!) Programming concepts super and this Java SDK classes Lists Reading the Java docs Free time to work on project 2

Project 2 Create your own drawing tool, emphasizing algorithmic generation/ modification/manipulation. Explore the balance of control between the tool and the person using the tool. The tool should do something different when moving vs. dragging (moving with the mouse button down). The code for your tool should use at least one class. Focus on: - Conceptualization & Motivation - Design - Execution Artist statement required!

Revisiting our example So far we have a rocket that flies around in a field of asteroids and fires missiles 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? (They have a fixed size) We can use a special Java object called an ArrayList! We'll also need to figure out when we have a collision

The Java SDK (Java SDK = Java Software Developer's Kit) Java comes with thousands of classes in the Java Platform API Documentation is available on Sun s website http://java.sun.com/j2se/1.4.2/docs/api/ http://java.sun.com/j2se/1.5.0/docs/api/ Note that Processing supports Java 1.4.2 features (not yet 1.5) even though versions after 0136 require Java 1.5 to run You may want to download the documentation Let s look at ArrayList

ArrayList is a resizeable list ArrayList This means we can add and delete things from the list without worrying about declaring the size up front The main methods we care about are add(), get(), remove(), and size() http://java.sun.com/j2se/1.4.2/docs/api/ Steps for using ArrayList Declare a variable of type ArrayList Create a new ArrayList and assign it to the variable Call add(), get(), remove() and size() on the ArrayList as you need them

Parents and children Remember that we declared a child class ArmedRocket whose parent was Rocket, and remember that classes are types So ArmedRocket is a type and Rocket is a type 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 one is illegal: ArmedRocket r4 = new Rocket(50, 60, 0); Same goes for method arguments as well

Using ArrayList.add() 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, let's declare our asteroids ArrayList ArrayList asteroids; And then initialize our asteroids asteroids = new ArrayList(); for (int i = 0; i < NUM_ASTEROIDS; i++) { asteroids.add(new Asteroid()); Let's try this in Processing

Getting things out of an ArrayList Looking at the API doc for ArrayList http://java.sun.com/j2se/1.4.2/docs/api/ ArrayList.get(i) Returns the i-th object (starting with 0, i is an int) But this doesn t work! asteroids.get(i).drawme(); Why not?

Need to cast back from Object Since things are put into an ArrayList as Object, they also come back out as Object It s like they forget their more detailed type So, when using ArrayList (or any container class), we need to cast back to the more detailed type. Casting is the conversion of something from one data type to another - requires care!! Asteroid asteroid = (Asteroid)asteroids.get(i); if (!asteroid.collision(r1)) { asteroid.drawme(); Hmmm we haven't seen this way of doing collision checking yet. Let's work on that now

Putting collision detection in Asteroid Currently, detecting collision takes place in the main draw() method But it would be cleaner (more object-oriented) if Asteroid itself knew how to detect collision Detecting collision depends on knowing the boundaries of the asteroid, which properly belongs in the asteroid class So to detect collision with a Rocket boolean collision(rocket r) { if ((r.xpos >= xpos - 26 && r.xpos <= xpos + 22) && (r.ypos >= ypos - 24 && r.ypos <= ypos + 26)) { return true; else { return false; We also want to detect collisions with a Missile

More collision detection We can use the same method name with a different type of parameter, Java will know how to resolve this boolean collision(missile m) { if ((m.xpos >= xpos - 26 && m.xpos <= xpos + 22) && (m.ypos >= ypos - 24 && m.ypos <= ypos + 26)) { return true; else { return false; Let's put these methods into Processing (remember to update the old collision detection code in the draw() method) if(!a[i].collision(r1)) { a[i].drawme();

Destroying asteroids 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) { asteroids.remove(this); if (large) { asteroids.add(new Asteroid(false, xpos, ypos, lastdrawmillis)); asteroids.add(new Asteroid(false, xpos, ypos, lastdrawmillis)); Hmm we've added some new things here: We need to store the size of the Asteroid (is it large or small?) We need to be able to create new smaller Asteroids at a specified position

Asteroid size Since we only want two sizes (big and small) we can use a boolean: boolean large; We also need to put something in the Asteroid's drawme() method to make sure that it will get drawn at the right size Let's make small Asteroids at half-scale if (!large) { scale(0.5); Now let's think about how to create the new smaller Asteroids at a given (x,y) position

super and this this is a special variable keyword 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) this.somevariable refers to the class variable (useful if a variable of local scope has the same name as some class variable) 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

A new Asteroid constructor Asteroid(boolean islarge, float initialx, float initialy, long previousdrawtime) { // set the initial x and y pos xpos = initialx; ypos = initialy; // get a random orientation for this asteroid rotation = random(0, TWO_PI); // get the asteroid's velocity in x and y directions // based on its orientation and size (bigger moves slower) if (islarge) { velocityx = sin(rotation)*10; velocityy = -cos(rotation)*10; else { velocityx = sin(rotation)*25; velocityy = -cos(rotation)*25; large = islarge; lastdrawmillis = previousdrawtime;

But what about the old constructor? We still want to be able to create an Asteroid in a random location So we want to keep our original constructor without parameters, but we need to update it to take into account the asteroid size (they start big) We can just use this to call our new constructor and let it do all the work Asteroid() { // create an asteroid at a random location on the display this (true, random(0, XSIZE), random(0, YSIZE), 0); Let's assemble all these Asteroid changes together and put them in Processing

Storing our size boolean large; The size variable Drawing at the correct size // after translating and rotating, we should // make sure to scale ourselves too if (!large) { scale(0.5);

The constructors Asteroid() { this (true, random(0, XSIZE), random(0, YSIZE), 0); Asteroid(boolean islarge, float initialx, float initialy, long previousdrawtime) { xpos = initialx; ypos = initialy; rotation = random(0, TWO_PI); if (islarge) { velocityx = sin(rotation)*10; velocityy = -cos(rotation)*10; else { velocityx = sin(rotation)*25; velocityy = -cos(rotation)*25; large = islarge; lastdrawmillis = previousdrawtime;

The destroy method void destroy(arraylist asteroids) { asteroids.remove(this); if (large) { asteroids.add(new Asteroid(false, xpos, ypos, lastdrawmillis)); asteroids.add(new Asteroid(false, xpos, ypos, lastdrawmillis));

Transition to ArrayList Now that we've created a destroy method to split large Asteroids into two smaller ones, we want to move to our ArrayList data structure Remember to remove the previous fixed-size Asteroid array! We've already declared and initialized the ArrayList, so next we need to draw it: for(int i = 0; i < asteroids.size(); i++) { Asteroid asteroid = (Asteroid)asteroids.get(i); if (!asteroid.collision(r1)) { asteroid.drawme();

Last but not least: calling destroy! Currently we check the Missile array to draw visible missiles, but now we also want to check if a Missile has collided with an Asteroid If it has, we should destroy the Asteroid and remove the Missile for(int i = 0; i < MAX_MISSILES; i++) { if (m[i]!= null) { m[i].drawme(); for (int j = 0; j < asteroids.size(); j++) { Asteroid asteroid = (Asteroid)asteroids.get(j); if (asteroid.collision(m[i])) { asteroid.destroy(asteroids); m[i] = null; break; if (m[i]!= null &&!m[i].isvisible()) { m[i] = null;

Summary Learned how to use ArrayList, our first Java Platform class Learned about super and subclasses as types Any instance of a subclass is an instance of the superclass, but not viceversa Can cast more abstract classes (parents) into more concrete classes (children) 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)

Remember For Thursday this week: Theory Readings Two presenters (you know who you are!) Everyone else: prepare one discussion question for each reading Happenings in the New York Scene - Kaprow, NMR pp. 83-86 The Cut-Up Method of Brion Gysin - Burroughs, NMR pp. 89-91 Six Selections by the Oulipo - NMR pp. 147-189 Finally today: free time to work on your drawing tools!