Prototyping with Methods

Slides:



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

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
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.
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.
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.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
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.
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)
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
© 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.
© 2006 Pearson Addison-Wesley. All rights reserved Syntax if (some_condition) { then_clause } Notes some_condition must be a valid boolean expression.
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.
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.
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.
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.
Lecture 15.1 Event Delegation.
CMSC 202 ArrayList Aug 9, 2007.
Functions + Overloading + Scope
Implementing Subprograms
Animating with EventTimer
How to Design Supplier Classes
Prototyping with Methods
Chapter 4 Procedural Methods.
CMSC 202 Static Methods.
Chapter 17 Linked Lists.
Chapter 19 Binary Search Trees.
Chapter 4 Inheritance.
Chapter 14 Graphs and Paths.
CMSC 202 ArrayList Aug 9, 2007.
CSE 115 September 29 – October 3, 2008.
Chapter 10 Datapath Subsystems.
CMSC 202 Java Primer 2.
Implementing Subprograms
Chapter 20 Hash Tables.
CMSC 202 ArrayList Aug 9, 2007.
CS139 October 11, 2004.
CSE 115 September 29 – October 3, 2008.
Chapter 7 Procedural Methods.
Implementing Subprograms
Classes and Objects Static Methods
The Facts to Be Explained
Classes, Objects and Methods
A simple function.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 4 Constructors Section 4.4
Circuit Characterization and Performance Estimation
Classes and Objects Object Creation
Corresponds with Chapter 5
Chapter 2 Reference Types.
Classes and Objects Systems Programming.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

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

A First Prototype public class Driver { private JFrame win; 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() { imports instance variables © 2006 Pearson Addison-Wesley. All rights reserved

Private Method (parameterless & void) private void MethodName ( ) { InstructionSequence } A private method is ____________ 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

private Oval poleTop, poleBottom, redRing, ringCenter; 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 ); } © 2006 Pearson Addison-Wesley. All rights reserved

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

makeRing(130, Color.green); makeRing(150, Color.red); 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 . . . makeRing(150, Color.red); 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(); } © 2006 Pearson Addison-Wesley. All rights reserved