Chapter 8. Classes and Objects

Similar documents
Chapter 5. Condi.onals

Name CMPS 5J Final March 17, 2010 This is a closed notes, closed book exam. Each problem is worth 1 point unless indicated otherwise.

Final Exam Winter 2013

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

Name CMPS 5J Final March 17, 2009 This is a closed notes, closed book exam.

5.1. Examples: Going beyond Sequence

Bits and Bytes. How do computers compute?

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

Chapter 8. More with classes Prac2ce with Zoog

Arrays. Array an ordered collec3on of similar items. An array can be thought of as a type of container.

Classes and objects. Chapter 2: Head First Java: 2 nd Edi4on, K. Sierra, B. Bates

What is a variable? A named locajon in the computer s memory. A variable stores values

A Founda4on for Programming

CMPT-166: Sample Final Exam Answer Key

What can we do with Processing? Let s check. Natural Language and Dialogue Systems Lab Guest Image. Remember how colors work.

Variables and Control Structures. CS 110 Eric Eaton

if / if else statements

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1

CSc 110, Spring Lecture 7: Graphics, return values and math

In this chapter: What is an array? Declaring an array. Initialization. Array operations using the for loop with an array. Arrays of objects.

Interaction Design A.A. 2017/2018

If the ball goes off either the right or left edge, turn the ball around. If x is greater than width or if x is less than zero, reverse speed.

We will start our journey into Processing with creating static images using commands available in Processing:

CSc 110, Spring Lecture 11: return values and math

Date Course Name Instructor Name Student(s) Name WHERE WILL IT LAND?

A formal design process, part 2

Building Java Programs

Evaluating Logical Expressions

Building Java Programs

Iteration in Programming

CPSC Fall L01 Final Exam

Class CSE F. *slides are from CSE S at SKKU & at MIT

CISC 1600, Lab 2.3: Processing animation, objects, and arrays

Basic Computer Programming (Processing)

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

+ Questions about Assignment 5?

CSc 110, Spring Lecture 11: if / else. Adapted from slides by Marty Stepp and Stuart Reges

CS116X Midterm Review Winter 2015

Recall that creating or declaring a variable can be done as follows:

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

CMSC427 Interac/ve programs in Processing: Polyline editor

CS 106 Winter Lab 05: User Interfaces

Fundamentals of Programming Data Types & Methods

append() function, 66 appending, 65, 97, 296 applications (apps; programs), defined, 2, 296

Exploring Projectile Motion with Interactive Physics

COMP 102: Test August, 2017

Classes & Objects CMSC 202

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

TOPIC 12 CREATING CLASSES PART 1

CS 106 Winter Lab 03: Input and Output

Introduction to Computer Programming for Non-Majors

COMP 102: Test. 2017, May 15 ** WITH SOLUTIONS **

GameSalad Creator Interface

COMP 102 : Test. 2017, Apr 3

Processing/Java Syntax. Classes, objects, arrays, and ArrayLists

In Java there are three types of data values:

An online version of these worksheets is available at:

CSc 110, Autumn Lecture 10: return values and math

Variables One More (but not the last) Time with feeling

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

Review. Custom Objects. Comparing Declarations and Initializers Built PopGame. Classes Fields and Methods Instantiation using the "new" keyword

Object-Oriented Programming in Processing

ITI 1120 Lab #9. Slides by: Diana Inkpen, Alan Williams, Daniel Amyot Some original material by Romelia Plesa

Model Solutions. COMP 102: Test May, 2015

Principles of So3ware Construc9on. A formal design process, part 2

Lecture 06: Classes and Objects

Constants are named in ALL_CAPS, using upper case letters and underscores in their names.

EECS 1001 and EECS 1030M, lab 01 conflict

Using Methods. Methods that handle events. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

EP486 Microcontroller Applications

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

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

Variables, Types, and Expressions

COMP 102: Test August, 2017

Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2

