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

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Your First Java Program: HelloWorld.java
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design Lecture 6, Tue Jan
Creating Shareable Models By: Eric Hutton CSDMS - Community Surface Dynamics Modeling System (pronounced ˈ s ɪ stəms) Image by Flickr user Let There Be.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 1 CMT1000: Introduction to Programming Ed Currie Lecture 3: Program structure.
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Lecture 241 CS110 Lecture 25 Tuesday, May 4, 2004 Announcements –final exam Thursday, May 20, 8:00 AM McCormack, Floor 01, Room 0608 (easier than last.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSC172 Intro Pepper. Goals Reminder of what a class is How to create and use classes How to design classes How to think in terms of objects How to comment.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.
Java Classes Using Java Classes Introduction to UML.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Wrapup1 CS110 Wrapup Tuesday, May 11, 2004 Announcements –final exam Thursday, May 20, 8:00 AM McCormack, Floor 01, Room 0608 (easier than last exam?!)
Classes CS 21a: Introduction to Computing I First Semester,
Lecture 21 CS110 Lecture 2 January 29, 2004 Announcements –hw1 part 1 – due right now –hw1 part 2 – due Tuesday night Questions Agenda –turnin –Object.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects.
Lecture 131 CS110 Lecture 13 Thursday, March 11, 2004 Announcements –hw5 due tonight –Spring break next week –hw6 due two weeks from tonight Agenda –questions.
Creating and running a Java program. Eclipse Interactive Development Environment (IDE)  Eclipse is an Interactive Development Environment (IDE) for Java.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Lecture 8 February 29, Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Lecture 71 CS110 Lecture 7 February 17, 2004 Announcements –hw3 due Thursday Agenda –questions –hw3 tips –getters and setters – information hiding –delegation.
Documentation javadoc. Documentation not a programmer's first love lives in a separate file somewhere usually a deliverable on the schedule often not.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Chapter 4 Introduction to Classes, Objects, Methods and strings
XP Tutorial 8 Adding Interactivity with ActionScript.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Creating a Java Application and Applet
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Chapter 3 Implementing Classes
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Computer Programming Your First Java Program: HelloWorld.java.
More Sophisticated Behavior
Some Tips for Using Eclipse
Writing Methods.
Chapter 4: Writing classes
Interfaces.
Introduction to Classes and Objects
Workshop for Programming And Systems Management Teachers
COP 3330 Object-oriented Programming in C++
February , 2009 CSE 113 B.
Classes CS 21a: Introduction to Computing I
References Revisted (Ch 5)
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

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

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

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)

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

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

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!

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

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)

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

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

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

Lecture 612 TextFile javadoc

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

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

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)

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

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

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

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

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

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

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)

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

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

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

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