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

Similar documents
Lecture 6 CSE 11 Fall 2013

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

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

Appendix E. Solutions to Exercises

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

Topic Notes: Java and Objectdraw Basics

CS 134 Programming Exercise 3:

CS 134 Programming Exercise 7:

File: GFace.java /* * File: GFace.java * This class implements a face as a GCompound. */

Programming Assignment 4 ( 100 Points )

Programming Exercise. Scribbler

Lecture 10 Declarations and Scope

Lecture 15 CSE11 Fall 2013 Arrays

Chapter 4 Defining Classes I

To gain experience using recursion and recursive data structures.

CS 134 Programming Exercise 2:

Programming Assignment 3 ( 100 Points ) START EARLY!

Why Structural Recursion Should Be Taught Before Arrays in CS 1

CS Problem Solving and Object-Oriented Programming

CS 134 Midterm Fall 2012

Chapter 4: Writing Classes

CS112 Lecture: Working with Numbers

CS Programming Exercise:

CS 162 Intro to CS II. Structs vs. Classes

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Solutions for Section #4

A Library to Support a Graphics-Based Object-First Approach to CS 1

Chapter 4. Defining Classes I

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Defining Classes and Methods

EECS168 Exam 3 Review

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

ITI Introduction to Computing II

Programming Assignment 2 (PA2) - DraggingEmoji & ShortLongWords

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

CS111: PROGRAMMING LANGUAGE II

PIC 10A. Lecture 15: User Defined Classes

ITI Introduction to Computing II

Comp 151. Using Objects (and the beginning of graphics)

CS 106 Introduction to Computer Science I

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

Classes Classes 2 / 35

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

Anatomy of a Class Encapsulation Anatomy of a Method

Lecture 9 CSE11 Fall 2013 Active Objects

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

What will this print?

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

ECE 122. Engineering Problem Solving with Java

CMSC131. Objects: Designing Classes and Creating Instances

Programming Project 1

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

Event-Driven Programming Facilitates Learning Standard Programming Concepts

CMSC131. Library Classes

Object Oriented Programming in C#

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Introduction to Computer Programming for Non-Majors

CS 134 Programming Exercise 1:

CSE 11 Midterm Fall 2008

Programming Languages and Techniques (CIS120)

CS 302: Introduction to Programming in Java. Lecture 15

Computer Science II (20073) Week 1: Review and Inheritance

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

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

CS-202 Introduction to Object Oriented Programming

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

CITS 4406 Problem Solving & Programming. Lecture 03 Numeric Data Processing

Lecture 18 CSE11 Fall 2013 Inheritance

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Today s Agenda. Quick Review

Assignment 1 due Monday at 11:59pm

Asking For Help. BIT 115: Introduction To Programming 1

Programming II (CS300)

CS 134 Programming Exercise 9:

Solutions for Section #2

2/4/11. Python Programming: An Introduction to Computer Science. Scientific Workflow Systems. Announcements

Programming II (CS300)

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Chapter 6 Introduction to Defining Classes

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

CS 170 Java Programming 1. Week 13: Classes, Testing, Debugging

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

CSCI 1301: Introduction to Computing and Programming

Classes Classes 2 / 36

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

And Even More and More C++ Fundamentals of Computer Science

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS1004: Intro to CS in Java, Spring 2005

Exam 1 - (20 points)

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Chapter 14. Inheritance. Slide 1

CS 112 Introduction to Programming

Solutions for Section #2

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

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

Transcription:

CS 120 Lecture 18 Java Classes (Java: An Eventful Approach, Ch. 6), 13 November 2012 Slides Credit: Bruce, Danyluk and Murtagh 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? 1