Condi(onals and Loops

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

2. [20] Suppose we start declaring a Rectangle class as follows:

Methods (part b) Alice In Ac5on, Ch 2

Lab Midterms Not Graded Yet

The following is a typical execution run of this program:

CS 106A, Lecture 16 Arrays

LCC 6310 The Computer as an Expressive Medium. Lecture 9

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

제 16 장 SFML 로작성해보는 프로젝트 II 1. 상속을사용하여게임에필요한클래스를설계할수있다.

Math Learning Center Boise State 2010, Quadratic Modeling STEM 10

Notes from the Boards Set # 5 Page

CSE 1223: Introduction to Computer Programming in Java Chapter 6 Arrays

Ge#ng Started with L Edit

Refactoring. So#ware Quality Quality Audit and Cer4fica4on. Master in Computer Engineering. Roberto García

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Classes and Objects. COMP1400/INFS1609 Week 8. Monday, 10 September 12

Programming Languages and Techniques (CIS120)

Miscellaneous Stuff That Might Be Important.

Basic SFML Moving a Sprite

1. Complete these exercises to practice creating user functions in small sketches.

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Transcription:

Chapter 8 Classes and Objects

Data Abstrac4on In Java there are three types of data values: primi4ve data values (int, float, boolean, etc.) arrays (actually a special type of object) objects Objects in a program are used to represent "real" (and some4mes not so real) objects from the world around us.

Objects An object might represent a string of characters, a planet, a type of food, a student, an employee, a piece of email, a ball, a car, a font, an image,... anything that can't be (easily) represented by a primi4ve value or an array. Just as 3 is a primi4ve value of type int, every object must also have a type. These types are called classes.

Classes A class describes a set of objects. It specifies what informa4on will be used to represent an object from the set (e.g. name and salary for an employee, posi4on, velocity, and size for a ball). It also specifies what opera4ons can be performed on such an object (get the name of the student, send an email message, move the ball).

Working with Objects Use new to make a new object e.g. Ball ball1 = new Ball(x,y,xv,yv,dia); Car car1 = new Car(top,left); String name = Dustin ; Operate on them with dot nota4on. ball1.update(); car1.move(); String fullname = name.append( Adams );

Which does not belong in this list considering what was on the last slide? A. int B. String C. Car D. name E. Ball Use new to make a new object e.g. Ball ball1 = new Ball(x,y,xv,yv,dia); Car car1 = new Car(top,left); String name = Dustin ; Operate on them with dot nota4on. ball1.update(); car1.move(); String fullname = name.append( Adams );

// our old ball example from before float velocity = 0; float ypos = 0; int ballradius = 10; float gravity = 0.1; void setup() { size(400,400); fill(0); // ball will be black

void draw() { background(255); // update the ball position and velocity if (ypos > height-ballradius) { velocity = -velocity; ypos = ypos + velocity; velocity = velocity + gravity; velocity = velocity*0.99; // add some drag // draw the ball ellipse(width/2, ypos, ballradius*2, ballradius*2); // What would it take to add a second ball?

Ball ball1; float gravity = 0.1; float drag = 0.99; void setup() { size(400, 400); ball1 = new Ball(width/4, 0, 0, 0, 10); fill(255,0,0); void draw() { background(255); ball1.update();

Ball ball1, ball2; float gravity = 0.1; float drag = 0.99; void setup() { size(400, 400); ball1 = new Ball(width/4, 0, 0, 0, 10); ball2 = new Ball(3*width/4, 0, 0.5, 0.3, 20); fill(255,0,0); void draw() { background(255); ball1.update(); ball2.update();

class Ball { float x, y, xvel, yvel; int diam; void update() {......

... void update() { // if hit the ground so need to reverse the velocity if (y > height-diam/2) { yvel = -yvel; // adjust position based on velocity y = y + yvel; x = x + xvel; // adjust the velocity - increasing due to gravity yvel = yvel + gravity; // always accelerate down // add some drag yvel = yvel*drag; // draw the ball ellipse(x, y, diam, diam);

class Ball { float x, y, xvel, yvel; int diam; void update() {... Ball(float xstart, float ystart, float xv, float yv, int diameter) { x = xstart; y = ystart; xvel = xv; yvel = yv; diam = diameter;

Given the Ball code just presented, which of these statements is not legal? A. ball = new Ball(10,20,0,0,0); B. ball = new Ball(0,0,0,0,0); C. ball = new Ball(); D. ball.update(); E. ball.diam = 200; class Ball { float x, y, xvel, yvel; int diam; void update() {... Ball(float xstart, float ystart, float xv, float yv, int diameter) { x = xstart; y = ystart; xvel = xv; yvel = yv; diam = diameter;

Elements of a Simple Class A class describes the data values used to represent an object and any opera4ons that can be performed on that object. The data values are stored in instance variables, also known as fields, or data members. The opera4ons are described by instance methods, some4mes called procedure members.

How many opera4ons were defined for the Ball class? Do not count crea4ng the Ball as an opera4on on a Ball. A. 0 B. 1 C. 2 class Ball { float x, y, xvel, yvel; int diam; void update() {... Ball(float xstart, float ystart, float xv, float yv, int diameter) { x = xstart; y = ystart; xvel = xv; yvel = yv; diam = diameter;

Making Your Own Classes class Ball { float x, y, xvel, yvel; int diam;

// redone with a simple ball class - no methods Ball ball1; float gravity = 0.1; void setup() { size(400, 400); ball1 = new Ball(); ball1.x = width/4; ball1.y = 0; ball1.diam = 10; ball1.xvel = 0; ball1.yvel = 0; fill(255,0,0);

void draw() { background(255); // update the ball position and velocity if (ball1.y > height-ball1.diam/2) { ball1.yvel = -ball1.yvel; ball1.y = ball1.y + ball1.yvel; ball1.yvel = ball1.yvel + gravity; ball1.yvel = ball1.yvel*0.99; // add some drag // draw the ball ellipse(ball1.x, ball1.y, ball1.diam, ball1.diam);

What is needed to add another ball?

Modify the Ball class to allow for specifying the ball color. Add a method so that the Ball color can be changed repeatedly a\er crea4on/ini4aliza4on. Fix update to update both x and y posi4ons and veloci4es. Add a method that takes a Ball as a parameter and returns true if the other ball has bumped into this ball. What other opera4ons might you add to the Ball class?

Elements of a Simple Class A class describes the data values used to represent an object and any opera4ons that can be performed on that object. The data values are stored in instance variables, also known as fields, or data members. The opera4ons are described by instance methods, some4mes called procedure members.

class Ball { float x, y, xvel, yvel; int diam; void update() {... Ball(float xstart, float ystart) { x = xstart; y = ystart; xvel = 0; yvel = 0; diam = 20;

class Ball { float x, y, xvel, yvel; int diam; void update() {... Ball(float xstart, float ystart, float xv, float yv, {... int diameter) boolean collidedwith(ball other) { void reversex() { x = -x;

class Ball { float x, y, xvel, yvel; int diam; color bcolor; void update() {... // draw the ball fill(bcolor); ellipse(x, y, diam, diam); Are the boldface addi4ons sufficient to assign different colors to balls? A. Yes B. No - there is a missing line. C. No - there is a syntax error. D. No - there are 2 missing lines. Ball(float xstart, float ystart, float xv, float yv, int diameter, color bcolor) { x = xstart; y = ystart; xvel = xv; yvel = yv; diam = diameter;

// We can have more than one Constructor class Ball { float x, y, xvel, yvel; int diam; void update() {... Ball() {... Ball(float xstart, float ystart) {... Ball(float xstart, float ystart, float xv, float yv, int diameter) {...

//Create a class to represent a light bulb that can be // turned on and off. Light light1, light2, light3; void setup() { size(400, 400); light1 = new Light(25, 25); light2 = new Light(width-30, height-60, false); light3 = new Light(width/2, height/2, 50);//specify size void draw() { light1.show(); light2.show(); light3.show(); //What s the minimum number of instance variables the Light class can have? A. 2 B. 3 C. 4 D. 5 E. 6

void mousepressed() { if (light1.clicked()) { light1.toggle(); if (light2.clicked()) { light2.toggle(); if (light3.clicked()) { light3.toggle(); //Create a class to represent a light bulb that can be // turned on and off. Light light1, light2, light3; void setup() { size(400, 400); light1 = new Light(25, 25); light2 = new Light(width-30, height-60, false); light3 = new Light(width/2, height/2, 50);//specify size void draw() { light1.show(); light2.show(); light3.show(); // How many methods plus constructors must the Light class have? A. 2 B. 3 C. 4 D. 5 E. 6