Scope. Dragging Algorithm Revisited. Tuesday, October 28, October 28, 2008

Similar documents
Dragging. Dragging Rectangles. Tuesday, October 21, October 21, User interface effect that we want:

Classes. Integer Division. Thursday, October 23, March 13, 2008

Java Classes (Java: An Eventful Approach, Ch. 6),

Topic Notes: Java and Objectdraw Basics

CS Problem Solving and Object-Oriented Programming

CS Programming Exercise:

Programming Exercise. Scribbler

CS 134 Programming Exercise 7:

Lecture 6 CSE 11 Fall 2013

Programming Assignment 3 ( 100 Points ) START EARLY!

CS 134 Programming Exercise 3:

CS 134 Programming Exercise 2:

Lecture 15 CSE11 Fall 2013 Arrays

Lecture 10 Declarations and Scope

Programming Assignment 4 ( 100 Points )

To gain experience using recursion and recursive data structures.

CSE 11 Midterm Fall 2008

Programming Project 1

Lecture 9 CSE11 Fall 2013 Active Objects

Chapter 6 Introduction to Defining Classes

CS 134 Programming Exercise 1:

public class TicketMachine Inner part omitted. public class ClassName Fields Constructors Methods

1. Write code to answer each of the following questions. For this question, it is not necessary for you to define constants or write comments.

Programming Exercise

CS 134 Programming Exercise 9:

Design to interfaces. Favor composition over inheritance Find what varies and encapsulate it

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Collections

Chapter 4 Defining Classes I

Programming Assignment 2 (PA2) - DraggingEmoji & ShortLongWords

CS 134 Midterm Fall 2012

CSE11 Lecture 20 Fall 2013 Recursion

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Fundamental Concepts and Definitions

Understanding class definitions

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Introduction to Design Patterns

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

Assertions, pre/postconditions

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

(a) Assume that in a certain country, tax is payable at the following rates:

Grace Documentation: Using GUI Components in the objectdraw dialect in Grace

Array. Prepared By - Rifat Shahriyar

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Today s Agenda. Quick Review

COMP : Practical 8 ActionScript II: The If statement and Variables

CS 51 Laboratory # 12

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

METHODS. CS302 Introduction to Programming University of Wisconsin Madison Lecture 15. By Matthew Bernstein

Aggregation. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

1 Lexical Considerations

Polymorphism Part 1 1

CS11 Intro C++ Spring 2018 Lecture 3

Java Object Oriented Design. CSC207 Fall 2014

Introduction to Programming Using Java (98-388)

CISC 1600, Lab 3.1: Processing

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Write a program that draws this image. The ligle squares are 5x5. The screen is the default 100x100. It doesn t change.

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

CISC 1600, Lab 2.1: Processing

APCS Semester #1 Final Exam Practice Problems

EECS168 Exam 3 Review

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

CSE 401 Final Exam. December 16, 2010

Midterm 2. 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer?

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

QUIZ Friends class Y;

Declarations and Access Control SCJP tips

CMPT-166: Sample Final Exam Answer Key

Lecture 7 Objects and Classes

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling)

Short Notes of CS201

CS2102, B15 Exam 2. Name:

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

CSE 113 A. Announcements - Lab

Scope. Scope is such an important thing that we ll review what we know about scope now:

Lecture 8 CSE11 Fall 2013 While Loops, Switch Statement, Simplifying Conditionals

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Industrial Programming

