CS/ENGRD 2110 SPRING 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 FALL 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

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

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

Lecture 4: The class hierarchy; static components

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

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

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Java Object Oriented Design. CSC207 Fall 2014

1B1b Classes in Java Part I

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

ITI Introduction to Computing II

ITI Introduction to Computing II

CS1004: Intro to CS in Java, Spring 2005

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

1.00 Lecture 8. Using An Existing Class, cont.

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

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

Some client code. output. subtype polymorphism As dynamic binding occurs the behavior (i.e., methods) follow the objects. Squarer

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

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

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

Object Orientated Analysis and Design. Benjamin Kenwright

Java Programming Lecture 7

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

PowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien

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

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

Programming Languages and Techniques (CIS120)

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

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

Advances in Programming Languages

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Inheritance (Part 5) Odds and ends

CS1150 Principles of Computer Science Objects and Classes

Chapter 6: Inheritance

3.1 Class Declaration

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

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

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

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

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

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

CS/120 Final Exam. Name

Object-oriented programming in Java (2)

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

Programming overview

CS-202 Introduction to Object Oriented Programming

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

Example: Fibonacci Numbers

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Inheritance Systems. Merchandise. Television Camcorder Shirt Shoe Dress 9.1.1

Object-Oriented Concepts

Creating Java Programs with Greenfoot

Chapter 4 Defining Classes I

Creating Java Programs with Greenfoot

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

UI Software Organization

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

We are on the GUI fast track path

Reusing Classes. Hendrik Speleers

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Arrays Classes & Methods, Inheritance

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Graphical User Interface

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

Programming Languages and Techniques (CIS120)

Slide 1 CS 170 Java Programming 1

One of these "compartments" is more correctly referred to as an element of the array

CSC 1214: Object-Oriented Programming

ITI Introduction to Computing II

Exceptions and Libraries

OBJECT ORİENTATİON ENCAPSULATİON

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

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

JAVA: A Primer. By: Amrita Rajagopal

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

YEAH session #6. 6 March 2014, 5:30p-6:30p Miles Seiver

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CS 1302 Chapter 9 (Review) Object & Classes

Name CS/120 Sample Exam #1 -- Riley. a) Every program has syntax, which refers to the form of the code, and, which refers to the meaning of the code.

Lecture 3: Java Graphics & Events

CS/ENGRD 2110 SPRING 2018

CSC9T4: Object Modelling, principles of OO design and implementation

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

Everything is an object. Almost, but all objects are of type Object!

Introduction to Java

Final Examination Semester 3 / Year 2010

Review: Array Initializer Lists

Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Subtypes and Subclasses

Inheritance (Part 2) Notes Chapter 6

CMSC 132: Object-Oriented Programming II

Transcription:

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

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

Java OO 4 References to course text and JavaSummary.pptx Objects: B.1 slide 10-16 Calling methods: B.2-B.3 slide 18 Class definition: B.5 slide 11 public, private: B.5 slide 11, 12 Indirect reference, aliasing: B.6 slide 17 Method declarations: B.7 Parameter vs argument: B.12-B.14 slide 14 Methods may have parameters Method calls may have arguments 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

Drawing an object of class javax.swing. 5 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 @25c7f37d 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 is a statement to do something

Evaluation of new-expression creates an object 6 Evaluation of @25c7f37d 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 @25c7f37d 9 2 + 3 + 4 @25c7f37d hide() show() settitle(string) gettitle() getx() gety() setlocation(int, int) getwidth() getheight() setsize(int,int)

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

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

Class definition 9 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 program you write; put all class definitions for program in that directory. You ll see this when we demo Eclipse

First class definition 10 /** 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@25c7fd38? C@25c7fd38 C C

Class extends (is a subclass of) 11 /** An instance is a subclass of Jframe */ 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@6667f34e 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 12 /** 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@6667f34e You know it is a function because it has a return type getwidth() getheight() area() C

Inside-out rule for finding declaration 13 /** 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@6667f34e getwidth() getheight() The whole method is in the object area() { C return getwidth() * getheight();

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

Class definition with a procedure definition 15 /** 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@6667f34e setsize(int, int) getwidth() getheight() area() setwtoh() C

Using an object of class Date 16 /** 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@6667f34e 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 17 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

Hello World! 18 /** 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!"); We explain static next week. Briefly: there is only one copy of procedure main, and it is not in any object args is an array of String elements