What if We can free ourselves from the limitations of ovals and rectangles and move on to A Draggable Face public class RevFaceDrag extends WindowController { private FunnyFace happy; private Location lastpoint; // FunnyFace to be dragged private boolean happygrabbed = false; // Whether happy has been grabbed by the mouse public void begin() { // Make the FunnyFace happy = new FunnyFace( FACE_LEFT, FACE_TOP, canvas ); public void onmousepress( Location point ){ lastpoint = point; happygrabbed = happy.contains( point ); public void onmousedrag( Location point ) { if (happygrabbed ) { happy.move( point.getx() lastpoint.getx(), point.gety() lastpoint.gety() ); lastpoint = point; 2

Making a Funny Face Physical Characteristics: Head Mouth Two Eyes Behaviors Can check contains Movable (by dragging) Components of a Class Instance Variables Methods Constructors (these are new!) 3

Instance Variables The data stored in a class Maintain the state of an object Instance variables for FunnyFace: private FramedOval head; private FramedOval mouth; private FramedOval lefteye, righteye; Methods Methods: Sections of code executed at certain times: onmousepress onmousedrag begin Special Types of Methods: Mutator Methods Accessor Methods 4

Actual Parameters Information given to a method when the method is invoked In the invocation of: happy.move (10, 20); happy is the object move is the name of the method the values 10 and 20 are passed to happy.move Order of parameters matters! A Mutator Method for FunnyFace //Move the entire face by (dx,dy) public void move( double dx, double dy ) { head.move( dx, dy ); lefteye.move (dx, dy ); righteye.move( dx, dy ); mouth.move( dx, dy ); 5

Formal Parameters dx and dy are the formal parameters Formal Parameters: used in the method declaration determine the order of actual parameters determine the type of the actual parameters Accessor Methods Return information about an object // Determine whether pt is inside FunnyFace public boolean contains( Location pt ) { return head.contains( pt ); 6

Constructors A constructor creates an object and initializes relevant instance variables Invoked by the name of the class public FunnyFace( double left, double top, DrawingCanvas canvas ){ head = new FramedOval( left, top, FACE_WIDTH, FACE_HEIGHT, canvas ); mouth = new FramedOval( left+(face_width-mouth_width)/2, top+2*face_height/3, MOUTH_WIDTH, MOUTH_HEIGHT, canvas ); lefteye = new FramedOval( left+eye_offset-eye_radius/2, top+2*face_height/3, MOUTH_WIDTH, MOUTH_HEIGHT, canvas ); righteye = new FramedOval( left+face_width-eye_offset- EYE_RADIUS/2, top+eye_offset, EYE_RADIUS, EYE_RADIUS, canvas ); 7

Class Convention public class Name { constant definitions variable declarations constructor methods Defining Methods Indirectly What if we wanted to move the FunnyFace to a specified location? We can reuse methods already defined public void moveto( double x, double y ) { this.move( x head.getx(), y head.gety() ); 8

this public void moveto( double x, double y ) { this.move( x head.getx(), y head.gety() ); We can use this to refer to object itself Local Variables Recall variables and their uses: Instance Variables: maintain the state of an object Formal Parameters: Determine the information required by a method Local Variables: Declared, initialized and used within a method Locals do not retain their values between method invocations 9

Using Local Variables public double elapsedmilliseconds() { // Return number of // milliseconds since last reset double difftime; difftime = System.currentTimeMillis() starttime; return difftime; difftime is a local variable Using Local Variables To simplify difficult calculations To eliminate redundant computations 10

Overloading Evaluating 5 + 7 gives 12 Evaluating "Good" + " Day" gives "Good Day" + is an overloaded operator: it can add numbers and concatenate strings The type of operation is determined by the types of the operands Overloading II In Java, users cannot define new overloaded operations We can overload methods! 11

Overloading III Consider: public void moveto( double x, double y ) { this.move( x head.getx(), y head.gety() ); And public void moveto( Location pt ) { this.move( pt.getx() head.getx(), pt.gety() head.gety() ); Overloading IV What determines when each moveto is invoked? Answer: The actual parameters moveto can be invoked either with two doubles or with a Location 12

Review Components of a class Instance Variables Constructors Methods Formal vs Actual Parameters Instance Variables and Local Variables Overloading Student To Do s HW09 SEVEN!!!! Programs from chapter 7! Practice examples on your own! Read Java: An Eventful Approach Ch. 6 (Today) 26 13