250 likes | 353 Views
CSCI S-1 Section 8. Coming Soon. Problem Set Four (72 + 5/15 points) Tuesday, July 21, 17:00 EST. Why Isn’t This Nice?. public static void notSoNiceDice { System.out.println ("Your turn!"); int roll1 = ( int ) ( Math.random () * 6) + 1;
E N D
Coming Soon • Problem Set Four (72 + 5/15 points) • Tuesday, July 21, 17:00 EST
Why Isn’t This Nice? public static void notSoNiceDice { System.out.println("Your turn!"); introll1 = (int) (Math.random() * 6) + 1; System.out.println("You rolled a " + roll1 + "."); introll2 = (int) (Math.random() * 6) + 1; System.out.println("Then you rolled a " + roll2 + "!"); System.out.println("The product of your rolls was " + (roll1 * roll2)); System.out.println("My turn!"); introll3 = (int) (Math.random() * 6) + 1; System.out.println("I rolled a " + roll3 + "."); int roll4 = (int) (Math.random() * 6) + 1; System.out.println("Then I rolled a " + roll4 + "!"); System.out.println("The sum of my rolls was " + (roll3 + roll4)); }
It’s Repetitive, Repetitive, Repetitive!!! Four times we evaluate (int) (Math.random() * 6) + 1. Why might we want to make this a separate rollDie() method? • Easier to read: if method calls rollDie(), you know what's going on • Easier to change: modify and test a single method • Easier to reuse : call rollDie() from many board games!
The Familiar Void //void methods have “side effects” and can help modularize Public static void main (String[] args) { methodTwo() } public static void methodTwo() { methodOne(); } public static void methodOne() { System.out.println("Inside method one"); }
Non-Void Methods “Return” Values public static void what? To get results back from rollDie(), have it return a value Why can’t we just use global/class variables? • anything can access or change them • it can be hard to determine which method made the change The semantics of ‘return’ are not mysterious • int n = 3+4 //+ returns 7 • int n = sum(3,4) //sum returns 7
introllDie() public static introllDie() { //return an int in the range 1-6 incl. }
introllDie() public static introllDie() { //return an int in the range 1-6 incl. inti = (int) (Math.random()*6 + 1); return i; } public static introllDie() { //same, but in one line }
introllDie() public static introllDie() { //return an int in the range 1-6 incl. inti = (int) (Math.random()*6 + 1); return i; } public static introllDie() { //same, but in one line return (int) (Math.random()*6 + 1); }
Going to Town //some exciting things you can do with return values public static void main (String[] args) { System.out.println(rollDie()); //print it int answer = rollDie(); //assign it intsumOfTwoRolls = rollDie() + rollDie(); //evaluate it double rollsOverFive = rollDie() / 5.0; //slice and dice it }
Parameter Passing public static booleanisGeneralissimoFrancoStillDead() { return true; } what methods don’t always return the same value? • depend on user/keyboard input • depend on random expressions • depend on passed parameters
Parameter Passing //method to determine if a character is a digit Public static booleanisDigit(char ch) { //code to calculate and return goes here } //invoking method supplies value for ch //what does boolean mean here? booleanb = isDigit(‘2); //sample call
isDigit /** * Determines if the given character is a digit (betw `0' and `9'). * @paramch character to be tested * @return whether the character is a digit */ public static booleanisDigit(char ch) { }
isDigit /** * Determines if the given character is a digit (betw `0' and `9'). * @paramch character to be tested * @return whether the character is a digit */ public static booleanisDigit(char ch) { if ((ch >= `0') && (ch <= `9')) return true; else return false; }
isDigit concise //repeating the code from the last slide… public static booleanisDigit(char ch) { if ((ch >= `0') && (ch <= `9')) return true; else return false; } public static booleanisDigit(char ch) { }
isDigit concise //repeating the code from the last slide… public static booleanisDigit(char ch) { if ((ch >= `0') && (ch <= `9')) return true; else return false; } public static booleanisDigit(char ch) { return ((ch >= `0') && (ch <= `9')); }
isEvenDigit /** * Determines if the given character is an even digit * @paramch character to be tested * @return whether the character is an even digit */ public static booleanisEvenDigit(char ch) { }
isEvenDigit /** * Determines if the given character is an even digit * @paramch character to be tested * @return whether the character is an even digit */ public static booleanisEvenDigit(char ch) { return ((ch == `0') || (ch == `2') || (ch == `4') || (ch == `6') || (ch == `8')); }
isEvenDigit with indexOf /** * Determines if the given character is an even digit * @paramch character to be tested * @return whether the character is an even digit */ public static booleanisEvenDigit(char ch) { // string method indexOfreturns -1 when char not in string }
isEvenDigit with indexOf /** * Determines if the given character is an even digit * @paramch character to be tested * @return whether the character is an even digit */ public static booleanisEvenDigit(char ch) { // string method indexOf returns -1 when char not in string final String EVENDIGITS = "02468"; return (EVENDIGITS.indexOf(ch) != -1); }
isOddDigit //determines if the given character is an odd digit //define in terms of isDigit and isEvenDigit public static booleanisOddDigit(char ch) { }
isOddDigit //determines if the given character is an odd digit //define in terms of isDigit and isEvenDigit public static booleanisOddDigit(char ch) { return (isDigit(ch) && !isEvenDigit(ch)); }
Example //read String from keyboard and print out even digits in reverse public static void main(String[] args) { }
Example //read String from keyboard and print out even digits in reverse import java.util.*; public static void main(String[] args) { Scanner kbd = new Scanner(System.in); String input = kbd.nextLine(); for (inti = input.length() - 1; i >= 0; i--) if (isEvenDigit(input.charAt(i)) System.out.print(input.charAt(i)); System.out.println(); }
Summary Familiar method calls with parameters System.out print( <some string> ); charAt( <some number> ); Declare a method that takes parameters by putting the data type and variable name inside the parentheses public static double fahrenheitToKelvin(double fahr) {} Call a method that takes parameters by putting something with the right data type into the parentheses d = fahrenheitToKelvin(32.0);