Guide to materials (SM = sample midterm)
Writing and using static methods
Text coverage: Basics and template pg. 143 (return void) and 152 (return a quantity), then examples show up in every chapter. Their declarations use the “static” keyword, and they can be called on their own, no object reference needed (unlike object methods).
hw1 #4 season method, #5 average, #7 printLetters, #8 contains, #9 startEndSame
Hw2: #3 swapPairs, #9 addToXTwice
p1 #1 priceIsRight, #2 addBigInts, #3 PrintPoints (of p1 solution, should be named printPoints to follow the usual conventions)
SM #3, #7
Then we turned attention to object methods (AKA instance methods), but static methods are still crucial! Every program starts out in a static method, main, and simple programs can just use that plus one or more static methods to help do the work.
Program in text: NumberGuess3.java, pg. 354 has static methods main, giveIntro, matches, getGuess, and getInt. We wrote down an API for it in the class exercise for class 2, Jan 31.
Scanner, line-by-line processing, Scanner for String: Chap. 6 to and including Sec. 6.3.
Hw1 #10, hw3 #1
P1 #2, #3, P2 #4
SM: not covered, but may be on real midterm exam
Program in text: HoursWorked2.java, pg. 400, has static methods main and processLine. Related class handout: handout for class of Feb 12.
Arrays of ints, doubles, Strings, etc: Chap. 7 to pg. 469
Hw1 #4, #12, hw2: #1, #2, #3
p1 #1, #2
SM #3
Program in text: Temperature2, pg. 436, has only static main method. Related class exercise: class 3 exercise.
Objects: Chap 8 to pg. 532, and
Arrays of Objects (Chap 7 pg. 469-470, plus examples in Chap. 8)
Here we start using object methods: no “static” keyword in their declaration, always called using an object reference.
Hw2: #4, #5, #6, #9, #10, Hw3: #2-#8
P1 #3, all of p2. Related class exercise on Point objects for p2: for class of Feb. 14.
SM #1, #2, #4
Class exercise on PhoneEntry object: class of Feb. 21
Class exercise on Automobile: class of Feb. 26
APIs handout summarizing Point, PhoneEntry, Automobile: linked after class of Feb. 26
Program in text: PointMain.java and Point.java, pg. 530. PointMain has static main, while Point has object methods distanceFromOrigin, getX, etc.
Template in text: pg. 530 for an object class.
Interfaces: Sec. 9.5
Hw3: #9, #10, #11, #12
SM #5, #6
Class exercise on Interfaces: class of Mar. 26
Program in text: ShapesMain, pg. 601, with just static main, using interface Shape.java, and classes Circle.java, Rectangle.java, Triangle.java, all with object methods getArea and getPerimeter, as required by the fact that they each “implements Shape”.
Templates in text: pg. 597 for declaration of an interface, pg 598 for declaring that a class implements an interface.
Note: The sample midterm solution and hw3 solution will be posted by Sunday. Solutions are already available for hw1, hw2, p1, and p2.
Note: For help on the practice midterm, go to our Wiki where a discussion page has been set up. To reach the Wiki, browse to our class web page at www.cs.umb.edu/it115 (or /cs115) and follow the link at the end of that page.
Writing a static
method
A static method accepts some information by its arguments, works on it, and returns one thing as a return value, or possibly nothing (return void).
So there are inputs and one output, or nothing at all.
Need to identify the input(s) and output and put those things in the right place.
Example: Write a method that finds the sum of an int array.
Input: int array
Output: sum of ints, so one int.
Layout: template, pg. 152
Public static int sumArray(int[] a) {
…
}
Calling a static
method
Int a[] myNums = { 1, 3,4};
Int sum = sumArray(myNums);
Writing an object
method
Well, this is not done in a vacuum like the static case. We design an object and its “behavior” involves one or more object methods.
Designing an object, and understanding object code.
An object has fields, constructors, and object methods. The simple case is fields, one constructor that specifies the fields, and methods. See template pg. 530.
The Point example right there does have two constructors, but the second one is the classic case of setting both fields.
Fields: x,y. Each Point object has its own x and y values.
Then there are two getters for the fields x and y. Very common but not required.
But no setters for individual x and y—OK, should set them together. setLocation and translate are mutators.
You see the object method code can directly use the fields. So the variables available to the code at the start of an object method are the arguments and also the fields.
So in distanceFromOrigin, we see Math.sqrt( x*x + y*y), using the fields, there are no args here. We are calculating the distance for this point, and returning it.
In translate, we have args dx and dy, and also access to x and y. We could code this
public void translate(int dx, int dy)
{
x = x + dx; // or x += dx; ßusing both arg variables
and fields in the code
y = y + dy;
}
But in the book, you see setLocation(x + dx, y + dy); another way, and showing it’s easy to call another object method while in the code of an object method.
You should be able to modify one of our examples. Last time we added Color to a point. We could do this without the interface involved, but just adding a Color field to Point. But let’s use a more familiar type.
Example: Labeled points. We can add a String label to each Point, so we could have a label “home” for (1,2) and “office” for (100,23). How do you do this?
Add a field “private String label”, add an arg to the constructor, add a getter—that’s the minimal job.
Interpreting
a totally new object class. RaceCar on sample midterm.
Look at TimeSpan on pg. 537 as example, but drop “throw…”
Top level analysis
· One field
· One constructor with two args, for convenience of caller
· Two methods: one mutator, one accessor
Create one
Call method on it
Print it out.