Class Notes (1,200,000)
US (500,000)
Cornell (1,000)
CS (100)
CS 2110 (30)
Gries (30)
Lecture 3

CS 2110 Lecture Notes - Lecture 3: Junit, Precondition, Class Invariant


Department
Computer Science
Course Code
CS 2110
Professor
Gries
Lecture
3

This preview shows pages 1-2. to view the full 7 pages of the document.
Lecture 3 - More Objects and Stuff
We’e see puli lasses, ut there is “O MUCH MORE
We’e see to tpes of ethods for lasses
Procedures
Functions
No e’re goa talk out constructors!
Let’s do a eaple: Tie lass
In Eclipse, we make a new Java Project
Go to the src folder, create new Class
Call it Time
Before Class header, we write a specification for the class
Then when we declare certain attributes of the class, we include their
invariants as a comment next to them
hr and min (Fields)
In this case we made these PRIVATE
THESE ARE CALLED FIELDS
Like attributes in Python
We do not want users to access these variables
Then we can write our Getters
These are public!
Then we add stuff that we desire, like a toString() method that gives us
the object of Tie i a strig of the for :
We write retur hr + : ;
BUT WAIT! hr and min are ints!
That’s oka!
Automatically casts themselves as Strings
Testing with JUnit
File > New > JUnit Test Case (Use version 4)
Then we create our test cases as needed, typically using assert
statements
assertEquals(expected val, computed val);
assertEquals(0, t.getHour());
assertEquals(0, t.getMinute());
assertEquals("00:00", t.toString());
This will fail with our previous definition for toString()
find more resources at oneclass.com
find more resources at oneclass.com

Only pages 1-2 are available for preview. Some parts have been intentionally blurred.

If we have hr = 0, min = 0, then our previous toString will
retur :
So we need to fix
We make a private method to convert numbers to
strigs ith a  efore the to fi to“tig
Priate eause user does’t eed to aess
it.
Now we change toString()
return make2(hr) + ":" + make2(mn);
YOU NEED TO PUT @Test BEFORE A TEST CASE
“o o e got soe good stuff, ut e still a’t do uh
What if we want to change the time?
Setters!
These are procedures, no return value
Just changing something about our object
BUT
We have invariants boi
Gotta make sure those are good
For example, if we do t.setHour(50);
That’s a BIG NO NO
You tell me what hour is past 23 mann.
So what do we do? Write Preconditions!
As a comment, we write Precondition: h in 0 ...23
Before function header, after but in specification
Then we can write asserts to enforce our
precondition
assert (0 <= h && h <=23);
No for the fu stuff…
Constructors!
Right now, to create a Time object with a specific time, we need to create
the object, then use setters
What if had a method that could immediately make the object the time
we want!?!?!?!
That’s hat a ostrutor is
public Time (int h, int m) {
hr = h;
mn = m;
}
find more resources at oneclass.com
find more resources at oneclass.com
You're Reading a Preview

Unlock to view full version