1
|
- Announcements
- Agenda
- questions
- testing
- modeling text files
- declarations (classes, variables, methods)
- shapes
|
2
|
- 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
|
3
|
- 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)
|
4
|
- 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
|
5
|
- 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
|
6
|
- 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!
|
7
|
- In this course:
- Terminal t = new Terminal();
- t.println(“something”); // print, then CR
- t.print(“something”); // no CR
- t.println(); // just
CR
- Standard Java:
- System.out.println(“something”);
- System.out.print(“something”);
- System.out.println();
- System class is part of Java library
- System class has public field out
- out can respond to print* messages
|
8
|
- System and Terminal both write to screen - now
- Terminal may write to a window later- today’s programs will still
work
- System class also has a public in field, for reading from keyboard
- System.in reads only Strings, hard to use
- Terminal read* methods are better tools (under the hood, Terminal is a
client for System.in)
|
9
|
- TextFile object models a text file in a computer (Windows, Unix, …)
- Design
- Public interface (API)
- Unit test
- Private implementation
- Declarations
- Getters and setters
|
10
|
|
11
|
- private fields
- owner, create and mod date, contents (lines 22-25)
- public methods (API) - see javadoc
- TextFile( String owner, String
contents) // constructor
- getContents(), setContents(String newContents )
- getSize(), getCreateDate(), getModDate(), getOwner()
- append(String text), appendLine(String text)
- public main for unit testing
|
12
|
|
13
|
- To test your work when there is no client, write your own main, with a
self documenting hard coded test of all the public methods
- Read main and its javadoc comment in TextFile.java
- main creates and exercises a TextFile
- Output pasted into input as a comment
- html <pre> </pre> block for web page preformatting
|
14
|
|
15
|
- Tell java compiler what’s coming
- where you get to make up names
(identifiers)
- just prepare for action - don’t do
anything
- classes (line 18, whole file)
- fields (instance variables) (lines 22-25)
- constructor (line 37)
- methods (51, 63, 74, 85, 97, 110, 117, 132, 158)
- local variables (lines 99, 160, 161)
- parameters for methods (lines 51, 74, 85)
|
16
|
- One per file
- File name matches class name
- TextFile.java (line 18):
|
17
|
- access className(parameters)
- {
- // body - what to do when new
one is built
- }
- 37 public TextFile( String
owner,
- String
contents)
- {…
- }
|
18
|
- A variable is a named place to hold a value
- local (inside method): Type name
- 160 Terminal terminal;
- 99 int charCount;
- instance (inside class): access Type name
- 23 private Date createDate;
- 25 private String contents;
- “field” and “instance variable” are synonyms
- parameters (in method declaration): Type name
- 74 public void append (String text);
|
19
|
- access ReturnType methodName (parameters)
- {
- // body - what to do when this
object gets message
- }
- 63 public String getContents() {…}
- 74 public void append(String text){…}
- 97 public int getSize() {…}
|
20
|
- Good
- private String contents;
- public String getContents()
- public void setContents (String contents)
- x = aTextFile.getContents() in client class
- Bad (public access to field itself)
- public String contents;
- x = aTextFile.contents in client class
|
21
|
- Hide details from the clients
- int getSize() (line 97)
- there is no size field - code delegates the job
- TextFile setContents(String contents) (line 51)
- changes modification date
- uses this
|
22
|
- Keyword for the object we are looking at
- Tricky - takes getting used to
- Settles ambiguity in variable names:
- 40 this.contents = contents;
- declared on line 25 on line 37
- Send a message to yourself
- 76 this.setContents(contents+text);
- is the same as
- setContents(contents+text);
- (this is implicit)
|
23
|
- 39, 40: Initialize owner and contents to values passed as parameters
(using this)
- 41: Set createDate field to refer to a new Date object (Date class comes
with Java)
- 42: Set modDate to be the same as createDate
|
24
|
- Practice new Java vocabulary (Lens.java)
- Improve TextFile class
- Draw box-and-arrow pictures
- Explore the Java API
|
25
|
- A 20x10 Screen with 3 HLines:
- ++++++++++++++++++++++
- +RRRRRRRRRR +
- +GGGGGGGGGGGGGGG +
- +BBBBBBBBBBBBBBB +
- + +
- + +
- + +
- + +
- + +
- + +
- + +
- ++++++++++++++++++++++
- draw 3 Boxes (2 overlapping):
- ++++++++++++++++++++++
- + +
- + RRRR +
- + RRRR +
- + RGGGGGGG +
- + GGGGGGG +
- + GGGGGGG GGGGGGG +
- + GGGGGGG GGGGGGG +
- + GGGGGGG +
- + GGGGGGG +
- + +
- ++++++++++++++++++++++
|
26
|
- Particular shapes:
- HLine, Box (source code provided)
- VLine, Frame, Triangle (hw3)
- Shapes are clients for Screen
- Use Screen javadoc API
- Don’t look at source code
- Clients for Shapes classes
- TestShapes (source code provided)
- Box is a client for HLine services
|