70 likes | 185 Views
Passing Other Objects. Strings are called immutable which means that once a String object stores a value, it never changes recall when we passed a message like toUpperCase( ) or replace(…) to a String that it returned a new String the original String did not change
E N D
Passing Other Objects • Strings are called immutable which means that once a String object stores a value, it never changes • recall when we passed a message like toUpperCase( ) or replace(…) to a String that it returned a new String • the original String did not change • String example = “Hi there”; • example.toUpperCase( ); -- this does not affect example, it merely returns “HI THERE” but example is still “Hi there” • example = example.toUpperCase( ); returns “HI THERE” and stores it in example, so now example will change • But if we pass a different type of object to a method, will it change? It depends on how the object is implemented and what we do to it in the method • arrays will change as shown in the example in the next slide
public class PassingArray { public static void main(String[] args) { int[ ] a = new int[10]; for(int i=0;i<10;i++) a[i]=i; printArray(a); changeArray(a); printArray(a); changeArray(a); printArray(a); } private static void changeArray(int[ ] x) { for(int i=0;i<10;i++) x[i]=x[i]+i; } private static void printArray(int[ ] x) { for(int i=0;i<10;i++) System.out.print(x[i]+" "); System.out.println("\n\n"); } } Passing and Changing Array Example Causes x to change, will a change in main?
Using Methods • Why use methods? It seems complicated? • Well first off, it isn’t really complicated, its just new – you will get used to it • Second, it really can help you design and implement a program by breaking your program into manageable pieces of code • Third, it becomes far easier to reuse code in different situations • So to motivate this, we will implement Hangman • What methods will Hangman need? • First, we need to pull out a random word from an array of words • if we are repeating the game in a loop, we want to make sure we don’t repeat a previous word, we can put the code to generate a unique word in a method and return the selected word as a String • We need to get the user’s guess • we will display the portion of the word already guessed • for instance “-a-a-a” if they have guessed ‘a’ and the word is “banana” • now we input their guess and verify that they input a letter (and optionally a letter which has not been guessed yet) • We then have to determine if the new guess is in the word or is a miss
Let’s Implement One of These Assume the word to be guessed is stored in the String word Assume the boolean array right indicates for each letter in word if the character has been guessed yet or not, for instance for banana, we would have the array {false, true, false, true, false, true} where ‘a’ has already been guessed Our method will receive the right array, the String word and the current guess public static int fillInRight(boolean[] right, String word, char guess) { int number=0; for(int i=0;i<word.length();i++) if(word.charAt(i)==guess) { right[i]=true; number++; } return number; }
A Simple Sort Method public static void sort(int a[], int n) { int temp; for(int i=0;i<n-1;i++) for(int j=0;j<n-1;j++) if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } • Sorting is one of the more common types of operations that a program will need • for a card game, we will want to sort the cards to be in ascending (or descending) order so that our methods that test the hand’s worth are easier to implement • there are several different sorting algorithms, what follows below is one of the two most simple versions, the bubble sort • can you figure out the logic?
Variations on Parameter Lists • If a method requires no parameters, the parameter list is ( ) in both the method call and in the method header • Another variation available in Java but not in many languages is that a method that can expect different numbers of or types of parameters • In this case, the person who has written the method has written several methods that share the same name, when you call the method, the actual method that is invoked depends on the parameters • this is known as method overloading • Some languages permit optional parameters, in Java this can be done by passing an array • We won’t be doing this in this camp, but that’s the idea behind (String[ ] args) in main which can receive 0 or more params
Example: Method Overloading • 3 findMaximum methods are available • Each expects a different type of parameter • int, char, double • Notice the methods are identical except for the types of parameters and the return type • in some situations, the method bodies may vary substantially public static double findMaximum (double x, double y) { if (x > y) return x; else return y; } public static int findMaximum(int x, int y) { if (x > y) return x; else return y; } public static char findMaximum (char x, char y) { if (x > y) return x; else return y; }