Graphics Tools and Parameters Chris Nevison Barbara Wells.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 6 Introduction to Defining Classes
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Chapter 4: Writing Classes
1 Composition A whole-part relationship (e.g. Dog-Tail) Whole and part objects have same lifetime –Whole creates instance of part in its constructor In.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
1. 2 Introduction to Methods  Method Calls  Parameters  Return Type  Method Overloading  Accessor & Mutator Methods  Student Class: Revisited.
Classes we can write our own classes – to represent and define our objects e.g. class for Complex objects represents a complex number defines its properties.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Programming What is a program? –A set of instructions –Understood by a computer.
Recall: Accessing Location Info private Location firstPoint; public void onMousePress( Location pressPt ) { new Text("Pressed", pressPt, canvas ); firstPoint.
Understanding class definitions Looking inside classes.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Composition details Recall, composition involves 3 things: –Declaration of instance variable of.
Chapter 5 - Making Music: An On-Screen Piano
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Writing Classes (Chapter 4)
Overview of our Approach Program Structure –Data, variables, and parameters –Basic control structures (conditionals, loops and Threads) –Class definitions.
METHODS AND SCOPE CSCE More on Methods  This is chapter 6 in Small Java.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Local Variables Garbage collection. © 2006 Pearson EducationParameters2 of 10 Using Accessors and Mutators Together What if you want to get a property.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 2 Objects and Mutator Methods. A Sample Program A rudimentary solar simulation: We want a click of the mouse in the window to make the ‘sun’ rise.
A Class of Our Own We’ve used many classes: –Location –FilledRect –Color –Text –And More! –But what if we want to make our own class?
Java Object Oriented Programming Encapsulation, Inheritance, Polymorphism (Java: An Eventful Approach - Ch. 17, 21.7), Slides Credit: Bruce, Danyluk and.
Chapter 5 Introduction to Defining Classes
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Classes Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
Classes. Preparation Scene so far has been background material and experience –Computing systems and problem solving –Variables –Types –Input and output.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java Working with Numbers (Java: An Eventful Approach, Ch 3), Slides Credit: Bruce, Danyluk and Murtagh CS 120 Lecture October 2012.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Java Objects (Java: An Eventful Approach, Ch 2), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture October 2012.
Teaching Control Structures, using objectdraw Chris Nevison Barbara Wells.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
A structured walkthrough
Inheritance ITI1121 Nour El Kadri.
Intro To Classes Review
Java Classes (Java: An Eventful Approach, Ch. 6),
Using local variable without initialization is an error.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Defining Your Own Classes Part 1
Defining Classes and Methods
Chapter 4: Writing classes
An Introduction to Java – Part II
Variables and Their scope
Classes, Encapsulation, Methods and Constructors (Continued)
Defining Classes and Methods
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Dr. R Z Khan Handout-3 Classes
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Corresponds with Chapter 5
Day 11 The Last Week!.
Presentation transcript:

Graphics Tools and Parameters Chris Nevison Barbara Wells

Two non-displayable classes Color –specifies a color –from java.awt library Location –specifies a location in two dimensions –part of objectdraw library

class Color constants –Color.white, Color.blue, Color.yellow, etc constructor: –takes red, green, blue parameters Color skyBlue = new Color(200, 200, 255); often used to set color of another object elevator.setColor(Color.blue); elevator.setColor(new Color(100, 150, 50));

class Location represents a position in 2 dimensional space –uses real value coordinates (double) has accessor methods to return coordinates –getX(), getY() has modifier or mutator methods to change loc.translate(dx, dy); constructor sets initial position Location loc = new Location(dx, dy);

Formal Parameters Specified in the definition of the method Represent information passed to the method when it is called Can be used to determine the behavior of the method or in calculations carried out by the method.

Formal Parameter Example public void onMouseClick(Location point) { elevator.moveTo(point); } Formal ParameterType of parameter Using the formal parameter Declaration of method

Actual Parameters the values given as parameters when a method is called –may be a literal value (number, string literal) –may be a variable in the current context –may be an expression –expression may be a newly created object new...

Actual Parameters Expression used for actual parameter must match the type specified in the declaration of the method Example: FilledRect documentation specifies that the moveTo method has a parameter of type Location –call to moveTo for an instance of FilledRect must supply an actual parameter of that type

Actual Parameters elevator.moveTo(point); instance of FilledRect call to method actual parameter

Actual Parameters elevator.moveTo(new Location(30,60)); instance of FilledRect call to method actual parameter, a newly created Location with no name

Communicating among methods using parameters objective: draw a line from location where mouse is pressed to location where mouse is released use WindowController methods –onMousePress –onMouseRelease –each has a formal parameter Location point create a Line with constructor that takes two Location parameters for the two ends

LineDraw, first attempt public class LineDraw1 extends WindowController { public void onMousePress(Location point) { Location startLine = point; } public void onMouseRelease(Location point) { Location endLine = point; new Line(startLine, endLine, canvas); }

Compile Error C:\apwork\Workshops\summer02\ObjDrawCode\LineDraw\Line Draw1.java:15: cannot resolve symbol symbol : variable startLine location: class LineDraw1 new Line(startLine, endLine, canvas); ^ 1 error error type offending word location of error

LineDraw, first attempt public class LineDraw1 extends WindowController { public void onMousePress(Location point) { Location startLine = point; } public void onMouseRelease(Location point) { Location endLine = point; new Line(startLine, endLine, canvas); } local variables parameters local to other method, not visible here

Scope variables declared within a method are only visible within that method formal parameters for a method are only visible within that method instance variables are visible within the class –an instance variable provides a means of communicating between methods within the same class

LineDraw, correct public class LineDraw2 extends WindowController{ Location startLine; public void onMousePress(Location point) { startLine = point;// assigns to instance variable } public void onMouseRelease(Location point) { Location endLine = point; new Line(startLine, endLine, canvas); } instance variable assign to the instance variable use the instance variable

Example: Scribble from Java, An Eventful Approach Uses these WindowController methods –onMousePress –onMouseDrag onMouseDrag is called repeatedly while the mouse is dragged

Scribble, attempt 1 public class Scribble1 extends WindowController{ Location startLine; public void onMousePress(Location point) { startLine = point; } public void onMouseDrag(Location point) { Location endLine = point; new Line(startLine, endLine, canvas); }

Scribble, attempt 1 result

Scribble, correct public class Scribble2 extends WindowController{ Location previousPoint; Location currentPoint; public void onMousePress(Location point) { previousPoint = point; } public void onMouseDrag(Location point) { currentPoint = point; new Line(previousPoint, currentPoint, canvas); previousPoint = currentPoint; }

State of an object Instance variables specify the attributes of an object State of an object is the set of attributes that determine its current configuration, that may change In Scribble class, state is given by –previousPoint –currentPoint Modifier or Mutator methods change state –affect the future behavior of the object