1 / 11

Introduction to Programming

Introduction to Programming. Java Lab 8: Methods. JavaLab8 lecture slides.ppt Ping Brennan ( Ping.Brennan@gmail.com ). 28 February 2014. Java Project. Project Name: JavaLab8. RepeatString. ReadDouble. Method Declaration for cubeVolume (in JFE, part 5.2). Passing a parameter.

guy-william
Download Presentation

Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming Java Lab 8: Methods JavaLab8 lecture slides.ppt Ping Brennan (Ping.Brennan@gmail.com) 28 February 2014

  2. Java Project Project Name: JavaLab8 RepeatString ReadDouble

  3. Method Declaration for cubeVolume (in JFE, part 5.2) Passing a parameter Type of return value Type of parameter variable Method name Name of parameter variable public static doublecubeVolume(doublesideLength) { double volume = sideLength* sideLength* sideLength; returnvolume; } Method body, executed when method is called. return statement exits method and returns volume.

  4. 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.

  5. Class RepeatString • Method public static String repeat(String str, int n) Input int n = 3; // first example int n = 1; // second example Examples (in method 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

  6. Anatomy of Class RepeatString Method name import java.util.Scanner; public class RepeatString { publicstaticvoidmain(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 slide 7) */ } // end of class RepeatString Call to method repeat

  7. Method Declaration for repeat Passing a parameter Type of return value Type of parameter variable Method name Name of parameter variable public static Stringrepeat(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. */ returnresult; } Method body, executed when method is called. return statement exits method and returns result.

  8. 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.

  9. Class ReadDouble • Method public staticdoublereadDouble(String prompt) • Usage in method main double salary = readDouble("Please enter your salary: " ); // read in salary Input A prompt string. double salary; Example (in method main): salary = readDouble("Please enter your salary: "); // Method displays prompt, then // reads in a floating point number, // 31000.50, and returns it. Computations Inside method readDouble: double inSalary = in.nextDouble(); return inSalary; Prompt Please enter your salary: 31000.50 (user’s input in green) Output The entered salary is 31000.50

  10. 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 the code to print out the salary */ /* To Do: write more code to 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 slide 11) */ } // end of class ReadDouble

  11. Method declaration for readDouble public static doublereadDouble(String prompt) { Scanner in = new Scanner(System.in); System.out.print(prompt + " "); /* To Do: write more 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: lastlywrite more code to return inSalary*/ } // end of readDouble

More Related