CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Advertisements

CS/ENGRD 2110 SPRING 2015 Lecture 4: The class hierarchy; static components 1.
CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
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.
CS/ENGRD 2110 SPRING 2015 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.
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
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.
0 Teaching Java —with OO first David Gries Computer Science Cornell University Ithaca, NY
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
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.
Programming in Java CSCI-2220 Object Oriented Programming.
Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.
CS/ENGRD 2110 FALL 2013 Lecture 4: The class hierarchy; static components 1.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
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.
Principled approach to teaching OO first David Gries Computer Science, Cornell 1.
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 CS1110. Lecture 2, 28 Jan Objects & classes PLive: Activities 3-3.1, 3-3.2, (not 3-3.3), 3-4.1, Summary of lectures: On course page,
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
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 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.
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.
Arguments to method main Packages APIspecs Characters Strings
CS Feb 2010 Customizing a class
Lecture 10 Review of classes
Examples of Classes & Objects
CS/ENGRD 2110 Fall 2017 Lecture 3: Fields, getters and setters, constructors, testing
Class Structure 15-Jun-18.
Some Eclipse shortcuts
CS/ENGRD 2110 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
CS100J. Lecture 2, 24 September Objects & classes
The fattest knight at King Arthur's round table was Sir Cumference
PRG 420 Education for Service-- prg420.com. PRG 420 Week 1 Coding a Simple “Hello, world!” Java™ Program(New Syllabus) For more course tutorials visit.
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
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.
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Fall2017 Lecture 4: The class hierarchy; static components
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Class Structure 16-Nov-18.
CSC 113 Tutorial QUIZ I.
Class Structure 28-Nov-18.
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 The fattest knight at King Arthur's round table was Sir Cumference. He acquired his size from too much pi. Lecture 6: Consequence.
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.
CS/ENGRD 2110 Fall 2018 Lecture 4: The class hierarchy; static components
Class Structure 7-Dec-18.
CS100J 06 September 2005 The class definition
Class Structure 2-Jan-19.
CS/ENGRD 2110 Fall 2018 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2019 Lecture 5: Local vars; Inside-out rule; constructors
Defining Classes and Methods
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Defining Classes and Methods
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
CS Feb 2011 Customizing a class
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java http://courses.cs.cornell.edu/cs2110

CMS VideoNote.com, PPT slides, DrJava, etc. CMS. Visit course webpage, click “Links”, then “CMS for 2110”. Videos of our lectures: Look at http://cornell.videonote.com/channels/1027/videos Download ppt slides the evening before each lecture, have them available in class. Please don’t ask questions on the piazza about that material the day before the lecture! Download DrJava (the jar file, not the app). It may require downloading an old version of Java. Got a Java question? See first if it’s answered on JavaHyperText

Next week’s recitation/discussion: Exception handling Visit course webpage, click on “Lecture notes” For the row for recitation 2, click on “tutorial”. Your job: BEFORE your recitation, look at all videos in the tutorial. There are about 25-30 minutes of tutorial! This is the longest tutorial you will have to do. Then, in recitation/discussion, you will have a problem set to do. Can do it with 1-2 other people (up to groups of 3). TA will walk around, helping, answering questions, giving pointers, etc. The problem set is due on the CMS no later than one week after the recitation (always on Wednesday night). But we encourage you to finish during the recitation and turn it in immediately. Anything to be submitted is always on the course assignments page!

Assignment A2 Get assignment A2 from course website later today. Objective: Get practice with Java functions. Learn about and use JUnit testing Given to you before A1, but due after A1. Provide flexibility, allow you to get ahead and learn Java early. Groups. You can do A2 with 1 other person. FORM YOUR GROUP EARLY! Use pinned Piazza Note @5 to search for partner!

Java OO (Object Orientation) Python and Matlab have objects and classes. Strong-typing nature of Java changes how OO is done and how useful it is. Put aside your previous experience with OO (if any). This lecture: First: describe objects, demoing their creation and use. Second: Show you a class definition and how it contains definitions of functions and procedures that appear in each object of the class. Third: Talk about keyword null. Fourth (if there is time). Show you a Java application, a class with a “static” procedure with a certain parameter.

Homework Study material of this lecture. Visit course website, click on Resources and then on Code Style Guidelines. Study 3. Documentation 3.1 Kinds of comments 3.2 Don’t over-comment 3.4 Method specifications 3.4.1 Precondition and postcondition Spend a few minutes perusing slides for lecture 3; bring them to lecture 3.

Java OO References to JavaHyperText entries Objects: B.1 object Calling methods: method call Class definition: class public, private: public private method Parameter vs argument: parameter, argument Inside-out rule Text mentions fields of an object. We cover these in next lecture Text uses value-producing method for function and void method for procedure. Get used to terminology: function and procedure Methods may have parameters Method calls may have arguments

