Methods - Methods import java.util.*; // Needed to use...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Methods import java.util.*; // Needed to use Scanner for input public class Methods { /** Method header * * Format: <modifiers> <return type> name ( <arguments> ) * * where * * <modifiers> can be public/private/protected and/or static * <return type> can be any variable type or void * <arguments> is 0 or more (type, name) pairs */ // Simplest method: no input, no return value static void noInputNoReturn () { System.out.println("Console output is a side effect."); } // input, but no return value // a and b are method arguments -- they hold copies of // the values sent to this method static void multiply(int a, int b) { int product = a * b; System.out.println(a + " * " + b + " = " + product); } public static void main (String args) { /** Create Scanner to read from the keyboard * * sc is a variable that holds a Scanner * "System.in" refers to the keyboard * * sc holds an address; we don't know where the actual * Scanner is in memory, so we have to use sc as a signpost */ Scanner sc = new Scanner(System.in); /** Read info from Scanner's source (the keyboard) * * Scanner has methods like nextInt(), which reads an * integer value from its source * * Scanner always reads in strict first-come, first-served * order from its source */ System.out.print("Enter a value: "); int temp = sc.nextInt(); // read int from the keyboard sc.nextLine(); // consume extraneous Enter character System.out.print("Enter another value: "); int temp2 = sc.nextInt(); // read next value as int multiply(temp, temp2); } } Methods import java.util.*; // Needed to use Scanner for input public class Methods { /** Method header * * Format: <modifiers> <return type> name ( <arguments> ) * * where * * <modifiers> can be public/private/protected and/or static * <return type> can be any variable type or void * <arguments> is 0 or more (type, name) pairs */ // Simplest method: no input, no return value static void noInputNoReturn () { System.out.println("Console output is a side effect."); } // input, but no return value // a and b are method arguments -- they hold copies of // the values sent to this method static void multiply(int a, int b) { int product = a * b; System.out.println(a + " * " + b + " = " + product); } /** * This example method returns a fixed value */ public static int getZero () { return 0; } /** * This method takes input, returns a (single) value */ public static double average (int a, int b, int c) { // Return average of three integers as a double //return (a + b + c) / 3.0; // force promotion before calculation return (double)(a + b + c) / 3.0; // alt: (double) ((a + b + c) / 3.0) // alt: ((double)a + b + c) / 3.0 } public static void main (String args) { /** Create Scanner to read from the keyboard * * sc is a variable that holds a Scanner * "System.in" refers to the keyboard * * sc holds an address; we don't know where the actual * Scanner is in memory, so we have to use sc as a signpost */ Scanner sc = new Scanner(System.in); /** Read info from Scanner's source (the keyboard)...
View Full Document

This note was uploaded on 04/23/2010 for the course CDE 456 taught by Professor John during the Spring '10 term at Algoma University.

Page1 / 24

Methods - Methods import java.util.*; // Needed to use...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online