Sep 26, Fall 2006IAT 800 Lecture 6 Methods and Classes.

Slides:



Advertisements
Similar presentations
1 Inheritance Classes and Subclasses Or Extending a Class.
Advertisements

Introduction to Java 2 Programming
Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming.
Abstract Classes We often define classes as subclasses of other classes – First, define the superclass – In some cases, a superclass may not have enough.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Data: Programming Design and Modularization IS 101Y/CMSC 101 Computational Thinking and Design Thursday, September 26, 2013 Marie desJardins University.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
IAT 334 Lab 2 Computer Graphics: Rocket, PImage. June 4, 2010IAT 3342 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
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.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Oct 17, Fall 2006IAT 800 ArrayList, Text, and more Image Manipulation.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
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.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Multiple Choice Solutions True/False a c b e d   T F.
Classes / Objects An introduction to object-oriented programming.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Continuous February 16, Test Review What expression represents the zip car eligibility rules of at least 18 years old and no incidents?
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
Computation as an Expressive Medium Lab 3: (Methods and) Classes Jason Alderman.
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Assignment 6.
July 28, 2015IAT 2651 Design Patterns. “Gang of Four” July 28, 2015IAT 2652.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
LCC 6310 Computation as an Expressive Medium Lecture 6.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Functional Abstraction Reduces Complexity.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Welcome back!. Object Oriented Programming – Encapsulation Classes encapsulate state (fields) and behavior (methods) – Polymorphism Signature Polymorphism.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
Overview of C++ Polymorphism
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
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.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
IAT 265 Lecture 7 Objects and ArrayList. May 19, 2016IAT 2652 Outline  Programming concepts –Super and this –Java SDK classes Lists Reading the Java.
Computer Graphics: Rocket, Java: Class
LCC 6310 Computation as an Expressive Medium
LCC 6310 Computation as an Expressive Medium
Trainings 11/4 Intro to Java.
Object Oriented Programming (OOP) LAB # 8
Week 6 Object-Oriented Programming (2): Polymorphism
Chapter 8 Objects.
Overview of C++ Polymorphism
Java Programming Language
An Example of Inheritance
Lecture 6 Methods and Classes
Lecture 6 Methods, Classes, Inheritance
Presentation transcript:

Sep 26, Fall 2006IAT 800 Lecture 6 Methods and Classes

Sep 26, Fall 2006IAT 800 Today  A few good programming practices  Methods  Classes –Rockets, asteroids –Subclasses –Methods that return values –BImage

Sep 26, Fall 2006IAT 800 Good Practices  Comments are your friend! –Put a block of commenting at the top of each program that explains what it does –Comment in code what a section will do –Comment when you appropriate code!!  Auto-format is also your friend!!  Your programs will have at least three sections Variable declaration setup() draw()

Sep 26, Fall 2006IAT 800 Methods  Methods, also known as –Functions (methods which return something) –Procedures (methods which don’t return stuff)  Method declaration  Method call void vendingMachine( int coinCents ){ println("You inserted "+coinCents+" cents."); } int quarter = 25; vendingMachine(quarter);

Sep 26, Fall 2006IAT 800 Methods  Method declaration The method declaration is like a blueprint Doesn't matter if declaration is before or after call! Parameter name (e.g. coinCents) is just the name given to data passed into the method… void vendingMachine( int coinCents ){ println("You inserted "+coinCents+" cents."); } void means doesn't return anything parameter

Sep 26, Fall 2006IAT 800 Methods  Method call vendingMachine(quarter) == vendingMachine(25) Method call must occur in either setup(), draw(), or another method… You can call it as many times as you like! –vendingMachine()s for everyone!! int quarter = 25; vendingMachine(quarter); argument

Sep 26, Fall 2006IAT 800 Classes  Types –Primitives: int, float, char, boolean … –Objects: array, string, class …

Sep 26, Fall 2006IAT 800 Objects  We’ve worked with some objects before, like Arrays.  We can make our own objects, to keep related data together, and methods to control that data.

