CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java

Slides:



Advertisements
Similar presentations
Recitation 4. 2-D arrays. Exceptions. Animal[] v= new Animal[3]; 2 declaration of array v v null Create array of 3 elements a6 Animal[] null Assign.
Advertisements

CS/ENGRD 2110 SPRING 2015 Lecture 4: The class hierarchy; static components 1.
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.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
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.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
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.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
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.
CS/ENGRD 2110 SPRING 2015 Lecture 6: Consequence of type, casting; function equals 1.
0 Teaching Java —with OO first David Gries Computer Science Cornell University Ithaca, NY
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
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.
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.
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.
4.3.1 Non-void Methods Parameters are largely one-way communication.  Shared instances variables is one way to accomplish this. calling codemethod parameter.
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.
Recitation 3 2D Arrays, Exceptions. 2D arrays 2D Arrays Many applications have multidimensional structures: ●Matrix operations ●Collection of lists ●Board.
© 2006 Pearson Addison-Wesley. All rights reserved Non-void Methods Parameters are largely one-way communication.  Shared instances variables is.
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.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
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.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
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.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
CS Feb 2010 Customizing a class
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Recitation 2 Exception handling.
CS/ENGRD 2110 Fall 2017 Lecture 3: Fields, getters and setters, constructors, testing
Some Eclipse shortcuts
Testing and Debugging.
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
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.
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2017 Lecture 4: The class hierarchy; static components
CS/ENGRD 2110 Fall2017 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 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
Algorithm Correctness
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
Sit next to someone. Today, we do some work in pairs.
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 5: Local vars; Inside-out rule; constructors
Defining Classes and Methods
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Classes and Methods 15-Aug-19.
CS Feb 2011 Customizing a class
Presentation transcript:

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

Homework HW1 The answers you handed in at the end of lecture 1 showed mass confusion! Perhaps 80% of you weren’t sure what to write. This was not graded! It was only to help us and you assess the situation. Doing HW1 will eliminate the confusion. Piazza note @30, (it is linked to in the pinned Piazza Recitation/Homework note.) ------------------------------------------------------------- Evaluation, Execution, Syntax, Semantics. Presenting an algorithm in English (2.5 minutes). Executing the assignment statement (2.5 minutes). Do HW1 and submit on the CMS

CMS VideoNote.com, PPT slides, JavaHyperText. CMS. Visit course webpage, click “Links”, then “CMS for 2110”. Videos of lectures from last semester: 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! Got a Java question? See first if it’s answered on JavaHyperText

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, a blueprint for objects, and how it contains definitions of methods (functions and procedures) that appear in each object of the class. Third: Talk about keyword null. Fourth: Introduce Exceptions

Homework Study material of this lecture. Visit JavaHyperText, click on Code Style. 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: object Calling methods: method call Class definition: class public, private: public private method Parameter vs argument: parameter, argument Inside-out rule Fields of an object may be mentioned. We cover these in next lecture Function: a method that returns a result. Procedure: method that does not return a result, void method. 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@25c7 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@25c7 If evaluation creates this object, value of expression is JFrame@25c7 JFrame@25c7 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@25c7 hide() show() setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() setSize(int,int) … JFrame h ? JFrame JFrame@25c7

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@25c7 hide() show() setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() setSize(int,int) … JFrame x= y; g= h; h ? JFrame JFrame@25c7

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@25c7 Then, execution of C k; k= new C(); creates object shown to right and stores its name in k C@25c7 C

Class extends (is a subclass of) JFrame /** An instance is a subclass of JFrame */ public class C extends javax.swing.JFrame { } C@6667 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@6667 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@6667 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@2abc getWidth() getHeight() … area() { return getWidth() * getHeight(); } JFrame C C@6667 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@6667 … 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@6667 … 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

Intro to Exceptions int x= 5; System.out.println("x is now "+x); assert x== 6; AssertionError@2 Throwable Error AssertionError When the assert statement is executed and x is not 6, an object of class AssertionError is created and “thrown”. It contains info needed to print out a nice message. java.lang.AssertionError at A0.main(A0.java:9)

ArithmeticException@4 Intro to Exceptions m(); ArithmeticException@4 Throwable Exception RuntimeException ArithmeticException public static void m() { int y= 5/0; } When 5/0 is evaluated, an object of class ArithmeticException is created and “thrown”. It contains info needed to print out a nice message. Exception in thread "main” java.lang.ArithmeticException: / by zero at A0.m(A0.java:15) at A0.main(A0.java:6) where it occurred where m was called

Intro to Exceptions You will learn all about exceptions in next week’s recitation! Throwable Error IOException AssertionException … Exception RuntimeException ArithmeticException NullPointerException IllegalArgumentException