lecture 61 cs110 lecture 6 thursday, february 12, 2004 announcements –hw2 due today agenda...
TRANSCRIPT
Lecture 6 1
CS110 Lecture 6Thursday, February 12, 2004
• Announcements– hw2 due today
• Agenda– questions– testing– modeling text files– declarations (classes, variables, methods)– shapes
Lecture 6 2
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 6 3
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 6 4
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 6 5
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 6 6
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 6 7
Java output• In this course:
Terminal t = new Terminal();t.println(“something”); // print, then CRt.print(“something”); // no CRt.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
Lecture 6 8
Terminal vs System
• 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)
Lecture 6 9
Modeling text files
• TextFile object models a text file in a computer (Windows, Unix, …)
• Design
• Public interface (API)
• Unit test
• Private implementation
• Declarations
• Getters and setters
Lecture 6 10
Examine text file properties
namesize
date
in xemacs
windowsview details
owner
contents: “public class Bank ….”
Lecture 6 11
TextFile.java
• 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
Lecture 6 12
TextFile javadoc
Lecture 6 13
TextFile unit test• 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
Lecture 6 14
TextFile unit test (javadoc)
dates will differ, of course
Lecture 6 15
Declarations• 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)
Lecture 6 16
Class declaration• One per file
• File name matches class name
• TextFile.java (line 18):
public class TextFile
{
// body - fields and methods
}
keywordaccess keyword identifier
Lecture 6 17
Constructor declaration
• access className(parameters) { // body - what to do when new one is built }
37 public TextFile( String owner, String contents)
{… }
Lecture 6 18
Variable declarations (review)• 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 name23 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);
Lecture 6 19
Method declarations
• 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() {…}
Lecture 6 20
getters and setters• 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
Lecture 6 21
getters and setters
• 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
Lecture 6 22
this• 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 yourself76 this.setContents(contents+text); is the same as76 setContents(contents+text);(this is implicit)
Lecture 6 23
TextFile constructor
• 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
Lecture 6 24
hw3
• Practice new Java vocabulary (Lens.java)
• Improve TextFile class
• Draw box-and-arrow pictures
• Explore the Java API
Lecture 6 25
Shapes
A 20x10 Screen with 3 HLines:
++++++++++++++++++++++
+RRRRRRRRRR +
+GGGGGGGGGGGGGGG +
+BBBBBBBBBBBBBBB +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
++++++++++++++++++++++
draw 3 Boxes (2 overlapping):
++++++++++++++++++++++
+ +
+ RRRR +
+ RRRR +
+ RGGGGGGG +
+ GGGGGGG +
+ GGGGGGG GGGGGGG +
+ GGGGGGG GGGGGGG +
+ GGGGGGG +
+ GGGGGGG +
+ +
++++++++++++++++++++++
• Character graphics on your terminal
Lecture 6 26
Shapes classes• 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