Download presentation
Presentation is loading. Please wait.
Published byMadlyn Sherman Modified over 9 years ago
1
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf, Assignment2 COMP 102 #7 2015T2
2
© Xiaoying Gao, Peter Andreae COMP102 7:2 Admin Assignment 2 due Wed 10am Only submit the.java files and reflection.txt 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 Forum Optional Tutorials: Tuesday 12-1, CO219 Optional help desk: Tuesday 1-2, CO242B More help for all 1 st year ECS students Mon, Wed and Fri 11am-12 in CO242B Mon 5pm-7pm in AM103, Wed 5pm-7pm in CO118
3
© Xiaoying Gao, Peter Andreae COMP102 7:3 Menu Exercises: Writing a program in lecture printf: printing text with format control Assignment 2 Another example if time
4
© Xiaoying Gao, Peter Andreae COMP102 7:4 An exercise 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
5
© Xiaoying Gao, Peter Andreae COMP102 7:5 Password Example
6
© Xiaoying Gao, Peter Andreae COMP102 7:6 Methods in the library Some methods just have "side effects": UI.println("Hello there!"); 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);
7
© Xiaoying Gao, Peter Andreae COMP102 7:7 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)
8
© Peter Andreae COMP102 6:8 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);
9
© Xiaoying Gao, Peter Andreae COMP102 7:9 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"
10
© Xiaoying Gao, Peter Andreae COMP102 7:10 Assignment 2 One class constants one or more methods Input from user: call UI methods Calculation/Processing: Variables: type (double or int), name Assignments Expressions Output: call UI methods to text to graphics
11
© Peter Andreae COMP102 6:11 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!
12
© Xiaoying Gao, Peter Andreae COMP102 7:12 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?
13
© Xiaoying Gao, Peter Andreae COMP102 7:13 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.
14
© Xiaoying Gao, Peter Andreae COMP102 7:14 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
15
© Xiaoying Gao, Peter Andreae COMP102 7:15 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
16
© Xiaoying Gao, Peter Andreae COMP102 7:16 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
17
© Xiaoying Gao, Peter Andreae COMP102 7:17 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.