CS/ENGRD 2110 Fall 2017 Lecture 3: Fields, getters and setters, constructors, testing http://courses.cs.cornell.edu/cs2110.

Slides:



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

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,
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.
1 CS1130 Spring 2011: David Gries & Walker White Transition to Object-Oriented Programming Assumes programming knowledge in a language like Matlab, C,
CS/ENGRD 2110 FALL 2014 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS/ENGRD 2110 SPRING 2015 Lecture 3: Fields, getters and setters, constructors, testing 1.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
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.
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.
Chapter 4 Introduction to Classes, Objects, Methods and strings
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Written by: Dr. JJ Shepherd
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
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.
Recursion (Continued)
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
Lecture 5:Interfaces and Abstract Classes
The need for Programming Languages
Concepts of Object Oriented Programming
Some Eclipse shortcuts
Scope, Objects, Strings, Numbers
Recursion (Continued)
Software Engineering 1, CS 355 Unit Testing with JUnit
Recursion (Continued)
Intro to PHP & Variables
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
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
CS/ENGRD 2110 Fall2017 Lecture 4: The class hierarchy; static components
Design by Contract Fall 2016 Version.
Lesson 2: Building Blocks of Programming
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Introduction to Data Structures
CS/ENGRD 2110 Fall 2017 Lecture 5: Local vars; Inside-out rule; constructors
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
CS/ENGRD 2110 Fall 2018 Lecture 4: The class hierarchy; static components
Race Conditions & Synchronization
CS/ENGRD 2110 Fall 2018 Lecture 3: Fields, getters and setters, constructors, testing
Recursion (Continued)
CS/ENGRD 2110 Fall 2018 Lecture 2: Objects and classes in Java
Lecture 4: Data Abstraction CS201j: Engineering Software
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
Data Structures and Algorithms for Information Processing
Barb Ericson Georgia Institute of Technology Oct 2005
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
CISC124 Labs start this week in JEFF 155. Fall 2018
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
COP 3330 Object-oriented Programming in C++
Barb Ericson Georgia Institute of Technology June 2005
Summary of what we learned yesterday
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Review of Previous Lesson
CS2013 Lecture 7 John Hurley Cal State LA.
CMPE212 – Reminders Assignment 2 due today, 7pm.
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CS/ENGRD 2110 Fall 2017 Lecture 3: Fields, getters and setters, constructors, testing http://courses.cs.cornell.edu/cs2110

Object-Oriented Programming Classes Objects House (houz) n. An object that… Can create many objects from the same plan (class). Usually, not all exactly the same. Class := a blueprint, design, plan, definition A blueprint, plan , a definition A house built from the blueprint

class House Object is an instance of a house. Contains bdrs (number of bedrooms) and baths (number of bathrooms) Methods in object refer to fields in object. Could have an array of such objects to list the apartments in a building. With variables h1 and h2 below, t1.getBeds() is 2 t2.getBeds() is 4 House@150 House bdrs 2 baths 1 getBeds() getBaths() toString() House@fa8 House bdrs 4 baths 2 getBeds() getBaths() toString() h1 House@150 h2 House@fa8