What is an Object. Industrial Programming. What is a Class (cont'd) What is a Class. Lecture 4: C# Objects & Classes

CS201 - Introduction to Programming Glossary By

THE UNIVERSITY OF WESTERN AUSTRALIA. School of Computer Science & Software Engineering CITS1001 OBJECT-ORIENTED PROGRAMMING AND SOFTWARE ENGINEERING

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

Lecture 06: Classes and Objects

X Language Definition

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Object Oriented Programming in C#

Decision Structures. Chapter 4

POLYMORPHISM Polymorphism: the type Polymorphism taking many shapes type of object

Object-Oriented Programming in Processing

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

CS11 Introduction to C++ Fall Lecture 7

CS 162, Lecture 25: Exam II Review. 30 May 2018

Transcription:

Scope October 28, 2008 1 private boolean rectgrabbed = false; private Location lastpoint; // Last mouse location public void onmousepress (Location point) { if (rect.contains (point)) { rectgrabbed = true; lastpoint = point; public void onmousedrag (Location point) { if (rectgrabbed) { // How far did mouse move? double dx = point.getx() - lastpoint.getx(); double dy = point.gety() - lastpoint.gety(); // Move the rectangle that distance rect.move (dx, dy); lastpoint = point; public void onmouserelease (Location point) { // Drop the rectangle rectgrabbed = false; Dragging Algorithm Revisited 2

private boolean traingrabbed = false; private Location lastpoint; // Last mouse location public void onmousepress (Location point) { if (train.contains (point)) { traingrabbed = true; lastpoint = point; public void onmousedrag (Location point) { if (traingrabbed) { // How far did mouse move? double dx = point.getx() - lastpoint.getx(); double dy = point.gety() - lastpoint.gety(); // Move the object that distance train.move (dx, dy); lastpoint = point; public void onmouserelease (Location point) { // Drop the object traingrabbed = false; Dragging a train 3 Defining a Class What do we want objects created from that class to do? Example: A train is a collection of shapes consisting of a car, wheels and a smokestack. A train can move on the track. The train can produce a puff of smoke. 4

Defining a Class What do we want objects created from that class to do? Data Example: A train is a collection of shapes consisting of a car, wheels and a smokestack. A train can move on the track. The train can produce a puff of smoke. 5 Defining a Class What do we want objects created from that class to do? Example: A train is a collection of shapes consisting of a car, wheels and a smokestack. A train can move on the track. The train can produce a puff of smoke. Methods 6

public class Train { private FilledRect car; Data private FilledOval rearwheel; private FilledOval frontwheel; private FramedRect smokestack; The Train Class public void move () { Methods public boolean contains (Location point) { public FilledOval producesmoke () { (almost) 7 Constructing a Train Need a way to say give me a new train We ve written code that creates things: new FramedOval (10, 10, 50, 50, canvas); You created a constructor for your penguin in the last lab: public Penguin (DrawingCanvas penguincanvas) { 8

public class Train { private FilledRect car; private FilledOval rearwheel; private FilledOval frontwheel; private FramedRect smokestack; public Train () { public void move () { public boolean contains (Location point) { public FilledOval producesmoke () { Constructor The Train Class 9 Constructing a Train Initialize the instance variables Get it on the display public class Train { private FilledRect car; private FilledOval rearwheel; private FilledOval frontwheel; private FramedRect smokestack; public Train () { car = new FilledRect (); rearwheel = new FilledOval (); frontwheel = new FilledOval (); smokestack = new FramedRect (); 10

public class Train { private static final int CAR_WIDTH = ; private static final int CAR_HEIGHT = ; private static final int WHEEL_DIAMETER = ; private static final int SMOKESTACK_HEIGHT = ; private static final int SMOKESTACK_WIDTH = ; Train Size Let train determine its own size Define constants within Train class public Train () { car = new FilledRect (, CAR_WIDTH, CAR_HEIGHT, ); rearwheel = new FilledOval (, WHEEL_DIAMETER, WHEEL_DIAMETER, ); frontwheel = new FilledOval (, WHEEL_DIAMETER, WHEEL_DIAMETER, ); smokestack = new FilledOval ( SMOKESTACK_WIDTH, SMOKESTACK_HEIGHT, ); 11 Train Location Suppose we want to let the method that calls new Train decide where to place the train Add parameters to the constructor public Train(double left, double top, DrawingCanvas traincanvas) { car = new FilledRect (left, top, CAR_WIDTH, CAR_HEIGHT, traincanvas); rearwheel = new FilledOval (left +, top +, WHEEL_DIAMETER, WHEEL_DIAMETER, traincanvas); frontwheel = new FilledOval (left +, top +, WHEEL_DIAMETER, WHEEL_DIAMETER, traincanvas); smokestack = new FilledOval (left +, top -, SMOKESTACK_WIDTH, SMOKESTACK_HEIGHT, traincanvas); 12

Moving a train How do we move it? How do we know where to move it to? public void move () { 13 Moving a train How do we move it? Move its pieces Technique is called delegation public void move () { car.move (); rearwheel.move (); frontwheel.move (); smokestack.move (); 14

Moving a train How do we know where to move it to? Model after things like FramedOval Let the caller tell the train how far to move Add parameters to the move method All the parts move the same amount. public void move (double dx, double dy) { car.move (dx, dy); rearwheel.move (dx, dy); frontwheel.move (dx, dy); smokestack.move (dx, dy); 15 public class Train { The Train Class public Train (int left, int top, DrawingCanvas traincanvas) { public void move (int dx, int dy) { public boolean contains (Location point) { public void producesmoke () { 16

TrainController public class TrainController extends WindowController { private Train train; public void begin() { train = new Train (10, 150, canvas); 17 Ignoring the User! What if we want to limit what the user can do? For example, don t allow the user to drag the train off the track How do we change the dragging code to do that? How do we change the move method to only move horizontally? 18

Staying on track! public void onmousedrag (Location point) { if (traingrabbed) { double dx = point.getx() - lastpoint.getx(); double dy = point.gety() - lastpoint.gety(); train.move (dx, dy); lastpoint = point; Normal drag moves in 2 dimensions public void onmousedrag (Location point) { if (traingrabbed) { double dx = point.getx() - lastpoint.getx(); train.move (dx); lastpoint = point; Staying on track ignores vertical motion 19 Staying on track! public void move (double dx, double dy) { car.move (dx, dy); rearwheel.move (dx, dy); frontwheel.move (dx, dy); smokestack.move (dx, dy); Normal drag moves in 2 dimensions public void move (double dx) { car.move (dx, 0); rearwheel.move (dx, 0); frontwheel.move (dx, 0); smokestack.move (dx, 0); Staying on track moves 0 pixels vertically 20

Scope Scope determines the part of a program that knows about a declaration. Depends on Where the declaration is: Instance variable Parameter Local variable Use of words public and private 21 Instance Variables Declared inside a class, but outside all methods Should always be private Scope: the entire class Should be given a value either where they are declared or in the constructor Key differentiator: Remember their value between method calls public class Train { private FilledRect car; public Train () { car = new FilledRect (); public void move (int dx, int dy) { car.move (dx, dy); 22

Parameters Parameter Declared in the signature of a method or constructor Say neither public nor private Scope: the entire method or constructor Key differentiator: They are given a value based on an argument passed in when the method is called public Train (int left, int trackheight, DrawingCanvas canvas) { car = new FilledRect (left, trackheight -,,, canvas); Somewhere else: new Train (10, 50, canvas); new Train (100, 30, canvas); 23 Local variables Local variable Declared inside a method or constructor Say neither public nor private Scope: (part of) that method or constructor They are given a value with an assignment statement inside the method or constructor Go out of existence when the block in which they are declared ends Key differentiator: Good for temporary computations public void onmousedrag (Location point) { Line line = new Line (start, point, canvas); line.setcolor (Color.RED); 24

Blocks public void onmousedrag (Location point) { if (hidergrabbed) { double dx = point.getx() - lastpoint.getx(); double dy = point.gety() - lastpoint.gety(); hider.move (dx, dy); lastpoint = point; scope of dy A local variable s scope is from the point at which it is declared to the end of the enclosing block. 25 Blocks public void onmousedrag (Location point) { if (hidergrabbed) { double dx = point.getx() - lastpoint.getx(); double dy = point.gety() - lastpoint.gety(); hider.move (dx, dy); lastpoint = point; scope of dx A local variable s scope is from the point at which it is declared to the end of the enclosing block. 26

Reusing Names within a Class public class SomeController extends WindowController { public void onmousepress (Location point) { double x = point.getx(); public void onmouserelease (Location point) { double y = point.gety(); Scopes do not overlap. No problem. 27 Reusing Names within a Class public class SomeController extends WindowController { private Location point; public void onmousepress (Location point) { double x = point.getx(); public void begin () { point = new Location (30, 50); Scopes overlap. Inner declaration hides outer one. 28

Reusing Names within a Class public class SomeController extends WindowController { private Location point; public void onmousepress (Location point) { this.point = point; double x = point.getx(); this. always gives access to the instance variable 29 Reusing Names in 2 Classes public class SomeController extends WindowController { public void begin () { SmileyFace face = new SmileyFace (canvas); public class SmileyFace { public SmileyFace (DrawingCanvas canvas) { FramedOval face = new FramedOval(, canvas); 2 distinct variables that have distinct values 30

Reusing Names in 2 Classes public class SomeController extends WindowController { public void begin () { FilledRect grass = new FilledRect (, canvas); Train train = new Train (canvas); public class Train { public Train (DrawingCanvas canvas) { FilledRect car = new FilledRect(, canvas); 2 distinct variables that will have the same value at runtime 31 Summary Classes give us a way to treat a collection of objects as a single object When we define a class, we are defining an abstraction - a way of stepping back from implementation details to define a new thing A train is an abstraction of multiple shapes. We can use it thinking of it as its own type of thing, with its own interesting behavior, like move Abstraction is essential to build complex programs (or other things!) 32