4.3.1 Non-void Methods Parameters are largely one-way communication.  Shared instances variables is one way to accomplish this. calling codemethod parameter.

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

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.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
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.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
1 Graphical objects We will draw graphics in Java using 3 kinds of objects: DrawingPanel : A window on the screen. Not part of Java; provided by the authors.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
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.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
©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.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
METHODS AND SCOPE CSCE More on Methods  This is chapter 6 in Small Java.
© 2006 Pearson Addison-Wesley. All rights reserved Inheritance Systems Merchandise ElectronicsClothing TelevisionCamcorderShirtDressShoe DigitalAnalog.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
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.
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.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
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.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Classes Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.
STRATEGY PATTERN. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
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.
© 2006 Pearson Addison-Wesley. All rights reserved Non-void Methods Parameters are largely one-way communication.  Shared instances variables is.
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.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
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.
Creating Scenarios In Greenfoot. Greenfoot Scenarios are made up of: Greenfoot Scenarios are made up of: A World class. A World class. Actor classes.
Lecture 4.1 More About Methods - Using a Prototyping Approach.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
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
Access Functions and Friend Functions
Inheritance ITI1121 Nour El Kadri.
Building Java Programs
Building Java Programs
Chapter 5 Classes.
Prototyping with Methods
Introduction to Object-oriented Program Design
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
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
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Presentation transcript:

4.3.1 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 ________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 return instruction.

4.3.2 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. ______ ______; 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

4.3.3 Think Halloween! 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.

4.3.4 Prototype 1 import java.awt.*; import javax.swing.JFrame; /** First prototype of jack-o-lantern program * Author: David D. Riley * Date: Sep, 2015 */ public class Driver { private JFrame win; public Driver() { Rectangle leftJack, midJack, rightJack; win = new JFrame("the window"); win.setBounds(10, 10, 380, 150); win.setLayout(null); win.getContentPane().setBackground( Color.black ); win.setVisible(true); leftJack = jackOLantern(20, 5); win.add( leftJack, 0 ); midJack = jackOLantern(140, 5); win.add( midJack, 0 ); rightJack = jackOLantern(260, 5); win.add( rightJack, 0 ); win.repaint(); } // the jackOLantern on next slide } import java.awt.*; import javax.swing.JFrame; /** First prototype of jack-o-lantern program * Author: David D. Riley * Date: Sep, 2015 */ public class Driver { private JFrame win; public Driver() { Rectangle leftJack, midJack, rightJack; win = new JFrame("the window"); win.setBounds(10, 10, 380, 150); win.setLayout(null); win.getContentPane().setBackground( Color.black ); win.setVisible(true); leftJack = jackOLantern(20, 5); win.add( leftJack, 0 ); midJack = jackOLantern(140, 5); win.add( midJack, 0 ); rightJack = jackOLantern(260, 5); win.add( rightJack, 0 ); win.repaint(); } // the jackOLantern on next slide }

4.3.5 Jack-o-lantern Prototype 1 (cont’d)... /** == a jack-o-lantern with two eyes and a stem * at location (x, y). */ private Rectangle jackOLantern(int x, int y) { Rectangle jackCanvas; Oval body, leftEye, rightEye; jackCanvas = new Rectangle(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 return jackCanvas; }... /** == a jack-o-lantern with two eyes and a stem * at location (x, y). */ private Rectangle jackOLantern(int x, int y) { Rectangle jackCanvas; Oval body, leftEye, rightEye; jackCanvas = new Rectangle(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 return jackCanvas; }...

4.3.6 Jack-o-lantern -- Prototype 2 Add the stems to the jacks. Make two changes: 1) alter the jackOLantern method as shown below. 2) write a getStem method (see next slide). // need to add stem here is replaced by... Rectangle stem; stem = getStem(); jackCanvas.add(stem, 0); OR

4.3.7 Jack-o-lantern -- Prototype 2 (cont’d)... /** == a green pumpkin stem image * AND the stem is 20 by 20 pixels * AND the stem is located at (40, 0) */ private Rectangle getStem() { Oval stem, stemCover; Rectangle stemBoundary; stemBoundary = new Rectangle(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); return stemBoundary; }... /** == a green pumpkin stem image * AND the stem is 20 by 20 pixels * AND the stem is located at (40, 0) */ private Rectangle getStem() { Oval stem, stemCover; Rectangle stemBoundary; stemBoundary = new Rectangle(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); return stemBoundary; }...

4.3.8 Jack-o-lantern -- Prototype 3 Add the teeth to the jacks. Make two changes: 1) alter the jackOLantern method as shown below. 2) write a getTooth method (see next slide). // need to make and add teeth here is replaced by... tooth1 = getTooth(20, 52); body.add(tooth1, 0); tooth2 = getTooth(35, 50); body.add(tooth2, 0); tooth3 = getTooth(50, 50); body.add(tooth3, 0); tooth4 = getTooth(65, 52); body.add(tooth4, 0); Let’s use a non-void method called getTooth.

4.3.9 Jack-o-lantern -- Prototype 3 (cont’d)... /** post: result == is 15 by 15 pixels * AND positioned at (x,y) * AND displays a black tooth-like image on orange */ private Rectangle getTooth(int x, int y) { Rectangle background, toothTop, toothBottom; background = new Rectangle(x, y, 15, 15); background.setBackground( Color.orange ); 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 == is 15 by 15 pixels * AND positioned at (x,y) * AND displays a black tooth-like image on orange */ private Rectangle getTooth(int x, int y) { Rectangle background, toothTop, toothBottom; background = new Rectangle(x, y, 15, 15); background.setBackground( Color.orange ); toothTop = new Rectangle(0, 0, 15, 10); background.add(toothTop, 0); toothBottom = new Rectangle(4, 9, 7, 7); background.add(toothBottom, 0); return background; }...

Using Anonymous Objects is replaced by... tooth1 = getTooth(20, 52); body.add(tooth1, 0); tooth2 = getTooth(35, 50); body.add(tooth2, 0); tooth3 = getTooth(50, 50); body.add(tooth3, 0); tooth4 = getTooth(65, 52); body.add(tooth4, 0); body.add( getTooth(20, 52), 0 ); body.add( getTooth(35, 50), 0 ); body.add( getTooth(50, 50), 0 ); body.add( getTooth(65, 52), 0 );

Jack-o-lantern – a final improvement Suppose you'd like to create overlapping jack-o-lanterns. Problem: Rectangles are not ideal for backgrounds. Solution: java.awt.Container

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()... Note the non-void (query) methods.