CS/ENGRD 2110 FALL 2014 Lecture 3: Fields, getters and setters, constructors, testing 1.

Slides:



Advertisements
Similar presentations
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advertisements

Written by: Dr. JJ Shepherd
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,
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;
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
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,
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
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.
Understanding class definitions Looking inside classes.
CS/ENGRD 2110 SPRING 2013 Lecture 2: Introduction to Java 1.
CS/ENGRD 2110 SPRING 2015 Lecture 3: Fields, getters and setters, constructors, testing 1.
ITP © Ron Poet Lecture 13 1 Helper Objects. ITP © Ron Poet Lecture 13 2 Console Helper Object  We have used Console objects for a while, let’s see what.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
CS61B L03 Building Objects (1)Garcia / Yelick Fall 2003 © UCB  Dan Garcia ( Kathy Yelick  (
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
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:
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.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
10-Nov-15 Java Object Oriented Programming What is it?
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
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.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CS/ENGRD 2110 FALL 2013 Lecture 4: The class hierarchy; static components 1.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
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.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
Software Design– Unit Testing SIMPLE PRIMER ON Junit Junit is a free simple library that is added to Eclipse to all automated unit tests. The first step,
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.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
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.
CS/ENGRD 2110 FALL 2016 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Fall 2017 Lecture 3: Fields, getters and setters, constructors, testing
Some Eclipse shortcuts
Introduction to Computer Science / Procedural – 67130
CS/ENGRD 2110 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
Review Session.
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2018 Lecture 3: Fields, getters and setters, constructors, testing
CS/ENGRD 2110 Spring 2017 Lecture 4: The class hierarchy; static components
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Fall 2017 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2016 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Fall 2018 Lecture 3: Fields, getters and setters, constructors, testing
CS/ENGRD 2110 Fall 2018 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2019 Lecture 3: Fields, getters and setters, constructors, testing
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2019 Lecture 4: The class hierarchy; static components
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Presentation transcript:

CS/ENGRD 2110 FALL 2014 Lecture 3: Fields, getters and setters, constructors, testing 1

Big Red Sports Network (BRSN) is Cornell’s only student-run Broadcast Media, and Journalism organization dedicated entirely to Sports, with strong parternships with Cornell Athletics, WVBR FM 93.5, and the Ithaca Voice. UPCOMING INFO SESSIONS WEDNESDAY 9/3- Kennedy 326 (5:00 PM) MONDAY 9/8- Goldwin Smith 342 (5:00 PM) THURSDAY 9/11- Goldwin Smith G76 (5:00 PM)

CS2110 Announcements 3 A0  Some people have not submitted A0 through CMS (perhaps because they had trouble getting Java/Eclipse going on their computer, or they added the class late, not in CMS, etc.).  No late penalty (this time) for A0 handed in through Wednesday. A1  Will be available on CMS and the course website this morning Piazza  Check course Piazza regularly for announcements  Also to learn about issues with assignments, to find partners, etc.

Assignment A1 is on course website, CMS 4 Write a simple class to maintain information about elephants. Objectives in brief:  Get used to Eclipse and writing a simple Java class  Learn conventions for Javadoc specs, formatting code (e.g. indentation), class invariants, method preconditions  Learn about and use JUnit testing Important: read carefully, including Step 7, which reviews what the assignment is graded on.

Homework 5 1. Course website contains classes Time and TimeTester. The body of the one-parameter constructor is not written. Write it. The one- parameter constructor is not tested in TimeTester. Write a procedure to test it. 2. Visit course website, click on Resources and then on Code Style Guidelines. Study 1. Naming conventions 3.3 Class invariant 4. Code organization 4.1 Placement of field declarations 5. Public/private access modifiers 3. Look at slides for next lecture; bring them to next lecture

Overview 6  An object can contain variables as well as methods. Variable in an object is called a field.  Declare fields in the class definition. Generally, make fields private so they can’t be seen from outside the class.  May add getter methods (functions) and setter methods (procedures) to allow access to some or all fields.  Use a new kind of method, the constructor, to initialize fields of a new object during evaluation of a new-expression.  Create a JUnit Testing Class to save a suite of test cases.

References to text and JavaSummary.pptx 7 Declaration of fields: B.5-B.6 slide 12 Getter/setter methods: B.6 slide 13, 14 Constructors: B.17-B.18 slide 15 Class String: A.67-A.73 JUnit Testing Class: none slide Overloading method names: B-21 slide 22

class Time 8 Object contains the time of day in hours and minutes. Methods in object refer to field in object. Could have an array of such objects to list the times at which classes start at Cornell. With variables t1 and t2 below, t1.getHour() is 8 t2.getHour() is 9 t2.toString() is “09:05” Time hr 8 min 0 getHour() getMin() toString() Time hr 9 min getHour() getMin() toString() 5 t1 t2

A class Time 9 /** An instance maintains a time of day */ public class Time { private int hr; //hour of the day, in private int min; // minute of the hour, in } Time hr 9 min 5 Access modifier private: can’t see field from outside class Software engineering principle: make fields private, unless there is a real reason to make public

Class invariant 10 /** An instance maintains a time of day */ public class Time { private int hr; // hour of the day, in private int min; // minute of the hour, in } Class invariant: collection of defs of variables and constraints on them (green stuff) Software engineering principle: Always write a clear, precise class invariant, which describes all fields. Call of every method starts with class invariant true and should end with class invariant true. Frequent reference to class invariant while programming can prevent mistakes.

Getter methods (functions) 11 /** An instance maintains a time of day */ public class Time { private int hr; // hour of the day, in private int min; // minute of the hour, in /** Return hour of the day */ public int getHour() { return hr; } /** Return minute of the hour */ public int getMin() { return min; } Time hr 9 min 5 getHour() getMin() Spec goes before method. It’s a Javadoc comment —starts with /**

A little about type (class) String 12 public class Time { private int hr; //hour of the day, in private int min; // minute of the hour, in /** Return a represention of this time, e.g. 09:05*/ public String toString() { return prepend(hr) + ":" + prepend(min); } /** Return i with preceding 0, if necessary, to make two chars. */ private String prepend(int i) { if (i > 9 || i < 0) return "" + i; return "0" + i; } … } Java: double quotes for String literals Java: + is String catenation “helper” function is private, so it can’t be seen outside class Concatenate with empty String to change any value to a String

Setter methods (procedures) 13 /** An instance maintains a time of day */ public class Time { private int hr; //hour of the day, in private int min; // minute of the hour, in … } Time hr 9 min 5 getHour() getMin() toString() No way to store value in a field! We can add a “setter method” /** Change this object’s hour to h */ public void setHour(int h) { hr= h; } setHour(int) setHour(int) is now in the object

Setter methods (procedures) 14 /** An instance maintains a time of day */ public class Time { private int hr; //hour of day, in private int min; // minute of hour, in … } Time hr 9 min 5 /** Change this object’s hour to h */ public void setHour(int h) { hr= h; } getHour() getMin() toString() setHour(int) Do not say “set field hr to h” User does not know there is a field. All user knows is that Time maintains hours and minutes. Later, we show an imple- mentation that doesn’t have field h but “behavior” is the same

Test using a JUnit testing class 15 In Eclipse, use menu item File  New  JUnit Test Case to create a class that looks like this: import static org.junit.Assert.*; import org.junit.Test; public class TimeTester public void test() { fail("Not yet implemented"); } Select TimeTester in Package Explorer. Use menu item Run  Run. Procedure test is called, and the call fail(…) causes execution to fail:

Test using a JUnit testing class 16 … public class TimeTester public void test() { Time t1= new Time(); assertEquals(0, t1.getHour()); assertEquals(0, t1.getMin(); assertEquals("00:00", t1.toString()); } Write and save a suite of “test cases” in TimeTester, to test that all methods in Time are correct Store new Time object in t1. Give green light if expected value equals computed value, red light if not: assertEquals(expected value, computed value);

Test setter method in JUnit testing class 17 public class TimeTester { public void testSetters() { Time t1= new Time(); t1.setHour(21); assertEquals(21, t1.getHour()); } Time hr 9 min 5 getHour() getMin() toString() setHour(int) TimeTester can have several test methods, each preceded All are called when menu item Run  Run is selected

Constructors —new kind of method 18 public class C { private int a; private int b; private int c; private int d; private int e; } C has lots of fields. Initializing an object can be a pain —assuming there are suitable setter methods C var= new C(); var.setA(2); var.setB(20); var.setC(35); var.setD(-15); var.setE(150); But first, must write a new method called a constructor C var= new C(2, 20, 35, -15, 150); Easier way to initialize the fields, in the new-expression itself. Use:

Constructors —new kind of method 19 Time hr 9 min 5 getHour() getMin() toString() setHour(int) Time(int, int) /** An object maintains a time of day */ public class Time { private int hr; //hour of day, private int min; // minute of hour, /** Constructor: an instance with h hours and m minutes. */ public Time(int h, int m) { hr= h; min= m; } Purpose of constructor: Initialize field of a new object so that its class invariant is true No return type or void Name of constructor is the class name Memorize! Precondition: h in 0..23, m in Need precondition

Revisit the new-expression 20 Time hr 0 min 0 getHour() getMin() toString() setHour(int) Time(int, int) Syntax of new-expression: new If you do not declare a constructor, Java puts in this one: public () { } Evaluation of new-expression: 1. Create a new object of class, with default values in fields Example: new Time(9, 5) 2. Execute the constructor-call Give as value of the expression the name of the new object

How to test a constructor 21 public class TimeTester public void testConstructor1() { Time t1= new Time(9, 5); assertEquals(9, t1.getHour()); assertEquals(5, t1.getMin(); } … } Create an object using the constructor. Then check that all fields are properly initialized —even those that are not given values in the constructor call Note: This also checks the getter methods! No need to check them separately. But, main purpose: check constructor

A second constructor 22 Time hr 9 min 5 getHour() getMin() toString() setHour(int) Time(int, int) Time (int) /** An object maintains a time of day */ public class Time { private int hr; //hour of day, private int min; // minute of hour, /** Constructor: an instance with m minutes. Precondition: m in 0..(23*60 +59) */ public Time(int m) { ??? What do we put here ??? } … Time is overloaded: 2 constructors! Have different parameter types. Constructor call determines which one is called new Time(9, 5) new Time(125)

Method specs should not mention fields 23 public class Time { private int hr; //in private int min; //in /** return hour of day*/ public int getHour() { return h; } /** return hour of day*/ public int getHour() { return min / 60; } Time hr 9 min 5 getHour() getMin() toString() setHour(int) public class Time { // min, in 0..23*60+59 private int min; Time min 545 getHour() getMin() toString() setHour(int) Specs of methods stay the same. Implementations, including fields, change! Decide to change implemen -tation