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

Similar documents
CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

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

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

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

CS/ENGRD 2110 FALL2017. Lecture 4: The class hierarchy; static components

Lecture 4: The class hierarchy; static components

Lecture 4: The class hierarchy; static components

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

JFrame In Swing, a JFrame is similar to a window in your operating system

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

CS/ENGRD 2110 SPRING Lecture 3: Fields, getters and setters, constructors, testing

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

CS/ENGRD 2110 FALL 2018

Java Object Oriented Design. CSC207 Fall 2014

Fundamentals of Object Oriented Programming

More on Exception Handling

CS/ENGRD 2110 FALL Lecture 4: The class hierarchy; static components

Adam Blank Lecture 3 Autumn 2016 CSE 143. Computer Programming II

More on Exception Handling

CSE 143. More ArrayIntList; pre/post; exceptions; debugging. Computer Programming II. CSE 143: Computer Programming II

EXCEPTION HANDLING. Summer 2018

Principles of Software Construction: Objects, Design, and Concurrency. Objects (continued) toad. Spring J Aldrich and W Scherlis

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

C a; C b; C e; int c;

Full file at Chapter 2 - Inheritance and Exception Handling

Recitation 3. 2D Arrays, Exceptions

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

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

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

To Think About. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Programming II (CS300)

CSE 331 Software Design & Implementation

ITI Introduction to Computing II

ITI Introduction to Computing II

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

CS1004: Intro to CS in Java, Spring 2005

CS 3 Introduction to Software Engineering. 3: Exceptions

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

BBM 102 Introduction to Programming II Spring Exceptions

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Programming Languages and Techniques (CIS120)

CS 11 java track: lecture 3

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

JAVA BASICS II. Example: FIFO

Chapter 6: Inheritance

Advances in Programming Languages

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Constants are named in ALL_CAPS, using upper case letters and underscores in their names.

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

BBM 102 Introduction to Programming II Spring 2017

Introduction. Exceptions: An OO Way for Handling Errors. Common Runtime Errors. Error Handling. Without Error Handling Example 1

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

COMP 401 Fall Recitation 6: Inheritance

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

CS/ENGRD 2110 SPRING 2018

Assumptions. History

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

The Liskov Substitution Principle

Prelim 1. CS 2110, March 15, 2016, 5:30 PM Total Question Name True False. Short Answer

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

COMP1008 Exceptions. Runtime Error

Java Loose Ends. 11 December 2017 OSU CSE 1

CS/ENGRD 2110 SPRING 2019

CS/ENGRD 2110 SPRING 2019

Javadoc. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 7

CSCI 261 Computer Science II

CS/ENGRD 2110 SPRING 2018

CmpSci 187: Programming with Data Structures Spring 2015

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Programming II (CS300)

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

Question 0. (out of 01) Question 1. (out of 10) Question 2. (out of 12) Question 3. (out of 12) Question 4. (out of 12) Question 5.

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

CS159. Nathan Sprague

CS/ENGRD 2110 SPRING Lecture 4: The class hierarchy; static components

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

CS61B Lecture #12. Today: Various odds and ends in support of abstraction.

Lecture 7. Log into Linux New documents posted to course webpage

Classes, interfaces, & documentation. Review of basic building blocks

Object Oriented Programming and Design in Java. Session 10 Instructor: Bert Huang

Readings for This Lecture

Programming Languages and Techniques (CIS120)

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

1Z0-808 oracle. Number: 1Z0-808 Passing Score: 800 Time Limit: 120 min.

Defining Classes and Methods

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

Lecture 1. Course Overview Types & Expressions

Express Yourself. Writing Your Own Classes

Review sheet for Final Exam (List of objectives for this course)

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

Object Oriented Programming

Recreation. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

CS/ENGRD 2110 SPRING Lecture 4: The class hierarchy; static components

Transcription:

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

