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

CS 2110 Lecture Notes - Lecture 3: Junit, Precondition


Department
Computer Science
Course Code
CS 2110
Professor
Gries
Lecture
3

This preview shows page 1. to view the full 5 pages of the document.
Lecture 3 - More Objects and Stuff
oWe’ve seen public classes, but there is SO MUCH MORE
oWe’ve seen two types of methods for classes
Procedures
Functions
oNow we’re gonna talk bout constructors!
oLet’s do an example: Time class
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 Time in a string of the form “03:21”
We write return hr + “:” + mn;
BUT WAIT! hr and min are ints!
That’s okay!
Automatically casts themselves as Strings
oTesting 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()
If we have hr = 0, min = 0, then our previous toString will
return “0:0”
So we need to fix
We make a private method to convert numbers to
strings with a “0” before them to fix toSting()
Private because user doesn’t need to access
it.
Now we change toString()
return make2(hr) + ":" + make2(mn);
YOU NEED TO PUT @Test BEFORE A TEST CASE
oSo now we got some good stuff, but we still can’t do much
find more resources at oneclass.com
find more resources at oneclass.com
You're Reading a Preview

Unlock to view full version

Only page 1 are available for preview. Some parts have been intentionally blurred.

oWhat 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);
oNow for the fun stuff…
oConstructors!
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 what a constructor is
public Time (int h, int m) {
hr = h;
mn = m;
}
Assert preconditions if needed
Now that we have a constructor, we need to change the way we create a
new instance/object of Time (instance and object are synonyms)
Before: Time t = new Time();
Now: Time t = new Time(5,22);
Just an example, point is, YOU NEED to put in arguments
for parameter
How does this get executed
Syntax: new <constructor-call>
New Time(9,5)
Algorithm:
1. Create a new object of class, with default values in fields
In this case, (0,0)
2. Execute the constructor call
3. Give as value of the expression the name of the new
object
Testing constructor
Construct new object of your choice
Test getHour and getMinute
find more resources at oneclass.com
find more resources at oneclass.com
You're Reading a Preview

Unlock to view full version