Sep 26, Fall 2006IAT 800 Classes  Classes are the blueprints for our new objects.  To declare a new Class (a new type of object): class MyToy { // fields (class variables) // methods (class functions) }

Sep 26, Fall 2006IAT 800 Fields and Methods class MySquare { int xPos, yPos; MySquare(x, y) { xPos = x; yPos = y; } void drawMe() { rect(xPos, yPos, 50, 50); } xy drawMe() fields constructor methods (one kind of method)

Sep 26, Fall 2006IAT 800 Fields and Methods class MySquare { int xPos, yPos; MySquare(x, y) { xPos = x; yPos = y; } void drawMe() { rect(xPos, yPos, 50, 50); } xy drawMe() MySquare square1 = new MySquare(10, 10); MySquare square2 = new MySquare(20, 90); 10 drawMe() 2090 drawMe() square1square2

Sep 26, Fall 2006IAT 800 Fields and Methods class MySquare { int xPos, yPos; MySquare(int x, int y) { xPos = x; yPos = y; } void drawMe() { rect(xPos, yPos, 50, 50); } MySquare square1 = new MySquare(10, 10); MySquare square2 = new MySquare(20, 90); xy drawMe() 10 drawMe() 2090 drawMe() square1 square2 square1.drawMe(); square2.drawMe();

Sep 26, Fall 2006IAT 800 Arrays of Objects?  Let’s make a bunch of squares! MySquare[] squares = new MySquare [10] ; // initialize all of our squares. for (int i = 0; i < 10; i ++) { squares[i] = new MySquare(i*10, i*10); } squares[4].drawMe(); // draw the 4 th square.

Sep 26, Fall 2006IAT 800 Asteroids  Let’s adapt this to make an array of Asteroids for our rocket from lecture. class Asteroid { //fields float rotation = 0; float xPos, yPos; float velocityX, velocityY; long lastDrawMillis = 0; … }

Sep 26, Fall 2006IAT 800 Asteroids  When we create an asteroid, let’s have it start in a random position, and move in a random direction. Class Asteroid { … // constructor Asteroid() { xPos = random(0, 400); yPos = random(0, 400); rotation = random(0, TWO_PI); velocityX = sin(rotation)*10; velocityY = -cos(rotation)*10; }

Sep 26, Fall 2006IAT 800 Asteroids Class Asteroid { … // draw method void drawMe() {

Sep 26, Fall 2006IAT 800 Revisit our example  So far we have a rocket that flies around in a field of asteroids  What if we want our rocket to be able to fire –But we don’t want to get rid of our non-firing rocket  Create a subclass!

Sep 26, Fall 2006IAT 800 Inheritance  Subclasses inherit fields and methods from parent Class ArmedRocket extends Rocket { … }

Sep 26, Fall 2006IAT 800 Our subclass needs a constructor  Our empty ArmedRocket example creates an error –Processing doesn’t know how to construct an ArmedRocket  We want the ArmedRocket constructor to do the same work as the Rocket constructor ArmedRocket(int initialX, int initialY, float initialRot) { super(initialX, initialY, initialRot); } The keyword super means to call the method in the parent with the same name

Sep 26, Fall 2006IAT 800 Now we have ArmedRocket  We can use an ArmedRocket now in our example  But, it’s basically just a copy of Rocket  The only reason to define an ArmedRocket is to add new capabilities or to override old ones

Sep 26, Fall 2006IAT 800 Add a fire() method  We want our fire method to draw a missile that shoots out of the rocket  We could have the fire method draw the missile… –Is there a problem with this?

Sep 26, Fall 2006IAT 800 Missiles should also be objects  The object oriented solution is to make the missile an object as well –All the different types of “things” in our domain should have a corresponding class  Like asteroids and rockets, the missile class should know how to draw itself –A Missile is similar to a rocket (position, rotation, draw method, etc.)  Now our ArmedRocket.fire() method can just create and return a missile

Sep 26, Fall 2006IAT 800 The fire() method Missile fire() { Missile m = new Missile(xPos, yPos, rotation); return m; }  Now add code in loop to draw missiles

Sep 26, Fall 2006IAT 800 Using the keyPressed() method  keyPressed() is a built in Processing method that is called when a key is pressed  Useful because you can tie events to keystrokes instead of to loop() –If we fire missiles in loop(), notice that we fire many times even if we push the key quickly –This is because loop() is called many times a second