lecture 61 cs110 lecture 6 thursday, february 12, 2004 announcements –hw2 due today agenda...

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

Upload: hugo-hines

Post on 29-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

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

Lecture 6 1

CS110 Lecture 6Thursday, February 12, 2004

• Announcements– hw2 due today

• Agenda– questions– testing– modeling text files– declarations (classes, variables, methods)– shapes

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

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

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

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)

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

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

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

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

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

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!

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

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

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

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)

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

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

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

Lecture 6 10

Examine text file properties

namesize

date

in xemacs

windowsview details

owner

contents: “public class Bank ….”

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

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

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

Lecture 6 12

TextFile javadoc

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

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

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

Lecture 6 14

TextFile unit test (javadoc)

dates will differ, of course

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

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)

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

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

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

Lecture 6 17

Constructor declaration

• access className(parameters) { // body - what to do when new one is built }

37 public TextFile( String owner, String contents)

{… }

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

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);

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

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() {…}

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

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

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

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

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

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)

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

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

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

Lecture 6 24

hw3

• Practice new Java vocabulary (Lens.java)

• Improve TextFile class

• Draw box-and-arrow pictures

• Explore the Java API

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

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

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

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