© 2006 Pearson Addison-Wesley. All rights reserved 4.3.1 Non-void Methods Parameters are largely one-way communication.  Shared instances variables is.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Lecture 10.2 Different Types of Loops, including for and do.
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
UML Basics & Access Modifier
Object-Oriented Programming in Java. Object-Oriented Programming Objects are the basic building blocks in an O-O program. – A program consists of one.
© 2006 Pearson Addison-Wesley. All rights reserved Reference Types A variable of reference type refers to (is bound to) an object Data of reference.
© 2006 Pearson Addison-Wesley. All rights reserved Inheritance Systems Merchandise ElectronicsClothing TelevisionCamcorderShirtDressShoe DigitalAnalog.
Utilities (Part 2) Implementing static features 1.
Two Parts of Every ADT An abstract data type (ADT)  is a type for encapsulating related data  is abstract in the sense that it hides distracting implementation.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
© A+ Computer Science - Chicken yeller = new Chicken();
Lecture 8.3 The Use of JComponent. © 2006 Pearson Addison-Wesley. All rights reserved More About the Standard Drawing Classes java.awt.Container.
© 2006 Pearson Addison-Wesley. All rights reserved How to Design a Supplier 1) Determine the public methods. (Don’t forget the constructor(s).) 2)
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
© 2006 Pearson Addison-Wesley. All rights reserved A DrawingGizmo object can be moved around a window to draw lines. It appears like an arrow. (See.
Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved A Crash Course in the Use of Timed Events What kinds of.
EVENTS Wiring together objects, code, and actions.
Java Graphics Graphical Components as objects. Graphics A Component is ◦A rectangular region of a computer screen ◦A graphical entity ◦Can sometimes contains.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
© 2006 Pearson Addison-Wesley. All rights reserved Syntax if (some_condition) { then_clause } Notes some_condition must be a valid boolean expression.
4.3.1 Non-void Methods Parameters are largely one-way communication.  Shared instances variables is one way to accomplish this. calling codemethod parameter.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
STRATEGY PATTERN. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;
Lecture 3.1 Using Graphics with JFrame. © 2006 Pearson Addison-Wesley. All rights reserved javax.swing.JFrame - int x - int y - int width - int.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
A software specification indicates the task (or some aspect of the task) that is supposed to be performed when software executes. Types of Specifications.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
© 2006 Pearson Addison-Wesley. All rights reserved Design by Prototype Programmers often approach a problem by creating a series of prototypes. Each.
Lecture 4.1 More About Methods - Using a Prototyping Approach.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved A Crash Course in the Use of Timed Events What kinds of.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
Prototyping with Methods
How to Design Supplier Classes
public class Doubler extends Base {
Chapter 5 Classes.
Java Programming: Guided Learning with Early Objects
Prototyping with Methods
Introduction to Object-oriented Program Design
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
A+ Computer Science METHODS.
CS/ENGRD 2110 Fall 2018 Lecture 2: Objects and classes in Java
Class Hierarchies and Type Conformance
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
A+ Computer Science METHODS.
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved Non-void Methods Parameters are largely one-way communication.  Shared instances variables is one way to accomplish this. calling codemethod parameter passage Sometimes the method needs to transmit information back to the caller.  Another alternative is a non-void method. These are sometimes called functions. Characteristics of a Non-void Method 1) A non-void method is declared with a type instead of “ void ”. 2) A non-void method can be called anywhere an expression of the same type is permitted. 3) A non-void method returns a value. Therefore, the last instruction it executes must be a ____________ instruction.

© 2006 Pearson Addison-Wesley. All rights reserved Writing Your Own Non-void Methods Syntax The form of the method is the same as for void methods except replacing “ void ” with the name of a class (or primitive type). 1) The method completes execution and returns to the caller. 2) The value of expr is returned as the value of the method and used in the expression from which the method was called. return expr ; where expr denotes a valid expression with the same type as the method. Each non-void method must include a return instruction with the following form. Executing a return