Class invariants Class invariant: collection of defs of variables and constraints on them (green stuff) /** An instance maintains info for a house */ public class House { private int bdrs; // number of bedrooms, in 0…10 private int baths; // number of bathrooms, in 1…5 } 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.

Generate javadoc With project selected in Package explorer, use menu item Project -> Generate javadoc In Package Explorer, click on the project -> doc -> index.html You get a pane with an API like specification of class Time, in which javadoc comments (start with /**) have been extracted! That is how the API specs were created.

Class House /** An instance maintains info for a House */ public class House { private int bdrs; // number of bedrooms, in 0…10 private int baths; // number of bathrooms, in 1…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 House@150 House bdrs 2 baths 1 getBeds() getBaths() toString()

Getter methods (functions) /** An instance maintains info for a house */ public class House{ private int bdrs; // number of bedrooms, in 0..10 private int baths; // number of bathrooms, in 1..5 /** Return number of bedrooms */ public int getBeds() { return bdrs; } /** Return number of bathrooms */ public int getBaths() { return baths; Spec goes before method. It’s a Javadoc comment —starts with /** House@150 House bdrs 2 baths 1 getBeds() getBaths() toString()

Setter methods (procedures) Do not say “set field bdrs to b” User does not know there is a field. All user knows is that House maintains bedrooms and bathrooms. Later, we show an imple-mentation that doesn’t have field b but “behavior” is the same /** An instance maintains info for a house */ public class House{ private int bdrs; // number of bedrooms, in 0..10 private int baths; // number of bathrooms, in 1..5 … } No way to store value in a field! We can add a “setter method” /** Change number of bathrooms to b */ public void setBeds(int b) { bdrs= b; } House@150 House bdrs 2 baths 1 getBeds() getBaths() toString() setBeds(int) is now in the object setBeds(int)

Method specs should not mention fields public class House{ private int bdrs; //in 0..10 private int baths; //in 1..5 /** return number of rooms*/ public int getRooms() { return bdrs+baths; } /** return number of rooms*/ public int getRooms() { return rooms; } public class House{ private int rooms; // rooms, in 1..15 House@150 House rooms 3 getBeds() getBaths() toString() Decide to change implemen-tation House@150 House bdrs 2 baths 1 getBeds() getBaths() toString() Specs of methods stay the same. Implementations, including fields, change!

A little about type (class) String public class House{ private int bdrs; // number of bedrooms, in 0..10 private int baths; // number of bathrooms, in 1..5 /** Return a representation of this house */ public String toString() { return plural(bdrs) + "," + baths; /** Return i with preceding 0, if necessary, to make two chars. */ private String plural(int i) { if (i ==1) return "" + i + "bedroom"; return "" + i + "bedrooms"; } … Java: double quotes for String literals Java: + is String catenation Catenate with empty String to change any value to a String “helper” function is private, so it can’t be seen outside class

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

Test using a JUnit testing class public class HouseTester { … @Test public void testSetters() { House h= new House(); h.setBeds(2); assertEquals(2, h.getBeds()); } Write and save a suite of “test cases” in HouseTester, to test that all methods in Time are correct Store new House object in h. 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 public class HouseTester { … @Test public void testSetters() { House h= new House(); h.setBeds(2); assertEquals(2, h.getBeds()); } HouseTester can have several test methods, each preceded by @Test. All are called when menu item Run Run is selected House@150 House bdrs 2 baths 1 getBeds() getBaths() toString()

Constructors —new kind of method 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(2, 20, 35, -15, 150); Easier way to initialize the fields, in the new-expression itself. Use: 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

Constructors —new kind of method /** An object maintains info about a house */ public class House{ private int bdrs; // number of bedrooms, in 0..10 private int baths; // number of bathrooms, in 1..5 /** Constructor: an instance with bd bedrooms and bth bathrooms. */ public House(int bd, int bth) { bdrs= bd; baths= bth; } Purpose of constructor: Initialize fields of a new object so that its class invariant is true Memorize! Precondition: bd in 0..10, bth in 1..5 Need precondition No return type or void House@150 House bdrs 2 baths 1 getBeds() getBaths() toString() Name of constructor is the class name

Revisit the new-expression Syntax of new-expression: new <constructor-call> Example: new House(9, 5) House@fa8 Evaluation of new-expression: 1. Create a new object of class, with default values in fields 2. Execute the constructor-call 3. Give as value of the expression the name of the new object House@fa8 Time bdrs baths getBeds() getBaths() toString() setBeds(int) House(int, int) 9 5 If you do not declare a constructor, Java puts in this one: public <class-name> () { }

How to test a constructor 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 public class HouseTester { @Test public void testConstructor() { House h= new House(9, 5); assertEquals(9, h.getBeds()); assertEquals(5, h.getBaths(); } … Note: This also checks the getter methods! No need to check them separately. But, main purpose: check constructor

Recap An object is defined by a class. An object can contain variables (fields) as well as methods (functions/procedures). Use comments and javadoc to document invariants and specify behavior 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.

CS2110 FAQs Lecture Videos: they’re available http://cornell.videonote.com/channels/1027/videos Grading Options: S/U is fine by us. Check with your advisor/major. Prelim conflicts: Please don’t email us about prelim conflicts! We’ll tell you at the appropriate time how we handle them. Other Questions: check course Piazza regularly for announcements.

Recitation This Week You must read/watch the tutorial BEFORE the recitation: www.cs.cornell.edu/courses/cs2110/2017fa/online/exceptions/EX1. html Get to it from the Tutorials page of the course website. NOTE THAT THERE ARE SIX WEB PAGES! Bring your laptop to class, ready to answer questions, solve problems. During the section, you can talk to neighbors, discuss things, answer questions together. The TA will walk around and help. The TA will give a short presentation on some issue if needed. Homework questions are on the course website. You will have until a week after the recitation (on a Wednesday night) to submit answers on the CMS.

Assignments A0 out: Due this Thursday (8/31) A1 out: Due next Wednesday (9/6) A2 out: Due the following week (9/13)

Assignment A1 Write a class to maintain information about PhDs ---e.g. their advisor(s) and date of PhD. 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.

Assignment A1 Groups. You can do A1 with 1 other person. FORM YOUR GROUP EARLY! Use Piazza Note @5 to search for partner! CHECK the pinned A1 note on the Piazza every day.