Lecture 4.1 More About Methods - Using a Prototyping Approach.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved Static Methods In Java it is possible to declare.
Lecture 10.2 Different Types of Loops, including for and do.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
ISBN Chapter 9 Subprograms. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-2 Figure 9.1 The three semantics models of parameter.
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
ISBN Chapter 10 Implementing Subprograms.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
CS 201 Functions Debzani Deb.
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
Introduction to Programming with Java, for Beginners Scope.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
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.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Topics Scope Scope and Lifetime Referencing Environments.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Methods. 2 A sequence of statements can be packaged together as a unit and re-used. A method is a named unit of re-usable code. modifier returnType methodName(
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Local Variables Garbage collection. © 2006 Pearson EducationParameters2 of 10 Using Accessors and Mutators Together What if you want to get a property.
Objective You will be able to define and identify the basic components of a java program by taking notes, seeing examples, and completing a lab. Construction.
© 2006 Pearson Addison-Wesley. All rights reserved How to Design a Supplier 1) Determine the public methods. (Don’t forget the constructor(s).) 2)
CPS120: Introduction to Computer Science Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CPS120: Introduction to Computer Science Lecture 14 Functions.
© 2006 Pearson Addison-Wesley. All rights reserved Enter onoff channel selection volume Ave-+ TV int channel int volume boolean isOperating.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
© 2006 Pearson Addison-Wesley. All rights reserved Syntax if (some_condition) { then_clause } Notes some_condition must be a valid boolean expression.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Lecture 7.1 Selection - the if Instruction. © 2006 Pearson Addison-Wesley. All rights reserved An algorithm often needs to make choices… choose.
4.3.1 Non-void Methods Parameters are largely one-way communication.  Shared instances variables is one way to accomplish this. calling codemethod parameter.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Lecture 3.1 Using Graphics with JFrame. © 2006 Pearson Addison-Wesley. All rights reserved javax.swing.JFrame - int x - int y - int width - int.
© 2006 Pearson Addison-Wesley. All rights reserved Non-void Methods Parameters are largely one-way communication.  Shared instances variables is.
© 2006 Pearson Addison-Wesley. All rights reserved Design by Prototype Programmers often approach a problem by creating a series of prototypes. Each.
Methods main concepts – method call – object 's methods – actual parameters – method declaration – formal parameters – return value other concepts – method.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
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.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Functions + Overloading + Scope
Prototyping with Methods
Chapter 7: User-Defined Functions II
How to Design Supplier Classes
Prototyping with Methods
Chapter 4 Procedural Methods.
Chapter 6 Methods: A Deeper Look
CSE 115 September 29 – October 3, 2008.
CSE 115 September 29 – October 3, 2008.
Chapter 7 Procedural Methods.
Implementing Subprograms
A simple function.
Corresponds with Chapter 5
Classes and Objects Systems Programming.
Presentation transcript:

Lecture 4.1 More About Methods - Using a Prototyping Approach

© 2006 Pearson Addison-Wesley. All rights reserved Prototyping with Methods Recall that the process of prototyping is one of creating a sequence of prototypes for the same problem. Each prototype is expected to move closer to the final product. Example: Think of a name for this image.

