Presentation is loading. Please wait.

Presentation is loading. Please wait.

Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,

Similar presentations


Presentation on theme: "Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,"— Presentation transcript:

1 Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method, printf COMP 102 #7 2014T2

2 © Xiaoying Gao, Peter Andreae COMP102 7:2 Menu Review Assignment2 printf: printing text with format control Administrivia: Assignment 2 due Wed 11am Only submit the.java files Extension: Email xgao@ecs.vuw.ac.nz and CC zarinah.amin@ecs.vuw.ac.nz Getting online help: comp102-help@ecs.vuw.ac.nzcomp102-help@ecs.vuw.ac.nz Smaller labs: Mon 4-5, Wed 9-10, Thur 4-5 Optional help desk: Tuesday 4-5, CO242B Optional Tutorials: Friday 10-11, CO219

3 © Xiaoying Gao, Peter Andreae COMP102 7:3 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 ID Return the password

4 © Xiaoying Gao, Peter Andreae COMP102 7:4 Password Example import ecs100.*; public class PasswordEx{ public void method1(){ String name = UI.askString("name: "); int ID = UI.askInt("ID: "); String p = this.method2(name, ID); UI.println(name + “ password : "+ p); } public String method2(String name, int ID){ String pw = name.substring(0,1) +ID; return pw; }

5 © Xiaoying Gao, Peter Andreae COMP102 7:5 Methods in the library 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);

6 © Xiaoying Gao, Peter Andreae COMP102 7:6 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)

7 © Xiaoying Gao, Peter Andreae COMP102 7:7 Assignment 2 One class Constants one or more methods Input from user: Call methods Calculation/Processing: Variables: type (double or int), name Assignments Expressions Output: Call methods to text to graphics

8 © Peter Andreae COMP102 6:8 Computing the Zone If distance is 188: zone is 1(0-199) If distance is 334: zone is 2(200-399) If distance is 900: zone is 5(800-999) How do you convert from distance to the zone? int distance = UI.askInt(“How far are you sending it (kilometers)”); int zone = By using int, not double, we force integer division!

9 © Peter Andreae COMP102 6:9 Calculators: formatting output Calculator methods printed out numbers horribly: … UI.println(miles + “ miles is ” + kilometres + “ km”); … Printed it out like 45 miles is 72.42050240516535 km How do you control the format of the output? use printf(….) not println(…..) UI.printf(“%4.2f miles is %4.2f km\n”, miles, kilometers);

10 © Xiaoying Gao, Peter Andreae COMP102 7:10 UI.printf Printing with a format string: printf(String format, …  arguments  ) UI.printf("%4.2f miles is the same as %4.2f km \n", mile, km); UI.printf(“%s bought %d items and the cost is $%.2f \n", name, num, cost); Holes all of the form %  flags   character  %s means print argument as a string %d means print argument as an integer %3d means print argument as an integer, padded to 3 characters %f means print argument as a floating point number %5.2f means print argument as floating point, with 2 decimal places, padded to at least 5 characters a string with "holes" values to fill each "hole"

11 © Xiaoying Gao, Peter Andreae COMP102 7:11 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? Two methods, one calls the other what name? what parameters? what actions?

12 © Xiaoying Gao, Peter Andreae COMP102 7:12 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. ????????

13 © Xiaoying Gao, Peter Andreae COMP102 7:13 What happens if we call the method: RTM1. askQuestions(); public void askQuestions(){ this.measureTime("John Quay is the Prime Minister"); this.measureTime("6 x 4 = 23"); this.measureTime("Summer is warmer than Winter"); this.measureTime("The population of Wellington is over 1,000,000"); The object the method was called on is copied to "this" place Understanding ReactionTimeMeasurer this: RTM- 1

14 © Xiaoying Gao, Peter Andreae COMP102 7:14 public void 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) ); } "John Quay is … " Understanding method calls " this: RTM- 1

15 © Xiaoying Gao, Peter Andreae COMP102 7:15 public void askQuestions(){ this.measureTime("John Quay is the Prime Minister"); this.measureTime("6 x 4 = 23"); this.measureTime("Summer is warmer than Winter"); this.measureTime("The population of Wellington is over 1,000,000"); Understanding ReactionTimeMeasurer this: RTM-1

16 © Xiaoying Gao, Peter Andreae COMP102 7:16 New measureTime worksheet: public void 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) ); } Each time you call a method, it makes a fresh copy of the worksheet! " 6 x 9 = 54 " Understanding ReactionTimeMeasurer " this: RTM- 1

17 © Xiaoying Gao, Peter Andreae COMP102 7:17 public void askQuestions(){ this.measureTime("John Quay is the Prime Minister"); this.measureTime("6 x 4 = 23"); this.measureTime("Summer is warmer than Winter"); this.measureTime("The population of Wellington is over 1,000,000"); Understanding ReactionTimeMeasurer this: RTM-1

18 © Xiaoying Gao, Peter Andreae COMP102 7:18 Defining methods to return values 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.

19 © Xiaoying Gao, Peter Andreae COMP102 7:19 Defining methods to return values 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) ; New kind of statement Means: exit the method and return the value The value must be of the right type

20 © Xiaoying Gao, Peter Andreae COMP102 7:20 What happens if we call the method: RTM-1. askQuestions(); public void askQuestions(){ long time = this.measureTime("John Quay is the Prime Minister"); time = time + this.measureTime("6 x 4 = 23"); time = time + this.measureTime("Summer is warmer than Winter"); time = time + this.measureTime("Population of Wtgn is 1,000,000"); Returning values. this: RTM-1

21 © Xiaoying Gao, Peter Andreae COMP102 7:21 Returning values return value: public long measureTime(String fact){ long startTime = System.currentTimeMillis(); String ans = UI.askString("Is it true that " + fact); long endTime = System.currentTimeMillis(); return (endTime - startTime) ; } "John Quay is … " " this: RTM- 1

22 © Xiaoying Gao, Peter Andreae COMP102 7:22 What happens if we call the method: RTM-1. askQuestions(); public void askQuestions(){ long time = this.measureTime("John Quay is the Prime Minister"); time = time + this.measureTime("6 x 4 = 23"); time = time + this.measureTime("Summer is warmer than Winter"); time = time + this.measureTime("Population of Wtgn is 1,000,000"); Returning values. this: RTM-1


Download ppt "Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,"

Similar presentations


Ads by Google