Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Graphical Output,
MiniDraw Testing COMP 102 # T1
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
JAVA LIBRARY CLASSES CITS Main concepts to be covered Using library classes: String, Math, Color Reading documentation Java 7 API is available.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
© Xiaoying Gao, Peter Andreae Class, method, statements COMP 102 #3 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
© Xiaoying Gao, Peter Andreae UI methods, Variables, Constants COMP 102 #4 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Create Objects,
©Xiaoying Gao, Peter Andreae First Java Program COMP 102 #2 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Agenda Object Oriented Programming Reading: Chapter 14.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Files COMP 102.
David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 103 #
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
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.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods Calling Methods Return.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington UI methods, Graphical.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
© Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Programs with Choice Booleans,
CSE 1030: Implementing Static Features Mark Shtern.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with While loops.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Fields, Constructors COMP 102 # T2
Yanal Alahmad Java Workshop Yanal Alahmad
FIGURE 4-10 Function Return Statements
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Recap Week 2 and 3.
Unit 3: Variables in Java
Presentation transcript:

Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with Parameters, methods return a value COMP 102 # T2

© Xiaoying Gao, Peter Andreae COMP102 6:2 Assignment 2 Template code uses Buttons The buttons are set up in a constructor (a special method)

© Xiaoying Gao, Peter Andreae COMP102 6:3 Menu Arguments and Parameters Methods that return a value

© Xiaoying Gao, Peter Andreae COMP102 6:4 A summary Class Constants Methods Statements Variables Assignment statements Method call statements Call a method in library UI.drawRect(20, 40, 100, 400); Call a method in the same class this.drawPicture(200, 100, 50);

© Xiaoying Gao, Peter Andreae COMP102 5:5 Version 3 import ecs100.*; import java.awt.Color; public class DrawShapes3{ public void drawMany(){ this.drawPicture(300, 100, 70); this.drawPicture(300,200, 100); } public void drawPicture(double x, double y, double d){ UI.setColor(Color.green); UI.fillRect(x-d,y-d/2,2*d,d); UI.setColor(Color.red); UI.fillOval(x-d/2, y-d/2, d, d); UI.setColor(Color.orange); UI.drawLine(x-2*d, y, x+2*d, y); UI.fillArc(x-d/2, y-d/2, d, d, 30, 120);} }

© Xiaoying Gao, Peter Andreae COMP102 6:6 Method with parameters Parameterising a method makes it more flexible and general Allows us to call the same method with different arguments to do the same thing in different ways Allows us to reuse the same bit of code this.drawPicture(100, 100, 40); this.drawPicture(300,200, 150);