Drawing an object of class javax.swing.JFrame Object is associated with a window on your computer monitor Name of object, giving class name and its memory location (hexadecimal). Java creates name when it creates object JFrame@25c7f37d hide() show() setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() setSize(int,int) … JFrame Object contains methods (functions and procedures), which can be called to operate on the object Function: returns a value; call on it is an expression Procedure: does not return a value; call on it is a statement

Evaluation of new-expression creates an object new javax.swing.JFrame() creates an object and gives as its value the name of the object JFrame@25c7f37d If evaluation creates this object, value of expression is JFrame@25c7f37d JFrame@25c7f37d hide() show() setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() setSize(int,int) … JFrame 2 + 3 + 4 9

A class variable contains the name of an object Type JFrame: Names of objects of class JFrame Consequence: a class variable contains not an object but name of an object, pointer to it. Objects are referenced indirectly. javax.swing.JFrame h; h= new javax.swing.JFrame(); If evaluation of new-exp creates the object shown, name of object is stored in h JFrame@25c7f37d hide() show() setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() setSize(int,int) … JFrame h ? JFrame JFrame@25c7f37d

A class variable contains the name of an object If variable h contains the name of an object, you can call methods of the object using dot-notation: Procedure calls: h.show(); h.setTitle(“this is a title”); Function calls: h.getX() h.getX() + h.getWidth() JFrame@25c7f37d hide() show() setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() setSize(int,int) … JFrame x= y; g= h; h ? JFrame JFrame@25c7f37d

Class definition: a blueprint for objects of the class Class definition: Describes format of an object (instance) of the class. /** description of what the class is for */ public class C { } This is a comment Access modifier public means C can be used anywhere declarations of methods (in any order) Class definition C goes in its own file named C.java On your hard drive, have separate directory for each Java project you write; put all class definitions for program in that directory. You’ll see this when we demo.

First class definition /** An instance (object of the class) has (almost) no methods */ public class C { } k ? C C@25c7fd38 Then, execution of C k; k= new C(); creates object shown to right and stores its name in k C@25c7fd38 C

Class extends (is a subclass of) JFrame /** An instance is a subclass of JFrame */ public class C extends javax.swing.JFrame { } C@6667f34e JFrame C: subclass of JFrame JFrame: superclass of C C inherits all methods that are in a JFrame hide() show() setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() … C Object has 2 partitions: one for JFrame methods, one for C methods Easy re-use of program part!

Class definition with a function definition /** An instance is a subclass of JFrame with a function area */ public class C extends javax.swing.JFrame { /** Return area of window */ public int area() { return getWidth() * getHeight(); } Spec, as a comment Function calls automatically call functions that are in the object You know it is a function because it has a return type C@6667f34e JFrame … getWidth() getHeight() area() C

Inside-out rule for finding declaration /** An instance … */ public class C extends javax.swing.JFrame { /** Return area of window */ public int area() { return getWidth() * getHeight(); } The whole method is in the object To what declaration does a name refer? Use inside-out rule: Look first in method body, starting from name and moving out; then look at parameters; then look outside method in the object. C@6667f34e getWidth() getHeight() … area() { return getWidth() * getHeight(); } JFrame C

Inside-out rule for finding declaration /** An instance … */ public class C extends …JFrame { /** Return area of window */ public int area() { return getWidth() * getHeight(); } Function area: in each object. getWidth() calls function getWidth in the object in which it appears. C@2abcde14 getWidth() getHeight() … area() { return getWidth() * getHeight(); } JFrame C C@6667f34e getWidth() getHeight() … area() { return getWidth() * getHeight(); } JFrame C

Class definition with a procedure definition /** An instance is a JFrame with more methods */ public class C extends javax.swing.JFrame { public int area() { return getWidth() * getHeight(); } /** Set width of window to its height */ public void setWtoH() { setSize(getHeight(), getHeight()); C@6667f34e … setSize(int, int) getWidth() getHeight() area() setWtoH() JFrame It is a procedure because it has void instead of return type Call on procedure setSize C

Using an object of class Date /** An instance is a JFrame with more methods */ public class C extends javax.swing.JFrame { … /** Put the date and time in the title */ public void setTitleToDate() { } setTitle((new java.util.Date()).toString()); C@6667f34e … setSize(int, int) setTitle(String) area() { } setWtoH() setTitleToDate JFrame An object of class java.util.Date contains the date and time at which it was created. It has a function toString(), which yields the data as a String. C

About null v1 C@16 v2 null getName() null denotes the absence of a name. v2.getName() is a mistake! Program stops with a NullPointerException You can write assignments like: v1= null; and expressions like: v1 == null

Hello World! /** A simple program that prints Hello, world! */ public class myClass { /** Called to start program. */ public static void main(String[ ] args) { System.out.println("Hello, world!"); } args is an array of String elements We explain static next week. Briefly: there is only one copy of procedure main, and it is not in any object