Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 61 CS110 Lecture 6 Thursday, February 12, 2004 Announcements –hw2 due today Agenda –questions –testing –modeling text files –declarations (classes,

Similar presentations


Presentation on theme: "Lecture 61 CS110 Lecture 6 Thursday, February 12, 2004 Announcements –hw2 due today Agenda –questions –testing –modeling text files –declarations (classes,"— Presentation transcript:

1 Lecture 61 CS110 Lecture 6 Thursday, February 12, 2004 Announcements –hw2 due today Agenda –questions –testing –modeling text files –declarations (classes, variables, methods) –shapes

2 Lecture 62 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

3 Lecture 63 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)

4 Lecture 64 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

5 Lecture 65 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

6 Lecture 66 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!

7 Lecture 67 Java output 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 Lecture 68 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)

9 Lecture 69 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

10 Lecture 610 Examine text file properties name size date in xemacs windows view  details owner contents: “public class Bank ….”

11 Lecture 611 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

12 Lecture 612 TextFile javadoc

13 Lecture 613 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 block for web page preformatting

14 Lecture 614 TextFile unit test (javadoc) dates will differ, of course

15 Lecture 615 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)

16 Lecture 616 Class declaration One per file File name matches class name TextFile.java (line 18): public class TextFile { // body - fields and methods } keyword access keyword identifier

17 Lecture 617 Constructor declaration access className(parameters) { // body - what to do when new one is built } 37 public TextFile( String owner, String contents) {… }

18 Lecture 618 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 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 Lecture 619 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() {…}

20 Lecture 620 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

21 Lecture 621 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

22 Lecture 622 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 yourself 76 this.setContents(contents+text); is the same as 76 setContents(contents+text); (this is implicit)

23 Lecture 623 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

24 Lecture 624 hw3 Practice new Java vocabulary (Lens.java) Improve TextFile class Draw box-and-arrow pictures Explore the Java API

25 Lecture 625 Shapes A 20x10 Screen with 3 HLines: ++++++++++++++++++++++ +RRRRRRRRRR + +GGGGGGGGGGGGGGG + +BBBBBBBBBBBBBBB + + ++++++++++++++++++++++ draw 3 Boxes (2 overlapping): ++++++++++++++++++++++ + + RRRR + + RGGGGGGG + + GGGGGGG + + GGGGGGG GGGGGGG + + GGGGGGG + + ++++++++++++++++++++++ Character graphics on your terminal

26 Lecture 626 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


Download ppt "Lecture 61 CS110 Lecture 6 Thursday, February 12, 2004 Announcements –hw2 due today Agenda –questions –testing –modeling text files –declarations (classes,"

Similar presentations


Ads by Google