Introduction to Programming Java Lab 8: Methods 1 March 2013 1 JavaLab8 lecture slides.ppt Ping...

11
Introduction to Programming Java Lab 8: Methods 1 March 2013 1 JavaLab8 lecture slides.ppt Ping Brennan ([email protected])

Transcript of Introduction to Programming Java Lab 8: Methods 1 March 2013 1 JavaLab8 lecture slides.ppt Ping...

Page 1: Introduction to Programming Java Lab 8: Methods 1 March 2013 1 JavaLab8 lecture slides.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk)

Introduction to Programming

Java Lab 8:Methods

1 March 2013 1JavaLab8 lecture slides.ppt

Ping Brennan ([email protected])

Page 2: Introduction to Programming Java Lab 8: Methods 1 March 2013 1 JavaLab8 lecture slides.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk)

Java Project

Project Name: JavaLab8

2

RepeatStringRepeatString

ReadDouble

Page 3: Introduction to Programming Java Lab 8: Methods 1 March 2013 1 JavaLab8 lecture slides.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk)

Method Declaration for cubeVolume (in JFE, part 5.2)

3

public static double cubeVolume(double sideLength){ double volume = sideLength * sideLength * sideLength; return volume; }

Type of return value

Method name

Type of parameter variable

Name of parameter variable

Passing a parameter

return statement exits method and returns volume.

Method body, executed when method is called.

Page 4: Introduction to Programming Java Lab 8: Methods 1 March 2013 1 JavaLab8 lecture slides.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk)

Class RepeatString

• Class RepeatString contains two methods:

– First method is the usual one, main– Second method repeat returns the string str

repeated n times. (See JFE, P5.5)

• Objectives

– Understand how to pass and return values in methods.

– Using methods to solve problems.

4

Page 5: Introduction to Programming Java Lab 8: Methods 1 March 2013 1 JavaLab8 lecture slides.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk)

Class RepeatString

• Method

public static String repeat(String str, int n)

5

Input

int n = 3; // first example

int n = 1; // second example Examples (in main):

String str1 = repeat("ho", 3);String str2 = repeat("ho", 1);

Loop

for statement

Print str1 repeated 3 times.

Print str2 repeated 1 time.

Output

hohoho

ho

Page 6: Introduction to Programming Java Lab 8: Methods 1 March 2013 1 JavaLab8 lecture slides.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk)

Anatomy of Class RepeatString

import java.util.Scanner;

public class RepeatString{

public static void main(String[] args){

String str1 = repeat("ho", 3); System.out.println("First example: " + str1);

/* To do: write more code to make a number of calls to the method repeat */

} // end of main

/* To do: insert code for the method repeat here (which is shown on next slide) */} // end of class RepeatString

6

Method name

Call to method repeat

Page 7: Introduction to Programming Java Lab 8: Methods 1 March 2013 1 JavaLab8 lecture slides.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk)

Method Declaration for repeat

7

public static String repeat(String str, int n) { String result = ""; /* To do: write a for statement to loop n times, each time joining the string, str, to current value in result. */

return result; }

Type of return value

Method name

Type of parameter variable

Name of parameter variable

Passing a parameter

return statement exits method and returns result.

Method body, executed when method is called.

Page 8: Introduction to Programming Java Lab 8: Methods 1 March 2013 1 JavaLab8 lecture slides.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk)

Class ReadDouble

• Displays a prompt string, followed by a space, and then reads in a number of type double.

• Objective– Construct a method to return a floating point

number after reading it inside the method when prompted.

8

Page 9: Introduction to Programming Java Lab 8: Methods 1 March 2013 1 JavaLab8 lecture slides.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk)

Class ReadDouble

• Method

public static double readDouble(String prompt)

• Usage in method main

double salary = readDouble("Please enter your salary: " ); // read in salary

9

InputA prompt string.double salary;

Example (in main): salary = readDouble("Please enter your salary: ");// Method displays prompt, then // reads in a floating point number,// 31000.50, and returns it.

ComputationsInside method readDouble:

double inSalary = in.nextDouble();

return inSalary;

PromptPlease enter your salary: 31000.50 (user’s input in red)

OutputThe entered salary is 31000.50

Page 10: Introduction to Programming Java Lab 8: Methods 1 March 2013 1 JavaLab8 lecture slides.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk)

Anatomy of Class ReadDouble

import java.util.Scanner;

public class ReadDouble{

public static void main(String[] args){

double salary = readDouble("Please enter your salary: ");

/* To do: write code to print out the salary */

/* To do: include a number of calls to the method readDouble, and print the results */

} // end of main

/* To do: insert code for the method readDouble here (which is shown on next slide) */

} // end of class ReadDouble10

Page 11: Introduction to Programming Java Lab 8: Methods 1 March 2013 1 JavaLab8 lecture slides.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk)

Method declaration for readDouble

public static double readDouble(String prompt) { Scanner in = new Scanner(System.in); System.out.print(prompt + " ");

/* To do: write code to declare a variable, inSalary,

of type double, and assign it the keyboard input.

Hint: use in.nextDouble() to read in a value. */ /* To do: lastly write code to return inSalary */ } // end of readDouble

11