Methods - 1 An Example of a Method private double...

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: 1 An Example of a Method private double feetToInches(double feet) { return 12 * feet; } Multiple return statements private int max(int val1, int val2) { if (val1 > val2) { return (val1); } else { return (val2); } } Predicate Methods private boolean isOdd(int x) { return ((x % 2) == 1); } Methods that Dont Return Anything private void printIntro() { println("Welcome to CS106A"); } Returning objects private GOval filledCircle(double x, double y, double r, Color color) { GOval circle = new GOval(x-r, y-r, 2*r, 2*r); circle.setFilled(true); circle.setColor(color); return circle; } A Full Program public class FactorialExample extends ConsoleProgram { private static final int MAX_NUM = 4; public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } } private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; } } 2 A Full Program public class FactorialExample extends ConsoleProgram { private static final int MAX_NUM = 4; public void run() { for(int i = 0; i < MAX_NUM; i ++) { println( i + "! = " + factorial( i )); } } private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i ++) { result *= i ; } return result; } } public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } } i public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } } i public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } } i public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } } i public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } } i 3 public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } } i private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; }...
View Full Document

This note was uploaded on 02/18/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Page1 / 9

Methods - 1 An Example of a Method private double...

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