© 2006 Pearson Addison-Wesley. All rights reserved Example private Rectangle newRedSquare( int s ) { Rectangle tmpRect; tmpRect = new Rectangle(0, 0, s, s); tmpRect.setBackground( Color.red ); return tmpRect; } private Rectangle newRedSquare( int s ) { Rectangle tmpRect; tmpRect = new Rectangle(0, 0, s, s); tmpRect.setBackground( Color.red ); return tmpRect; } The method A valid call (assuming bigSquare is a Rectangle variable) bigSquare = newRedSquare( 250 );

© 2006 Pearson Addison-Wesley. All rights reserved Another Example Program Requirements: Draw three jack-o-lanterns as follows What patterns do you see repeated? What does such repetition suggest for the use of methods? Our first prototype does not include the teeth or stems.

© 2006 Pearson Addison-Wesley. All rights reserved Jack-o-lantern prototype 1 import java.awt.*; import javax.swing.JFrame; /** First prototype of jack-o-lantern program * Author: David D. Riley * Date: Sep, 2005 */ public class Driver { private JFrame win; public Driver() { win = new JFrame("the window"); win.setBounds(10, 10, 380, 150); win.setLayout(null); win.setBackground( Color.black ); win.setVisible(true); makeAndAddJackOLantern(20, 5, win); makeAndAddJackOLantern(140, 5, win); makeAndAddJackOLantern(260, 5, win); win.repaint(); } // makeAndAddJackOLantern on next slide } import java.awt.*; import javax.swing.JFrame; /** First prototype of jack-o-lantern program * Author: David D. Riley * Date: Sep, 2005 */ public class Driver { private JFrame win; public Driver() { win = new JFrame("the window"); win.setBounds(10, 10, 380, 150); win.setLayout(null); win.setBackground( Color.black ); win.setVisible(true); makeAndAddJackOLantern(20, 5, win); makeAndAddJackOLantern(140, 5, win); makeAndAddJackOLantern(260, 5, win); win.repaint(); } // makeAndAddJackOLantern on next slide }

© 2006 Pearson Addison-Wesley. All rights reserved Jack-o-lantern prototype 1 (cont’d)... /** post: A jack-o-lantern with two eyes and a stem * has been instantiated and added to w * at location (x, y). */ private void makeAndAddJackOLantern(int x, int y, JFrame w) { Container jackCanvas; Oval body, leftEye, rightEye; jackCanvas = new Container(); jackCanvas.setBounds(x, y, 100, 100); body = new Oval(0, 20, 100, 80); body.setBackground(Color.orange); jackCanvas.add(body, 0); leftEye = new Oval(20, 25, 15, 10); body.add(leftEye, 0); rightEye = new Oval(65, 25, 15, 10); body.add(rightEye, 0); // need to make and add stem here // need to make and add teeth here w.add(jackCanvas, 0); }... /** post: A jack-o-lantern with two eyes and a stem * has been instantiated and added to w * at location (x, y). */ private void makeAndAddJackOLantern(int x, int y, JFrame w) { Container jackCanvas; Oval body, leftEye, rightEye; jackCanvas = new Container(); jackCanvas.setBounds(x, y, 100, 100); body = new Oval(0, 20, 100, 80); body.setBackground(Color.orange); jackCanvas.add(body, 0); leftEye = new Oval(20, 25, 15, 10); body.add(leftEye, 0); rightEye = new Oval(65, 25, 15, 10); body.add(rightEye, 0); // need to make and add stem here // need to make and add teeth here w.add(jackCanvas, 0); }... java.awt.Container

© 2006 Pearson Addison-Wesley. All rights reserved java.awt.Container Class Diagram java.awt.Container - int x - int y - int width - int height «constructor» + Container() «update» + void add(java.awt.Component, int) + void remove(java.awt.Component) + void repaint( ) + void setBounds(int, int, int, int) + void setLocation(int, int) + void setSize(int, int) «query» + int getX() + int getY() + int getWidth() + int getHeight()...

© 2006 Pearson Addison-Wesley. All rights reserved Jack-o-lantern -- Prototype 2 Add the stems to the jacks. Make two changes: 1) alter the makeAndAddJackOLantern method as shown below. 2) write a makeAndAddStem method (see next slide). // need to make and add stem here is replaced by... makeAndAddStem(jackCanvas);

