1 fencepost loops suggested reading:4.1. 2 the fencepost problem problem: write a static method...

Download 1 Fencepost loops suggested reading:4.1. 2 The fencepost problem Problem: Write a static method named printNumbers that prints each number from 1 to a

Post on 13-Dec-2015

212 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Slide 1

1 Fencepost loops suggested reading:4.1 Slide 2 2 The fencepost problem Problem: Write a static method named printNumbers that prints each number from 1 to a given maximum, separated by commas. For example, the method call: printNumbers(5) should print: 1, 2, 3, 4, 5 Let's write a solution to this problem... Slide 3 3 Flawed solutions public static void printNumbers(int max) { for (int i = 1; i1 can say s.toUpperCase() --> "H" can say s.charAt(0) --> 'h' What is s + 1 ? What is c + 1 ? What is s + s ? What is c + c ? Slide 16 16 Text processing questions Write a method named pigLatinWord that accepts a String as a parameter and outputs that word in simplified Pig Latin, by placing the word's first letter at the end followed by the suffix ay. pigLatinWord("hello") prints ello-hay pigLatinWord("goodbye") prints oodbye-gay Write methods named encode and decode that accept a String as a parameter and outputs that String with each of its letters increased or decreased by 1. encode("hello") prints ifmmp decode("ifmmp") prints hello Slide 17 17 Text processing question Write a method printName that accepts a full name as a parameter, and prints the last name followed by a comma, followed by the first name and middle initial. For example, printName("James Tiberius Kirk"); would output: Kirk, James T. Method nameDescription charAt(index)character at a specific index indexOf(String)index where the start of the given String appears in this String (-1 if it is not there) length()number of characters in this String substring(index1)characters from index1 to end of String substring(index1, index2)characters from index1 to just before index2 toLowerCase()a new String with all lowercase letters toUpperCase()a new String with all uppercase letters Slide 18 18 Methods with if/else suggested reading:4.5 Slide 19 19 if/else with return Methods can be written to return different values under different conditions using if/else statements: public static int min(int a, int b) { if (a > b) { return a; } else { return b; } An example that maps chess board squares to colors: public static Color chessBoardColor(int row, int column) { if ((row + column) % 2 == 0) { return Color.WHITE; } else { return Color.BLACK; } Slide 20 20 More examples Another example that returns the first word in a String: public static String firstWord(String s) { int index = s.indexOf(" "); if (index >= 0) { return s.substring(0, index); } else { // only one word in String return s; } It is an error not to return a value in every path: public static int min(int a, int b) { if (a > b) { return a; } // Error; not all code paths return a value. // Vhat if b >= a ? } Slide 21 21 for loops with if/else return Methods with loops that return values must consider the case where the loop does not execute the return. public static int indexOf(String s, char c) { for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == c) { return i; } } // error; what if c does not occur in s? } A better version that returns -1 when c is not found: public static int indexOf(String s, char c) { for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == c) { return i; } } return -1; // not found } Slide 22 22 if/else return question Write a method named numUnique that accepts two integers as parameters and returns how many unique values were passed. For example, numUnique(3, 7) returns 2 because 3 and 7 are two unique numbers, but numUnique(4, 4) returns 1 because 4 and 4 only represent one unique number. Write a method named countFactors that returns the number of factors of an integer. For example, countFactors(60) returns 11 because 1, 2, 3, 4, 5, 6, 10, 15, 20, 30, and 60 are factors of 60. Modify the pigLatinWord and encode / decode methods seen previously so that they return their results rather than printing them. Slide 23 23 Method return question Write a program that prompts the user for a maximum integer and prints out a list of all prime numbers up to that maximum. Here is an example log of execution: Maximum number? 50 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 14 total primes

Recommended

View more >