180 likes | 214 Views
Writing Methods. AP Computer Science A. How to get started. Most of you can look at a method that has already been written and figure out what it does. However, you also have to know how to write code, because 50% if the test is implementing methods.
E N D
Writing Methods AP Computer Science A
How to get started • Most of you can look at a method that has already been written and figure out what it does. However, you also have to know how to write code, because 50% if the test is implementing methods. • The first thing you should do is look at the name of the class, private variables, and all methods. This will give you a general idea what kind of class it is.
Locate the class name, method, and private variables if any public void BankAccount { private int balance; public void addMoney( int deposit ){// implementation not shown} public int getBalance( ){// implementation not shown} public void print( ) {//implementation not shown} public int compareTo( BankAccount other ){//implementation not shown} } Usually you can figure out what the methods are for and what the variables are for, just by scanning through the class. The class is called BankAccount and most likely the variable balance is the amount of money a person has in the bank, the method addMoney, adds money to the account, and the print method probably prints the amount of money in the bank account. We’ve seen the compareTo method several times, and usually it returns -1, if this is less than other, 0 if this and other are the same, and 1 if this is greater than other.
Writing the addMoney method • When you write a method, make sure you read the preconditions and postcondition.( Not given in this example) • Look at the methods return type and also it’s parameters.( the addMoney method returns nothing, but takes an int as it’s parameters called deposit) • Locate any private variables, because you will usually need to use them within your method.( there is one private variable called balance of type int) • Locate all other methods, because you may need to use them within your method also.( the other methods are getBalance, print, and compareTo) What do you think the addMoney method will look like?
addMoney public void addMoney( int deposit ) { balance += deposit; } // That’s it. Remember this is not the only way. Here’s another way to do it. public void addMoney( int deposit ) { balance = balance + deposit; }
compareTo This one is not so obvious. Remember, we are comparing this to the parameter other. To compare two accounts we need to know their balance. So the first step is to get the balance of this and the balance of other. Well, since we have already scanned the class, we know there is a getBalance method, so we will call that method to get both balances. public int compareTo( BankAccount other ) { int b1 = this.getBalance( ); // get the balance of this int b2 = other.getBalance( ); // get the balance of other } Also, since the getBalance method returns an int, we store the value returned inside variables b1 and b2 which both have to be of type int.
compareTo continued Now that we have both balances, we need to compare them. Since they are of type int, we can use the operators <, > , and ==. public intcompareTo( BankAccount other ) { int b1 = this.getBalance( ); // get the balance of this int b2 = other.getBalance( ); // get the balance of other if( b1 < b2 ) // if b1 is less than b2 then return -1 return -1; if( b1 > b2 ) // if b1 is greater than b2 return 1 return 1; return 0; // if b1 is neither less than or greater than, then it is equal, so // return 0 } All of your compareTo methods look the same in general, that is they have the same basic algorithm. If you know the algorithm, then it’s just a matter of knowing what methods to call and what types your variables to should have.
Complete Question 1, findZero method, in the Workpacket-pg 164 • Remember if the directions are unclear to you, look at the examples given, it might clear up any confusion you may have. • Read the postcondition, to find out what to do. • If you don’t understand the precondition, don’t worry, you usually can answer the question without fully understanding the precondition. It usually just specifies assumptions about the value(s) of the parameter(s). • When you finish, compare your answer to the answer on the next slide. Remember, there is more than one way to solve a problem, so yours solution may look different. • The variable A is an array not ArrayList, so you don’t say A.get( 1 ), but A[1], and you don’t say A.size( ), but instead you say A.length, no parenthesis.
findZero-answer public static int findZero( int[ ] A, int pos) { for( int i = pos; i < A.length; i++ ) // set i to pos, not 0. Go thru the array { int temp = A[i]; // get each element in the array A if( temp = = 0 ) // if the current element is equal to 0 return i; // return the index } return -1; // Don’t forget to return -1, if there are no zeros }
Now complete Question 3 part A, page 160, the listToString method • Remember, you may need to use other methods you’ve already created inside this method. • A is an ArrayList so call the get method to access the elements inside A.( A.get( 1 ) or A.get( 2 ) or A.get( i )) • Also remember to find the number of elements in the ArrayList you say A.size( ), not A.length. • Once you write this method, check the solution on the next slide. • You can use the += operator with the String class. For example: String a = “ hi, “; a += “bye”. • A will have the value “hi, bye”, in the above example.
listToString public String( int start, int end ) { String temp = “ “; for( int i= start; i < end; i++ ) // i is initialized to start { temp += (String)A.get(i ); } return temp; } Notice that i is initialized to start, and the for loop will stop once i becomes equal to end.
printAllWords • Complete page 171 Part B. • Remember you can use any previous methods, like the one you just wrote for part a, or any method that is included in the class definition, to complete this method.
Huh? I don’t know what to do. • First of all, before you answer the question, make sure you scanned the class to get a sense of what methods and variables you can use. • We have a variable called A which is an ArrayList object. It contains single letters which are saved as Strings. • We have three methods listToString, printAllWords( the one you have the write ), and isWord. • Next read the postcondition, and look at the example given if it is unclear.
Come up with a plan Before you write some code, ask yourself how you would go about finding the answer by hand if you were asked to do so. “I” “N” “O” “T” “E”, suppose this is ArrayList A By hand you would start at I, since I is a word, print I, then got to IN, since IN is a word, print IN, then go to INO. Well INO is not a word don’t print it. Next check INOT, still not a word. Then check INOTE, still not a word. Were not done though, we now change the starting point to N. N is not a word, so then we check NO, NO is a word so print it. Then we check NOT, it is a word so print it. Then check NOTE, it is a word so print it. Now we do that all over again, but we start at O this time. O is not a word, so don’t print it. Then we check OT, not a word. Then we check OTE, not a word. So we move on to T as the starting point. T is not a word, so check TE, it’s not a word, so we move on to E. E is not a word, and we are finally done. Now , the challenge is how to we tell the computer to do this. Remember, the value of ArrayList A can be anything, not just “I” “N” “O” “T” “E”, so we cannot assume anything about the size of the ArrayList.
Implementing our plan “I” “N” “O” “T” “E” lets put indexes here 0 1 2 3 4 We will have to start at index 0. We will first check indexes 0, then 0,1, then 0,1,2, then 0,1,2,3, then 0, 1,2,3, 4. Then we will check 1, then 1, 2, then 1, 2, 3, then 1, 2, 3, 4, Then we will check 2, then 2, 3, then 2, 3, 4. Then we will check 3, then 3, 4. Then we will check 4. We will need a double for loop to this, using 0 as the starting point, and since we don’t know the size ahead of time, we will call the method A.size( ), so that we know when to stop.
printAllWords for( int start = 0; start < A.size( ); start++ ) { for( int end = 0; end < A.size( ); end++) { String word = listToString( start, end ); if( isWord( word ) ) System.out.println( word + “ “); } } System.out.println( ); // See the next side if you still don’t quite understand
printAllWords for( int start = 0; start < A.size( ); start++ ) //start a 0 to A.size, changes index of 1st letter { for( int end = 0; end < A.size( ); end++) // changes the index of the last letter { String word = listToString( start, end ); // put all the letters together // into one String, from index // start to index end if( isWord( word ) ) // check to see if this is a word System.out.println( word + “ “); // it it’s a word print it and a space } } System.out.println( ); // print a line at the end of all the words
That’s it • Many times it’s hard to start a problem, but the more practice you get, you will begin to realize that all problems are very similar. • Don’t memorize how to do a problem, instead learn how it works, and try to answer the same problem more than once if you needed help the first time. That’s how you will know if you really retained what you learned. • Remember to look at what methods you are given, because you will often have to use them inside your method. • Practice Makes Perfect.