05.ppt

advertisement
CS110 Lecture 5
Tuesday, February 10, 2004
• 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)
Lecture 5
1
hw2 (general programming) tips
•
•
•
•
•
•
•
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!
Lecture 5
2
new
• 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
Lecture 5
3
Bank constructor
•
•
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 }
Lecture 5
4
Linear Equations
• 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
Lecture 5
5
Temperature conversion application
Lecture 5
6
clients
• 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)
Lecture 5
7
LinearEquation API
• 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
Lecture 5
8
www.cs.umb.edu/joi/latest-joi-cd/
javadoc/2/linear/index.html
Lecture 5
9
javadoc
• Web documentation generated from source
• Special javadoc comment: /** … */
• LinearEquation.java line 53
/**
* Compute y, given x.
*
* @param x the input value.
* @return the corresponding value of y: mx+b.
*/
public double compute( double x )
{
// arithmetic here
}
Lecture 5
10
How Temperatures class uses
LinearEquation services
• 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 ) );
Lecture 5
11
Pseudocode for PhoneBill
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
Lecture 5
12
Testing
• 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
Lecture 5
13
Testing a client class
• 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)
Lecture 5
14
Test cases known in advance
• 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
Lecture 5
15
Interactive Tests
• 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
Lecture 5
16
Incomplete testing …
• 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!
Lecture 5
17
Download