© Xiaoying Gao, Peter Andreae COMP102 6:7 Arguments can be variables with values import ecs100.*; public class ParameterExample{ public void method1(){ int m = 1; int n = 5; this.method2(m, n+3); m = m + 1; this.method2(n, m); } public void method2(int x, int y){ UI.println(x + ", "+ y); UI.println("result: " + (x + y/2)); }

© Xiaoying Gao, Peter Andreae COMP102 6:8 Arguments and parameters Must match Type of data Number of data Order of data Do not need to match name

© Xiaoying Gao, Peter Andreae COMP102 6:9 Bad example, bad style, important concept import ecs100.*; public class ParameterEx2{ public void method1(){ int x = 5; int y = 10; this.method2(x, y); UI.println("method1, x: " + x + ", y: "+ y); this.method2(y, x); UI.println("method1, x: " + x +", y: "+ y); } public void method2(int x, int y){ UI.println("method2, x: " + x + ", y: " + y); x = x+1; y = y*y; UI.println("method 2, x is now:"+ x +", y is now:" + y); } }

© Xiaoying Gao, Peter Andreae COMP102 6:10 Local variables and parameters Local variables and parameters are only accessible within the same method The variables with the same name in different methods are completely separated things Data is past from one method to another by argument and parameter match Match by type, order, number Not by name (Two or more methods to share the same data without using parameters: data fields) Later in the course

© Xiaoying Gao, Peter Andreae COMP102 6:11 Method returns a value import ecs100.*; public class ReturnEx{ public void method1(){ double w = 10.0; double h = 2.0; double s = this.rectArea(w, h); UI.println("the area is " + s); } public double rectArea(double width, double height){ double area = width * height; return area; }

© Xiaoying Gao, Peter Andreae COMP102 6:12 Method returns a value import ecs100.*; public class ReturnEx2{ public void TestArea(){ double radius = 10.0; UI.println("the area is " + this.circleArea(radius)); } public double circleArea(double radius){ double area = Math.PI * Math.pow(radius,2); return area; }

© Xiaoying Gao, Peter Andreae COMP102 6:13 More examples import ecs100.*; public class ReturnEx4{ public void rectCheck(){ double w = 10.0; double h = 2.0; boolean q = this.isSquare(w, h); UI.println("is it a square? " + q); } public boolean isSquare(double width, double height){ return width==height; }

© Xiaoying Gao, Peter Andreae COMP102 7:14 Method Definitions (3) /** Method returns a value*/ public double rectArea(double width, double height){ double area = width * height; return area; } 〈 Comment 〉〈 Header 〉〈 Body 〉 {} public 〈 type 〉 〈 parameters 〉 () 〈 name 〉 〈 type 〉, 〈 name 〉

© Xiaoying Gao, Peter Andreae COMP102 6:15 An exercises Define a class with two methods First method Ask the user for a name Ask the user for an ID number Call the second method to generate a password based on name and ID Show the user his/her password Second method Generate a password using the first character of name and all digits in ID Return the password

© Xiaoying Gao, Peter Andreae COMP102 6:16 Password Example

© Xiaoying Gao, Peter Andreae COMP102 7:17 Methods that return values Some methods just have "side effects": UI.println("Hello there!"); UI.printf("%4.2f miles is the same as %4.2f km\n", mile, km); UI.fillRect(100, 100, wd, ht); UI.sleep(1000); Some methods just return a value: long now = System.currentTimeMillis(); double distance = 20 * Math.random(); double ans = Math.pow(3.5, 17.3); Some methods do both: double height = UI.askDouble("How tall are you"); Color col =JColorChooser.showDialog(null, "paintbrush", Color.red);

© Xiaoying Gao, Peter Andreae COMP102 7:18 Call a method objectName.methodName(arguments) Objects Predefined: UI, Math, System (they are in fact Class names) Default: this Arguments, data to use Check documentation (or your own code), to find out parameters Must match the type, order, number of parameters If the method returns a value Assign it to a variable (store it) Print it out (use it for output) Do nothing (throw it away)

© Xiaoying Gao, Peter Andreae COMP102 6:19 Another Java Program Design a Java program to measure reaction time of users responding to true and false "facts". Ask the user about a fact: "Is it true that the BE is a 4 Year degree?" Measure the time they took Print out how much time. Need a class what name? Need a method what name? what parameters? what actions?

© Xiaoying Gao, Peter Andreae COMP102 6:20 ReactionTimeMeasurer (Version 1) Need method that calls the measureTime method repeatedly public class ReactionTimeMeasurer { public void askFourQuestions() { this.measureTime("John Quay is the Prime Minister"); this.measureTime("11 x 13 = 143"); this.measureTime("Summer is warmer than Winter"); this.measureTime("Wellington has over 1,000,000 people"); } public void measureTime(String fact) { long startTime = System.currentTimeMillis(); String ans =UI.askString("Is it true that" + fact); long endTime = System.currentTimeMillis(); UI.println("You took " + (endTime - startTime) +" milliseconds"); } } this is the object that the current method was called on. ????????

© Xiaoying Gao, Peter Andreae COMP102 7:21 Defining methods to return values (v2) Improving ReactionTime: Make measureTime return the time, then add them all up and print out the average. public void askFourQuestions() { long time = this.measureTime("John Quay is the Prime Minister"); time = time + this.measureTime("11 x 13 = 143"); time = time + this.measureTime("Summer is warmer than Winter"); time = time + this.measureTime("Wellington has 1,000,000 people"); UI.printf("Average time = %d milliseconds\n", (time / 4)); } public void measureTime(String fact) { long startTime = System.currentTimeMillis(); …… } long Specifies the type of value returned. void means "no value returned" If measureTime returns a value instead of just printing it out.

© Xiaoying Gao, Peter Andreae COMP102 7:22 Defining methods to return values (v2) If you declare that a method returns a value, then the method body must return one! public long measureTime(String fact) { long startTime = System.currentTimeMillis(); String ans = UI.askString("Is it true that " + fact); long endTime = System.currentTimeMillis(); UI.printf("You took %d milliseconds\n", (endTime - startTime) ); } return (endTime - startTime) ; Return statement Means: exit the method and return the value The value must be of the right type