1 / 25

CSCI S-1 Section 8

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;

lada
Download Presentation

CSCI S-1 Section 8

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. CSCI S-1Section 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!"); 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)); }

  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. introllDie() public static introllDie() { //return an int in the range 1-6 incl. }

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

  9. 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); }

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

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

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

  13. 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) { }

  14. 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; }

  15. 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) { }

  16. 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')); }

  17. 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) { }

  18. 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')); }

  19. 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 }

  20. 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); }

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

  22. 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)); }

  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 (inti = 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( <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);

More Related