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

Slides:



Advertisements
Similar presentations
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Graphical Output,
Advertisements

MiniDraw Testing COMP 102 # T1
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 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.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
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 Review COMP 102 #
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.
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 Methods with Parameters COMP.
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.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
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.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 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 Designing with Classes and.
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.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
Fields, Constructors COMP 102 # T2
Yanal Alahmad Java Workshop Yanal Alahmad
Unit 3: Variables in Java
Functions, Part 2 of 3 Topics Functions That Return a Value
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 Menu Arguments and Parameters Methods that return a value Administrivia: Assignment 2 is due next Wed 11am. Lab booking is still open

© Xiaoying Gao, Peter Andreae COMP102 6:3 A summary Class Constants Methods Statements Variables Assignment statements Call another method

© Xiaoying Gao, Peter Andreae COMP102 5:4 Examples import ecs100.*; public class Triangle{ public void ManyTriangles(){ this.drawTri(100, 100, 40, 50, 200, 400); this.drawTri(300,200, 150, 100, 200, 200); } public void drawTri(double x1, double y1, double x2, double y2, double x3, double y3){ UI.drawLine(x1, y1, x2, y2); UI.drawLine(x2, y2, x3, y3); UI.drawLine(x3, y3, x1, y1); }

© Xiaoying Gao, Peter Andreae COMP102 6:5 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.drawTri(100, 100, 40, 50, 200, 400); this.drawTri(300,200, 150, 100, 200, 200);

© Xiaoying Gao, Peter Andreae COMP102 6:6 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:7 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:8 Bad example, bad style import ecs100.*; public class ParameterEx2{ public void method1(){ int x = 1; int y = 5; this.method2(x, y+3); x = x + 1; this.method2(y, x); } public void method2(int x, int y){ UI.println(x + ", "+ y); UI.println("result: " + (x + y/2)); }

© Xiaoying Gao, Peter Andreae COMP102 6:9 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:10 Method return 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:11 Method returns a value import ecs100.*; public class ReturnEx2{ public double circleArea(double radius){ double area = Math.PI * Math.pow(radius,2); return area; } public void TestArea(){ double radius = 10.0; UI.println("the area is " + this.circleArea(radius)); }

© Xiaoying Gao, Peter Andreae COMP102 7:12 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:13 An exercises Define a class with two methodes 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 ID Return the password

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

© Xiaoying Gao, Peter Andreae COMP102 7:15 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:16 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:17 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:18 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:19 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:20 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