University of British Columbia Midterm Format Clarification Reminder: Lab Schedule Change Recap: Formal vs. Actual Parameters CPSC 111, Intro to Computation ■ formal parameter: in declaration of class 2009W2: Jan-Apr 2010 ■ you do not need to memorize APIs ■ no labs next week Feb 8-12 ■ actual parameter: passed in when method is called ■ we will provide javadoc APIs for any classes or ■ TAs will hold office hours in labs during Monday lab Tamara Munzner ■ variable names may or may not match methods you need to write/debug code in the exam times to answer pre-midterm questions ■ if parameter is primitive type ■ Mon Feb 8 11am - 3pm ICICS 008 ■ call by value: value of actual parameter copied into ■ labs resume after break formal parameter when method is called Mathematical Operators, Static Methods ■ staggered to ensure that even Monday morning labs ■ changes made to formal parameter inside method have seen material in previous week's lecture body will not be reflected in actual parameter value Lecture 14, Fri Feb 5 2010 outside of method ■ if parameter is object: covered later borrowing from slides by Kurt Eiselt http://www.cs.ubc.ca/~tmm/courses/111-10 1 2 3 4 Recap: javadoc Comments javadoc Method Comment Example javadoc Class Comment Example Recap: Scope ■ Specific format for method and class header comments ■ Fields of class are have class scope: accessible to /** /** Die: simulate rolling a die ■ running javadoc program will automatically generate HTML Sets the die shape, thus the range of values it can roll. * @author: CPSC 111, Section 206, Spring 05-06 any class member documentation @param numSides the number of sides of the die * @version: Jan 31, 2006 ■ in Die and Point class implementation, fields */ * ■ Rules accessed by all class methods public void setSides(int numSides) { * This is the final Die code. We started on Jan 24, ■ /** to start, first sentence used for method summary sides = numSides; * tested and improved in on Jan 26, and did a final ■ Parameters of method and any variables declared ■ @param tag for parameter name and explanation } * cleanup pass on Jan 31. within body of method have local scope: accessible */ ■ @return tag for return value explanation only to that method /** ■ other tags: @author , @version Gets the number of sides of the die. ■ not to any other part of your code ■ */ to end @return the number of sides of the die ■ In general, scope of a variable is block of code */ ■ Running public int getSides() { within which it is declared % javadoc Die.java return sides; % javadoc *.java ■ block of code is defined by braces { } } 5 6 7 8 Cleanup Pass Key Topic Summary Increment and Decrement ■ Would we hand in our code as it stands? ■ Often want to increment or decrement by 1 ■ good use of whitespace? ■ obvious way to increment ■ Generalizing from something concrete ■ well commented? ■ count = count + 1; ■ fancy name: abstraction ■ every class, method, parameter, return value ■ assignment statement breakdown ■ clear, descriptive variable naming conventions? ■ Hiding the ugly guts from the outside ■ constants vs. variables or magic numbers? ■ retrieve value stored with variable count Mathematical Operators ■ fancy name: encapsulation ■ fields initialized? ■ add 1 to that value ■ Not letting one part ruin the other part ■ good structure? ■ store new sum back into same variable count ■ follows specification? ■ fancy name: modularity ■ obvious way to decrement ■ ideal: do as you go ■ Breaking down a problem ■ count = count - 1; ■ commenting first is a great idea! ■ fancy name: functional decomposition ■ acceptable: clean up before declaring victory 9 10 11 12 Shorthand Operators Shorthand Assignment Operators Data Conversion Data Conversion ■ Java shorthand ■ what value ends up assigned to total ? ■ Math in your head ■ Math in your head int total = 5; ■ count++; // same as count = count + 1; ■ 1/3 same as .33333333333333333…. ■ 1/3 same as .33333333333333333…. int current = 4; ■ count--; // same as count = count - 1; total *= current + 3; ■ Math in Java: it depends! ■ Math in Java: it depends! ■ note no whitespace between variable name and operator ■ remember that Java evaluates right before left of = int a = 1 / 3; int a = 1 / 3; // a is 0 ■ first right side is evaluated: result is 7 ■ Similar shorthand for assignment double b = 1 / 3; double b = 1 / 3; // b is 0.0 ■ total *= 7; ■ tigers += 5; // like tigers=tigers+5; ■ total = total * 7; int c = 1.0 / 3.0; int c = 1.0 / 3.0; // Java’s not happy ■ lions -= 3; // like lions=lions-3; ■ total = 5 * 7; ■ bunnies *= 2; // like bunnies=bunnies*2; double d = 1.0 / 3.0; double d = 1.0 / 3.0; // d is 0.333333333 ■ total = 35; ■ dinos /= 100; // like dinos=dinos/100; 13 14 15 16
Recommend
More recommend