© 2006 Pearson Addison-Wesley. All rights reserved Jack-o-lantern -- Prototype 2 (cont’d)... /** pre: c.getWidth()==100 and c.getHeight()>20 * post: A green pumpkin stem image has been added * to the top center of c * at location (x, y). */ private void makeAndAddStem(Container c) { Oval stem, stemCover; Container stemBoundary; stemBoundary = new Container(); stemBoundary.setBounds(40, 0, 20, 20); stem = new Oval(6, 6, 28, 28); stem.setBackground(Color.green); stemCover = new Oval(7, 7, 14, 14); stem.add(stemCover, 0); stemBoundary.add(stem, 0); c.add(stemBoundary, 0); }... /** pre: c.getWidth()==100 and c.getHeight()>20 * post: A green pumpkin stem image has been added * to the top center of c * at location (x, y). */ private void makeAndAddStem(Container c) { Oval stem, stemCover; Container stemBoundary; stemBoundary = new Container(); stemBoundary.setBounds(40, 0, 20, 20); stem = new Oval(6, 6, 28, 28); stem.setBackground(Color.green); stemCover = new Oval(7, 7, 14, 14); stem.add(stemCover, 0); stemBoundary.add(stem, 0); c.add(stemBoundary, 0); }...

© 2006 Pearson Addison-Wesley. All rights reserved Jack-o-lantern -- Prototype 3 Add the teeth to the jacks. Make two changes: 1) alter the makeAndAddJackOLantern method as shown below. 2) write a newTooth method (see next slide). // need to make and add teeth here is replaced by... tooth1 = newTooth(20, 52); body.add(tooth1, 0); tooth2 = newTooth(35, 50); body.add(tooth2, 0); tooth3 = newTooth(50, 50); body.add(tooth3, 0); tooth4 = newTooth(65, 52); body.add(tooth4, 0); Let’s use a non-void method called newTooth. Note that the method must also include declarations for Container variables tooth1, tooth2, tooth3 and tooth4.

© 2006 Pearson Addison-Wesley. All rights reserved Jack-o-lantern -- Prototype 3 (cont’d)... /** post: result == a 15 by 15 Container positioned at (x,y) * and displaying a tooth-like image. */ private Container newTooth(int x, int y) { Container background; Rectangle toothTop, toothBottom; background = new Container(); background.setBounds(x, y, 15, 15); toothTop = new Rectangle(0, 0, 15, 10); background.add(toothTop, 0); toothBottom = new Rectangle(4, 9, 7, 7); background.add(toothBottom, 0); return background; }... /** post: result == a 15 by 15 Container positioned at (x,y) * and displaying a tooth-like image. */ private Container newTooth(int x, int y) { Container background; Rectangle toothTop, toothBottom; background = new Container(); background.setBounds(x, y, 15, 15); toothTop = new Rectangle(0, 0, 15, 10); background.add(toothTop, 0); toothBottom = new Rectangle(4, 9, 7, 7); background.add(toothBottom, 0); return background; }...

© 2006 Pearson Addison-Wesley. All rights reserved Jack-o-lantern -- a final improvement is replaced by... tooth1 = newTooth(20, 52); body.add(tooth1, 0); tooth2 = newTooth(35, 50); body.add(tooth2, 0); tooth3 = newTooth(50, 50); body.add(tooth3, 0); tooth4 = newTooth(65, 52); body.add(tooth4, 0); body.add(newTooth(20, 52), 0); body.add(newTooth(35, 50), 0); body.add(newTooth(50, 50), 0); body.add(newTooth(65, 52), 0);