Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI S-1 Section 8. Coming Soon Problem Set Four (72 + 5/15 points) – Tuesday, July 21, 17:00 EST.

Similar presentations


Presentation on theme: "CSCI S-1 Section 8. Coming Soon Problem Set Four (72 + 5/15 points) – Tuesday, July 21, 17:00 EST."— Presentation transcript:

1 CSCI S-1 Section 8

2 Coming Soon Problem Set Four (72 + 5/15 points) – Tuesday, July 21, 17:00 EST

3 Why Isn’t This Nice? public static void notSoNiceDice{ System.out.println("Your turn!"); int roll1 = (int) (Math.random() * 6) + 1; System.out.println("You rolled a " + roll1 + "."); int roll2 = (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!"); int roll3 = (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)); }

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

5 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"); }

6 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

7 int rollDie() public static int rollDie() {//return an int in the range 1-6 incl. }

8 int rollDie() public static int rollDie() {//return an int in the range 1-6 incl. int i = (int) (Math.random()*6 + 1); return i; } public static int rollDie() {//same, but in one line }

9 int rollDie() public static int rollDie() {//return an int in the range 1-6 incl. int i = (int) (Math.random()*6 + 1); return i; } public static int rollDie() {//same, but in one line return (int) (Math.random()*6 + 1); }

10 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 int sumOfTwoRolls = rollDie() + rollDie();//evaluate it double rollsOverFive = rollDie() / 5.0;//slice and dice it }

11 Parameter Passing public static boolean isGeneralissimoFrancoStillDead() { return true; } what methods don’t always return the same value? depend on user/keyboard input depend on random expressions depend on passed parameters

12 Parameter Passing //method to determine if a character is a digit Public static boolean isDigit(char ch) { //code to calculate and return goes here } //invoking method supplies value for ch //what does boolean mean here? boolean b = isDigit(‘2);//sample call

13 isDigit /** * Determines if the given character is a digit (betw `0' and `9'). * @param ch character to be tested * @return whether the character is a digit */ public static boolean isDigit(char ch) { }

14 isDigit /** * Determines if the given character is a digit (betw `0' and `9'). * @param ch character to be tested * @return whether the character is a digit */ public static boolean isDigit(char ch) { if ((ch >= `0') && (ch <= `9')) return true; elsereturn false; }

15 isDigit concise //repeating the code from the last slide… public static boolean isDigit(char ch) { if ((ch >= `0') && (ch <= `9')) return true; elsereturn false; } public static boolean isDigit(char ch) { }

16 isDigit concise //repeating the code from the last slide… public static boolean isDigit(char ch) { if ((ch >= `0') && (ch <= `9')) return true; elsereturn false; } public static boolean isDigit(char ch) { return ((ch >= `0') && (ch <= `9')); }

17 isEvenDigit /** * Determines if the given character is an even digit * @param ch character to be tested * @return whether the character is an even digit */ public static boolean isEvenDigit(char ch) { }

18 isEvenDigit /** * Determines if the given character is an even digit * @param ch character to be tested * @return whether the character is an even digit */ public static boolean isEvenDigit(char ch) { return ((ch == `0') || (ch == `2') || (ch == `4') || (ch == `6') || (ch == `8')); }

19 isEvenDigit with indexOf /** * Determines if the given character is an even digit * @param ch character to be tested * @return whether the character is an even digit */ public static boolean isEvenDigit(char ch) { // string method indexOf returns -1 when char not in string }

20 isEvenDigit with indexOf /** * Determines if the given character is an even digit * @param ch character to be tested * @return whether the character is an even digit */ public static boolean isEvenDigit(char ch) { // string method indexOf returns -1 when char not in string final String EVENDIGITS = "02468"; return (EVENDIGITS.indexOf(ch) != -1); }

21 isOddDigit //determines if the given character is an odd digit //define in terms of isDigit and isEvenDigit public static boolean isOddDigit(char ch) { }

22 isOddDigit //determines if the given character is an odd digit //define in terms of isDigit and isEvenDigit public static boolean isOddDigit(char ch) { return (isDigit(ch) && !isEvenDigit(ch)); }

23 Example //read String from keyboard and print out even digits in reverse public static void main(String[] args) { }

24 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 (int i = input.length() - 1; i >= 0; i--) if (isEvenDigit(input.charAt(i)) System.out.print(input.charAt(i)); System.out.println(); }

25 Summary Familiar method calls with parameters System.out print( ); charAt( ); 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);


Download ppt "CSCI S-1 Section 8. Coming Soon Problem Set Four (72 + 5/15 points) – Tuesday, July 21, 17:00 EST."

Similar presentations


Ads by Google