© 2006 Pearson Addison-Wesley. All rights reserved import java.awt.Color; import javax.swing.JFrame; public class Driver { private JFrame win; private Oval poleTop, poleBottom; private Rectangle pole; public Driver() { win = new JFrame("Ring Stack"); win.setBounds(10, 10, 500, 400); win.setLayout(null); win.setVisible(true); win.setBackground(Color.black); pole = new Rectangle(240, 80, 20, 200); pole.setBackground(Color.yellow); win.add(pole, 0); poleTop = new Oval(240, 77, 20, 6); poleTop.setBackground(Color.orange); win.add(poleTop, 0); poleBottom = new Oval(240, 277, 20, 6); poleBottom.setBackground(Color.yellow); win.add( poleBottom, 0 ); /* construct and place three rings */ win.repaint(); } import java.awt.Color; import javax.swing.JFrame; public class Driver { private JFrame win; private Oval poleTop, poleBottom; private Rectangle pole; public Driver() { win = new JFrame("Ring Stack"); win.setBounds(10, 10, 500, 400); win.setLayout(null); win.setVisible(true); win.setBackground(Color.black); pole = new Rectangle(240, 80, 20, 200); pole.setBackground(Color.yellow); win.add(pole, 0); poleTop = new Oval(240, 77, 20, 6); poleTop.setBackground(Color.orange); win.add(poleTop, 0); poleBottom = new Oval(240, 277, 20, 6); poleBottom.setBackground(Color.yellow); win.add( poleBottom, 0 ); /* construct and place three rings */ win.repaint(); } A First Prototype

© 2006 Pearson Addison-Wesley. All rights reserved Private Methods When programs get to be too lengthy, it is a good idea to create subprograms. In Java subprograms take the form of methods. A method has its own code (body) and sometimes has its own (local) variables. A private method is declared within a class and can be called from any other method within the same class import java.awt.Color; import javax.swing.JFrame; public class Driver { private JFrame win;... public Driver() { win = new JFrame( "Ring Stack " );... } private void someLocalMethod() {... }... } import java.awt.Color; import javax.swing.JFrame; public class Driver { private JFrame win;... public Driver() { win = new JFrame( "Ring Stack " );... } private void someLocalMethod() {... }... } instance variables imports private methods

© 2006 Pearson Addison-Wesley. All rights reserved Private Method (parameterless & void) private void MethodName (){ InstructionSequence } A private method is called by an instruction that consists of the form MethodName( ); When a method is called, the body of the method ( InstructionSequence ) is executed, then the method returns to the location immediately after the call instruction.

© 2006 Pearson Addison-Wesley. All rights reserved public class Driver { private JFrame win; private Oval poleTop, poleBottom, redRing, ringCenter; private Rectangle pole; public Driver() { win = new JFrame("Ring Stack"); win.setBounds(10, 10, 500, 400); win.setLayout(null); win.setVisible(true); win.setBackground(Color.black); pole = new Rectangle(240, 80, 20, 200); pole.setBackground(Color.yellow); win.add( pole, 0 ); poleTop = new Oval(240, 77, 20, 6); poleTop.setBackground(Color.orange); win.add( poleTop, 0 ); poleBottom = new Oval(240, 277, 20, 6); poleBottom.setBackground(Color.yellow); win.add( poleBottom, 0 ); makeRedRing(); /* construct and place two more rings */ win.repaint(); } private void makeRedRing() { redRing = new Oval(200, 150, 100, 30); redRing.setBackground(Color.red); win.add( redRing, 0 ); ringCenter = new Oval(10, 10, 80, 10); ringCenter.setBackground( Color.black ); redRing.add( ringCenter, 0 ); } public class Driver { private JFrame win; private Oval poleTop, poleBottom, redRing, ringCenter; private Rectangle pole; public Driver() { win = new JFrame("Ring Stack"); win.setBounds(10, 10, 500, 400); win.setLayout(null); win.setVisible(true); win.setBackground(Color.black); pole = new Rectangle(240, 80, 20, 200); pole.setBackground(Color.yellow); win.add( pole, 0 ); poleTop = new Oval(240, 77, 20, 6); poleTop.setBackground(Color.orange); win.add( poleTop, 0 ); poleBottom = new Oval(240, 277, 20, 6); poleBottom.setBackground(Color.yellow); win.add( poleBottom, 0 ); makeRedRing(); /* construct and place two more rings */ win.repaint(); } private void makeRedRing() { redRing = new Oval(200, 150, 100, 30); redRing.setBackground(Color.red); win.add( redRing, 0 ); ringCenter = new Oval(10, 10, 80, 10); ringCenter.setBackground( Color.black ); redRing.add( ringCenter, 0 ); }

© 2006 Pearson Addison-Wesley. All rights reserved Local Variables Any method can have its own local variables. Local variables are declared within the body of the method. (Note: it is best to include local variable declarations before other instructions of the body.) The syntax of a local variable declaration is the same as for an instance variable excepting that the “private” prefix must be omitted. The scope (region where they are known) of an instance variable is the class in which it is declared. The scope of a local variable is its method, enclosed by { … }. Local variables are preferable programming style to instance variables.

© 2006 Pearson Addison-Wesley. All rights reserved private void makeRedRing( ) { Oval redRing, ringCenter; Rectangle ringCover, centerCover; redRing = new Oval(200, 150, 100, 30); redRing.setBackground(Color.red); win.add( redRing, 0 ); ringCenter = new Oval(10, 10, 80, 10); ringCenter.setBackground( Color.black ); redRing.add( ringCenter, 0 ); ringCover = new Rectangle(40, 0, 20, 11); ringCover.setBackground(Color.yellow); redRing.add( ringCover, 0 ); centerCover = new Rectangle(30, 0, 20, 11); centerCover.setBackground(Color.yellow); ringCenter.add( centerCover, 0 ); } private void makeRedRing( ) { Oval redRing, ringCenter; Rectangle ringCover, centerCover; redRing = new Oval(200, 150, 100, 30); redRing.setBackground(Color.red); win.add( redRing, 0 ); ringCenter = new Oval(10, 10, 80, 10); ringCenter.setBackground( Color.black ); redRing.add( ringCenter, 0 ); ringCover = new Rectangle(40, 0, 20, 11); ringCover.setBackground(Color.yellow); redRing.add( ringCover, 0 ); centerCover = new Rectangle(30, 0, 20, 11); centerCover.setBackground(Color.yellow); ringCenter.add( centerCover, 0 ); }

© 2006 Pearson Addison-Wesley. All rights reserved Private Method with Parameters private void MethodName (){ InstructionSequence { Parms TypeParameterName, Type denotes any valid data type (such as int or the name of some class). Parameters (also called formal parameters) are assigned arguments at the time of call. Otherwise, they behave like local variables.

© 2006 Pearson Addison-Wesley. All rights reserved public Driver() { win = new JFrame(” Ring Stack” );... makeRing(130, Color.green); makeRing(150, Color.red); makeRing(170, Color.blue); win.repaint(); } private void makeRing(int y, Color c) { Oval theRing, ringCenter; Rectangle ringCover, centerCover; theRing = new Oval(200, y, 100, 30); theRing.setBackground(c); win.add( theRing, 0 ); ringCenter = new Oval(10, 10, 80, 10); ringCenter.setBackground( Color.black ); theRing.add( ringCenter, 0 ); ringCover = new Rectangle(40, 0, 20, 11); ringCover.setBackground(Color.yellow); theRing.add( ringCover, 0 ); centerCover = new Rectangle(30, 0, 20, 11); centerCover.setBackground(Color.yellow); ringCenter.add( centerCover, 0 ); }... public Driver() { win = new JFrame(” Ring Stack” );... makeRing(130, Color.green); makeRing(150, Color.red); makeRing(170, Color.blue); win.repaint(); } private void makeRing(int y, Color c) { Oval theRing, ringCenter; Rectangle ringCover, centerCover; theRing = new Oval(200, y, 100, 30); theRing.setBackground(c); win.add( theRing, 0 ); ringCenter = new Oval(10, 10, 80, 10); ringCenter.setBackground( Color.black ); theRing.add( ringCenter, 0 ); ringCover = new Rectangle(40, 0, 20, 11); ringCover.setBackground(Color.yellow); theRing.add( ringCover, 0 ); centerCover = new Rectangle(30, 0, 20, 11); centerCover.setBackground(Color.yellow); ringCenter.add( centerCover, 0 ); }...

© 2006 Pearson Addison-Wesley. All rights reserved To fix the code... public Driver() { win = new JFrame("Ring Stack"); win.setBounds(10, 10, 500, 400); win.setLayout(null); win.setVisible(true); win.setBackground(Color.black); pole = new Rectangle(240, 80, 20, 200); pole.setBackground(Color.yellow); win.add( pole, 0 ); poleTop = new Oval(240, 77, 20, 6); poleTop.setBackground(Color.orange); win.add( poleTop, 0 ); poleBottom = new Oval(240, 277, 20, 6); poleBottom.setBackground(Color.yellow); win.add( poleBottom, 0 ); makeRing(170, Color.blue); makeRing(150, Color.red); makeRing(130, Color.green); win.repaint(); } public Driver() { win = new JFrame("Ring Stack"); win.setBounds(10, 10, 500, 400); win.setLayout(null); win.setVisible(true); win.setBackground(Color.black); pole = new Rectangle(240, 80, 20, 200); pole.setBackground(Color.yellow); win.add( pole, 0 ); poleTop = new Oval(240, 77, 20, 6); poleTop.setBackground(Color.orange); win.add( poleTop, 0 ); poleBottom = new Oval(240, 277, 20, 6); poleBottom.setBackground(Color.yellow); win.add( poleBottom, 0 ); makeRing(170, Color.blue); makeRing(150, Color.red); makeRing(130, Color.green); win.repaint(); }