Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own.

Slides:



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

Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
More about classes and objects Classes in Visual Basic.NET.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
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,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Computer Science I Inheritance Professor Evan Korth New York University.
Programming Games Computer science big ideas. Computer Science jargon. Show virtual dog Homework: [Catch up: dice game, credit card or other form.] Plan.
Inheritance using Java
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Programming Games Simulated ballistic motion: cannon ball. Classwork: Final day for midterm project and all projects for first part of class. Homework:
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CSC 142 Computer Science II Zhen Jiang West Chester University
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
Object Oriented Software Development
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Objects. The Heart of the Matter - The "Black Box" Object-oriented software is all about objects. An object is a "black box" which receives and sends.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Test 2 Review. General Info. All tests are comprehensive. You are still responsible for the material covered prior to the first exam. You will be tested.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Computer Science I Programming in Java (programming using Processing IN Java, using IntelliJ IDE) Classwork/Homework: copy your Processing projects over.
Computer Science I Arrays. Parallel structures. Classwork/Homework: Build your own bouncing things.
Computer Science I Recap: variables & functions. Images. Pseudo-random processing.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Coming up: Inheritance
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Computer Science I Variables. Methods. Classwork/Homework: Re-do or do new drawing using variables & function(s). Zip folder and upload to Moodle dropbox.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
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.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
OOP Basics Classes & Methods (c) IDMS/SQL News
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
Computer Science I More class examples. Paths. Jigsaw. Tolerance. Classwork/homework: Your class project. Post proposal for midterm project.
Time to upload Virtual something.
Computer Science I Libraries. Sound. Saving frames as images. Classwork/Homework: Make sketch using sound. Add saving frames.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Classwork: Examine and enhance falling drop(s) or make your own.
Computer Science I Variables. Methods.
Java Primer 1: Types, Classes and Operators
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance, Polymorphism, and Interfaces. Oh My
Comp 249 Programming Methodology
Inheritance, Polymorphism, and Interfaces. Oh My
Polymorphism.
Programming games Classes and objects (used for Jigsaw, Bouncing stuff, other projects) Homework: Complete cannonball. Video or Audio. Your own project.
Inheritance Inheritance is a fundamental Object Oriented concept
Recitation 7 October 7, 2011.
Presentation transcript:

Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own.

Object Oriented Programs OOPs Classes are another way to extend the language. A class defines objects. Objects have variables and functions using those variables. The functions are called methods. OOPs provides inheritance, a mechanism for re-using code for things that are similar, but not identical.

Go immediately to example Re-do bouncing balls using a Ball class to hold the information. Advance to bouncing things by defining a Thing class, and then a Ball class and a Box class that inherit from the Thing class. Advance further by adding a Picture class.

Planning What do we want to do? – Create a ball with its information – Move a ball, making use of information – Show a ball, making use of information Note: for first version, will combine moving and showing. Will change for subsequence versions. Set up an array to hold all the balls.

Show code out of order, incomplete class Ball { float bx; //instance variables float by; float dx; float dy; int balldiam; Ball ( ) { //called the constructor … } void moveAndShow() { //method }

Plugs into previous class definition Ball (float x, float y, float vx, float vy, int diam) { bx = x; by = y; dx = vx; dy = vy; balldiam = diam; } void moveAndShow() { ellipse(bx,by,balldiam,balldiam); bx = bx + dx; by = by + dy; if ((bx>=width) || (bx<=0)) {dx = -dx;} if ((by>=height) || (by<=0)) {dy = -dy;} }

Declaring array of objects Ball[] balls = {}; This declares balls to be an array of Ball objects AND initializes it to be empty.

setup Create (construct) and add elements to the balls array. Makes use of append function. Since append is a general function for arrays of anything, needs what is called casting. void setup() { balls = (Ball[]) append(balls,new Ball(width/2,width/2,3,3,20)); balls = (Ball[]) append(balls,new Ball(width/4,width/3,2,1,40)); balls = (Ball[]) append(balls,new Ball(width*.75,width/6,1,2,60)); size (900,600); }

draw …. Is really simple void draw(){ background(0); //erase window for (int i=0;i<balls.length;i++) { balls[i].moveAndShow(); }

Aside By the way… setup, draw, mousePressed, and various other functions are actually methods of the Java PApplet class. The Processing programmer defines them as part of the sketch, which is, in face, part of a definition of a subclass. – MORE on this later.

Add rectangles Use the word Box to avoid conflicts. The coding for a circle and a rectangle are similar. So….define a class named Thing and two subclasses: Ball and Box. – and soon Picture. Replace moveAndShow with a move method and a show method. – The show methods are different. The critical code is class Ball extends Thing and super( ); // to call the constructor for the parent class

class Thing { float bx; float by; float dx; float dy; int wdiam; int hdiam; Thing (float x, float y, float vx, float vy, int w, int h) { bx = x; by = y; dx = vx; dy = vy; wdiam = w; hdiam = h; }

void move() { bx = bx + dx; by = by + dy; if ((bx>=width) || (bx<=0)) {dx = -dx;} if ((by>=height) || (by<=0)) {dy = -dy;} } void show() { // will be overridden by // inherited objects }

class Ball extends Thing { // no new variables Ball (float x, float y, float vx, float vy, int diam) { super(x,y,vx,vy,diam,diam); //wdiam and hdiam same } void show(){ ellipse(bx,by,wdiam,hdiam); } class Box extends Thing { // no new variables Box (float x, float y, float vx, float vy, int wdiam, int hdiam) { super(x,y,vx,vy,wdiam,hdiam); } void show(){ rect(bx,by,wdiam,hdiam); }

The draw function Invokes whatever the appropriate method is. void draw(){ background(0); //erase window for (int i=0;i<things.length;i++) { things[i].show(); things[i].move(); }

Add images? Just add an new subclass. Still need to add the image file to the Processing folder. class Picture extends Thing { PImage pic; Picture (float x, float y, float vx, float vy, int w, int h, String imagefilename) { super(x,y,vx,vy,w,h); pic = loadImage(imagefilename); } void show() { image(pic,bx,by,wdiam,hdiam); }

Semantic errors vs syntactic errors A syntactic error is an error Processing can catch. – Similar to grammatical error. A semantic error is an error of meaning. – I meant to add 10 instead of subtracting 10. – Timing wrong. – ?? – No help from Processing.

Reflection Use of classes pushes more into syntactic realm. – Use of wrong datatypes, wrong number of parameters, trying to use method that doesn't exist, etc. Processing catches errors sooner! Use of variables, functions, and classes means more over all planning.

Classwork / Homework Study examples. Make enhancements. Make your own project using classes.