1
|
- Announcements
- hw2 due Thursday when labs close
- hw1 returned today (answer posted soon)
- tutoring available (see course web page)
- Agenda
- questions
- new, constructors
- client classes; javadoc
- LinearEquation and Temperatures classes
- testing (time permitting)
|
2
|
- You can be less than perfect and still do well
- Write/think pseudocode before you write Java
- Submit partial solutions, park on downhill slope
- Use memo.txt to track problems/solutions
- If you get help please say so!
- Naming and prettyprinting conventions matter
- When UI is specified
- go for exact match (PhoneBill class)
- don’t do more than is asked for!
|
3
|
- Important Java keyword
- Use new to create an object: in Bank.java main
- Bank javaBank = new
Bank(“Engulf…”);
- Think of ordering a Bank from the Bank factory
- Invokes special method in Bank.java called a constructor
- Name of constructor is name of class
|
4
|
- sets value for bankName field
- creates a Terminal and two BankAccounts (invoking constructors in those
classes)
- 41 public Bank( String name )
- 42 {
- 43 bankName = name;
- 44 atm = new Terminal();
- 45 account1 = new BankAccount
- ( INITIAL_BALANCE );
- 46 account2 = new BankAccount
- ( INITIAL_BALANCE );
- 47 }
|
5
|
- Truck rental rate: $75 plus $1.15 per mile
- totalCost = 75.0 + 1.15 * milesDriven
- Trips by air take 2 hours at the airport and 1 hour for each 400 miles
flown:
- tripTime = 2.0 + (1.0/400.0)*flightMiles
- Temperature conversion:
- degreesF = (9.0/5.0)*degreesC +
32.0
- y = m*x + b // common abstraction
- Model linear equations as Java objects:
- write general class LinearEquation
- many applications can use its features
|
6
|
|
7
|
- Only method in Temperatures class is main
- No arithmetic in Temperatures.java
- class Temperatures is a client for LinearEquation, which provides services
- Bank is a client for Terminal and BankAccount
- The client knows only the API
- application programming interface
- whatever’s declared public in that class
- not the implementation (Java details)
|
8
|
- Two public constructors
- public LinearEquation( double m, double b )
- public LinearEquation( double x1, double y1,
- double x2,
double y2 )
- Two public methods
- public double compute( double x )
- public LinearEquation getInverse()
- To see how to use these services, look at the javadoc
|
9
|
|
10
|
- Web documentation generated from source
- Special javadoc comment: /** … */
- LinearEquation.java line 53
|
11
|
- construct a LinearEquation with new
(line 25)
- 25 LinearEquation c2f =
- new LinearEquation( 9.0/5.0,
32.0 );
- (carefully use 9.0/5.0 to avoid integer division!)
- send a getInverse message to create another
- 28 LinearEquation f2c = c2f.getInverse();
- send compute messages (lines 36,
38 , not 35, 37 )
- 35 terminal.print(
"c2f.compute( 0.0 ), should see 32.0: ");
- 36 terminal.println( c2f.compute(
0.0 ) );
|
12
|
- main
- create a Terminal
- get basic rate, message unit
cost from user
- create LinearEquation objects
you need
- get number of message units
from user
- compute cost, tell user
- get amount willing to spend
from user
- compute number of message
units, tell user
- Use Temperatures.java as a template –
copy the ideas there, or edit a copy of the file, changing what’s necessary
|
13
|
- Important
- Psychologically difficult
- you don’t want to know if your code is broken
- when the programming is done you’d like to be done
- In industry there’s a QA department - consider asking a friend
- We test your code when we run it. We’re mean.
- Where is the error?
- in the code
- in the documentation
- in the specification
|
14
|
- Suppose you’ve written LinearEquation, and it compiles correctly.
- How can you know it’s right?
- Need a test driver: main somewhere to test all the public methods in the
client
- Temperatures class tests LinearEquation
- (Terminal has its own main for testing – try it)
|
15
|
- First part of Temperatures main
- Tests hard coded (part of program, known at compile time) - no user
input
- Output echoes input (self documenting)
- Test cases provided by programmer or specification
|
16
|
- Second part of Temperatures main
- Programmer doesn’t know test cases in advance (at compile time)
- Input provided at run time by user (in CS110 we use Terminal
for this)
- No need for output to echo input
|
17
|
- Temperatures does not test LinearEquation thoroughly enough
- second constructor (line through two points) never used
- no stress testing (hard cases – big numbers, negative numbers)
- Our grading scripts will test all of your code – so you should do it
first!
|