1 For more info: CS1110 16 Sep 2008 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston to.

Slides:



Advertisements
Similar presentations
1 FrontPage 2000 Online Tutorial The following tutorial aims to help you get started with FrontPage 2000 for the creation of basic web pages. The different.
Advertisements

TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
1 CS100J. Lecture 2, 25 January 2007 Today’s topic: Objects and classes Reading for this lecture: Section 1.3. It’s most important that you study this.
CS/ENGRD 2110 SPRING 2015 Lecture 4: The class hierarchy; static components 1.
1 CS Sept. Customizing a class & testing Quiz 2 on Tuesday 15 Sept Purpose of a constructor (slide 5) Evaluating a new expression (slide 6) Fields;
1 CS100J 1 February Customizing a class & testing Quote for the day: There is no reason anyone would want a computer in their home. - -Ken Olson,
Chapter 41 Defining Classes and Methods Chapter 4.
CS100J 11 September 2003 Course Management System for CS100J is now populated with students who were pre-registered. Look at course web page to see how.
CS 1110 Prelim I: Review Session. Exam Info Prelim 1: 7:30–9:00PM, Thursday, 6 October, Statler Auditorium Look at the previous Prelims Arrive early!
CS100J 11 September 2003 For more info: The "at" was made famous by Ray Tomlinson, a researcher at BBN in Boston.
1 CS100J 30 January 2007 The class definition Course Management System (CMS) for CS100J is populated with students who were pre-registered. Look at course.
1 CS100J 05 February 2005 Today’s topic: Customizing a class (continued) Quiz 1 is today Quiz 2 is next Tuesday Quote for the day: There is no reason anyone.
CS/ENGRD 2110 FALL 2014 Lecture 3: Fields, getters and setters, constructors, testing 1.
Quote for the day: Computational thinking: a fundamental skill for everyone … [It] is … choosing an appropriate representation for a problem or modeling.
UML Basics & Access Modifier
CS/ENGRD 2110 SPRING 2015 Lecture 3: Fields, getters and setters, constructors, testing 1.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
1 CS Sept 2010 Customizing a class Quote for the day: I have traveled the length and breadth of this country and talked with the best people, and.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Mail merge I: Use mail merge for mass mailings Perform a complete mail merge Now you’ll walk through the process of performing a mail merge by using the.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CS1110 lecture 5 14 Sept 2010 Testing; the class Object; toString; static variables & methods Reading for this lecture: Testing with JUnit (Appendix I.2.4.
1 CS1110 lecture 4 9 Sept. Customizing a class & testing Classes: fields; getter & setter methods. Secs (p. 45) & 3.1 (pp. 105–110 only) Constructors.
1 CS Sept 2010 Inside-out rule; use of this, super Developing methods (using Strings). Read sec. 2.5, stepwise refinement Listen to Plive, 2.5.1–2.5.4.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS1110 lecture 5 8 Feb 2010 Testing; class Object; toString; static variables/methods Reading for this lecture: Testing with JUnit (Appendix I.2.4 & pp.
1 CS1110 lecture 4 9 Sept. Customizing a class & testing Classes: fields; getter & setter methods. Secs (p. 45) & 3.1 (pp. 105–110 only) Constructors.
CS1101: Programming Methodology Aaron Tan.
CS/ENGRD 2110 FALL 2013 Lecture 4: The class hierarchy; static components 1.
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
1 CS2111 is no longer required for affiliation with Computer Science How to install newest Java Royal Flush is better than Full.
1 CS100J 08 September 2005 Today’s topic: Customizing a class (continued) Quote for the day: There is no reason anyone would want a computer in their home.
1 For more info: CS100J 7 February 2006 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston.
1 CS1110 Classes, stepwise refinement 17 Feb 2009 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of.
1 CS September 2008 Discussion of Methods: Executing method calls.If-statements. The return statement in a function. Local variables. For this and.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
1 Royal Flush is better than Full House CS March 2009 Testing/Debugging Read chapter 14, pp. 385–401.
1 For more info: CS100J 08 February 2005 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston.
1 CS1110 Thursday, 10 Feb 2011 Discussion of Methods: Executing method calls. If-statements. The return statement in a function. Local variables. For this.
1 For more info: CS February 2009 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS/ENGRD 2110 SPRING 2016 Lecture 4: The class hierarchy; static components 1.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
1 CS February 2009 Inside-out rule; use of this and super Developing methods (using String ops). Read sec. 2.5 on stepwise refinement Listen to.
1 CS Sept 2010 Customizing a class Quote for the day: I have traveled the length and breadth of this country and talked with the best people, and.
1 CS Sept Customizing a class & testing Quote for the day: There is no reason anyone would want a computer in their home. - -Ken Olson, founder.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Perform a complete mail merge Lesson 14 By the end of this lesson you will be able to complete the following: Use the Mail Merge Wizard to perform a basic.
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
CS Feb 2010 Customizing a class
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Lecture 10 Review of classes
CS1110 Classes, stepwise refinement 23 Sep 2009
CS100J. Lecture 2, 24 September Objects & classes
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
Can perform actions and provide communication
Testing and debugging A short interlude 2-Dec-18.
CS 1110 Please do this now. Draw three variables on a piece of paper as shown to the right: b 4 c 7 d false Below is a “program” consisting of 3 assignment.
Sit next to someone. Today, we do some work in pairs.
CS/ENGRD 2110 Fall 2018 Lecture 4: The class hierarchy; static components
CS100J 06 September 2005 The class definition
Can perform actions and provide communication
Sit next to someone. Today, we do some work in pairs.
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Testing and debugging A short interlude 17-Apr-19.
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
For more info: CS100J 13 September 2005 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston.
CS Feb 2011 Customizing a class
Workshop for Programming And Systems Management Teachers
Presentation transcript:

1 For more info: CS Sep 2008 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston to help develop the ARPANET, which later turned into the internet. In 1971, Ray Tomlinson of BBN was given the task of figuring out how to send files from one person to another. He created with file attachments. He as the separator between an name and location. Names in other languages: Italian: chiocciolina = little snail French: petit escargot = little snail German: klammeraffe = spider monkey Dutch: api = short for apestaart (monkey's tail) Finnish: miau = cat tail Israeli: strudel = a pastry Danish: snabel = an "A" with a trunk Spanish: un arroba = a unit of about 25 pounds Norwegian: kanel-bolle= spiral-shaped cinnamon cake TODAY: Testing using JUnit. Object: the superest class of them all. pp Function toString. Static variables and methods. Sec. 1.5 (p. 47).

2 /** Each instance describes a chapter in a book * */ public class Chapter { private String title; // The title of the chapter private int number; // The number of chapter private Chapter previous; // previous chapter (null if none) /** Constructor: an instance with title t, chap n, previous chap c */ public Chapter(String t, int n, Chapter c) { title= t; number= n; previous= c; } /** = title of this chapter */ public String getTitle() { return title; } /** = number of this chapter */ public int getNumber() { return number; } /** = (name of) the previous chapter (null if none) */ public Chapter getPrevious() { return previous; } } Today, we use a class Chapter. An instance describes a book. Here, we have a constructor and three getter methods Download class from course web page.

3 Testing --using Junit. Pages (through Sec ). Bug: Error in a program. Testing: Process of analyzing, running program, looking for bugs. Test case: A set of input values, together with the expected output. Debugging: Process of finding a bug and removing it. Get in the habit of writing test cases for a method from the specification of the method even before you write the method. To create a framework for testing in DrJava, select menu File item new Junit test case…. At the prompt, put in the class name ChapterTester. This creates a new class with that name. Immediately save it —in the same directory as class Chapter. The class imports junit.framework.TestCase, which provides some methods for testing.

4 What test cases should we use to test this function? How many do we need? /** = a String that consists of the first letter of each word in s. E.g. for s = “Juris Hartmanis”, the answer is “JH”. Precondition: s has one of the forms “first last” “first middle last” with one or more blanks between each pair of names. There may be blanks at the beginning and end. public String initialsOf(String s) { … }

5 /** A JUnit test case class. * Every method starting with the word "test" will be called when running * the test with JUnit. */ public class ChapterTester extends TestCase { /** A test method. * (Replace "X" with a name describing the test. You may write as * many "testSomething" methods in this class as you wish, and each * one will be called when testing.) */ public void testX() { } assertEquals(x, y): test whether x equals y ; print an error message and stop the method if they are not equal. x: expected value, y: actual value. Other methods listed on page 488.

6 /** Test first constructor and getter methods getTitle, getNumber, and getPrevious */ public void testFirstConstructor() { Chapter c1= new Chapter("one", 1, null); assertEquals("one”, c1.getTitle(), ); assertEquals(1, c1.getNumber()); assertEquals(null, c1.getPrevious()); } Every time you click button Test in DrJava, all methods with a name testXXX will be called. one test case /** Test Setter methods setTitle, setNumber, and setPrevious */ public void testSetters() { Chapter c1= new Chapter("one", 1, null); c1.setTitle("new title"); c1.setNumber(18); Chapter c2= new Chapter("two", 2, null); c1.setPrevious(c2); assertEquals("new title", c1.getTitle()); assertEquals(18, c1.getNumber()); assertEquals(c2, c1.getPrevious()); } testMethods to test getters and setters

7 Class Object: The superest class of them all See 1/2-page section on page 154. The reason for this will become clear later. You need this information to do assignment A2. Every class that does not extend another one automatically extends class Object. public class C { … } is equivalent to public class C extends Object { …}

8 Class Object: The superest class of them all Bill Patient name B. Clinton address New York owes $ See 1/2-page section on page 154. Bill Patient name B. Clinton address New York owes $ equals(Object) toString() this is really this Object Because it is always there, to avoid clutter, we don’t generally draw the partition for superclass Object

9 Method toString() Bill Patient name B. Clinton address New York owes toString() $ equals(Object) toString() Object Convention: c.toString() returns a representation of folder c, giving info about the values in its fields. Put following method in Patient. /** = representation of this Patient */ public String toString() { return name + “ ” + address + “ ” + owes; } In appropriate places, the expression c automatically does c.toString()

10 Example of toString in another class /** An instance represents a point (x, y) in the plane */ public class Point { private int x; // the x-coordinate private int y; // the y-coordinate /** Constructor: An instance for point (xx, yy) */ public Point(int xx, int yy) { } /** = a representation of this point in form “(x, y)” */ public String toString() { return ; } Getter and setter methods are not given on this slide Function toString should give the values in the fields in a format that makes sense for the class. Example: “(3, 5)” Fill these in

11 A static field does not appear in each folder. It appears in the file drawer, by itself, on a piece of paper. There is only ONE copy of it. public class Chapter { private String title; // title of chapter private static int numberChaps= 0; // no. of Chapter objects created } Reference static variable using Chapter.numberChaps a0 Chapter title “peace” a1 Chapter title “truth” numberChaps 2 File drawer for class Chapter Use a static variable when you want to maintain information about all (or some) folders.

12 Make a method static when it does not refer to any of the fields or methods of the folder. public class Chapter { private int number; // Number of chapter private static int numberOfChapters= 0; /** = “This chapter has a lower chapter number than Chapter c”. Precondition: c is not null. */ public boolean isLowerThan(Chapter c) { return number < c.number; } /** = “b’s chapter number is lower than c’s chapter number”. Precondition: b and c are not null. */ public static boolean isLower(Chapter b, Chapter c) { return b.number < c.number; }