Homework HW1 2 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 @34, (find a link to it 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

PPT slides, JavaHyperText. 3 CMS. Visit course webpage, click Links, then CMS for 2110. 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

Try Java out in https://tryjshell.org 4 On Piazza note @29, Preston Rozwood talked about JShell. Problem: You need Java 9 to use it. Maybe next semester we ll switch to 9. Eric Wang then suggested using https://tryjshell.org Where you can type in Java snippets and have them executed/evaluated. It s not as easy to use as DrJava, but it can help in some cases. You don t need Java version 9 to use it. It s not using the Java on your computer. Thank, Preston and Eric!

Java OO (Object Orientation) 5 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 6 1. Study material of this lecture. 2. 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 3. Spend a few minutes perusing slides for lecture 3; bring them to lecture 3.

Java OO 7 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 Methods may have parameters Method calls may have arguments 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.

Drawing an object of class javax.swing. 8 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 @25c7 hide() show() settitle(string) gettitle() getx() gety() setlocation(int, int) getwidth() getheight() setsize(int,int) 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 9 Evaluation of new javax.swing.() creates an object and gives as its value the name of the object If evaluation creates this object, value of expression is @25c7 9 2 + 3 + 4 @25c7 @25c7 hide() show() settitle(string) gettitle() getx() gety() setlocation(int, int) getwidth() getheight() setsize(int,int)

A class variable contains the name of an object 10 Type : Names of objects of class javax.swing. h; h= new javax.swing.(); If evaluation of new-exp creates the object shown, name of object is stored in h h @25c7? Consequence: a class variable contains not an object but name of an object, pointer to it. Objects are referenced indirectly. @25c7 hide() show() settitle(string) gettitle() getx() gety() setlocation(int, int) getwidth() getheight() setsize(int,int)

A class variable contains the name of an object 11 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() x= y; g= h; h @25c7? @25c7 hide() show() settitle(string) gettitle() getx() gety() setlocation(int, int) getwidth() getheight() setsize(int,int)

Class definition: a blueprint for objects of the class 12 Class definition: Describes format of an object (instance) of the class. /** description of what the class is for */ public class C { declarations of methods (in any order) This is a comment Access modifier public means C can be used anywhere 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 13 /** An instance (object of the class) has (almost) no methods */ public class C { Then, execution of C k; k= new C(); creates object shown to right and stores its name in k k C@25c7? C@25c7 C C

Class extends (is a subclass of) 14 /** An instance is a subclass of */ public class C extends javax.swing. { C: subclass of : superclass of C C inherits all methods that are in a Object has 2 partitions: one for methods, one for C methods C@6667 hide() show() settitle(string) gettitle() getx() gety() setlocation(int, int) getwidth() getheight() C Easy re-use of program part!

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

Inside-out rule for finding declaration 16 /** An instance */ public class C extends javax.swing. { /** Return area of window */ public int area() { return getwidth() * getheight(); 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() The whole method is in the object area() { C return getwidth() * getheight();

Inside-out rule for finding declaration 17 /** An instance */ public class C extends { /** Return area of window */ public int area() { return getwidth() * getheight(); C@2abc getwidth() getheight() area() { C return getwidth() * getheight(); Function area: in each object. getwidth() calls function getwidth in the object in which it appears. C@6667 getwidth() getheight() area() { C return getwidth() * getheight();

Class definition with a procedure definition 18 /** An instance is a with more methods */ public class C extends javax.swing. { public int area() { return getwidth() * getheight(); /** Set width of window to its height */ public void setwtoh() { setsize(getheight(), getheight()); Call on procedure setsize It is a procedure because it has void instead of return type C@6667 setsize(int, int) getwidth() getheight() area() setwtoh() C

Using an object of class Date 19 /** An instance is a with more methods */ public class C extends javax.swing. { /** Put the date and time in the title */ public void settitletodate() { settitle((new java.util.date()).tostring()); C@6667 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. setsize(int, int) settitle(string) area() { C setwtoh() settitletodate

About null 20 v1 C@16 v2 null C@16 C 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 21 7 int x= 5; 8 System.out.println("x is now "+x); 9 assert x== 6; 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. AssertionError@2 Throwable Error AssertionError java.lang.assertionerror at A0.main(A0.java:9)

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

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