0% found this document useful (0 votes)
69 views19 pages

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

1. The document summarizes a lecture on objects and classes in Java. It describes how objects are created from classes and how methods can be defined to operate on objects of a class. 2. Key concepts covered include defining a class, creating objects from a class, using dot notation to call methods on an object, and defining both functions and procedures as methods in a class. 3. An example class is provided that extends an existing JFrame class, overriding and adding new methods like an area function and a method to set the title to the current date.

Uploaded by

Adam Holmes
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views19 pages

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

1. The document summarizes a lecture on objects and classes in Java. It describes how objects are created from classes and how methods can be defined to operate on objects of a class. 2. Key concepts covered include defining a class, creating objects from a class, using dot notation to call methods on an object, and defining both functions and procedures as methods in a class. 3. An example class is provided that extends an existing JFrame class, overriding and adding new methods like an area function and a method to set the title to the current date.

Uploaded by

Adam Holmes
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

1

CS/ENGRD 2110 SPRING 2012


Lecture 2: Objects and classes in Java http://courses.cs.cornell.edu/cs2110

Thursday, 5 Sept. Lecture in Bailey Hall


2

East Ave

Bailey Malott Hall

Tower Road Garden Ave

Day Hall We are here Barton Hall

Campus Road

Java OO (Object Orientation)


3

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 defines functions, and procedures that appear in each object of the class. Third (if there is time). Show you a Java application, a class with a static procedure with a certain parameter.

Homework
4

1. 2.

3.

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 Dont over-comment 3.4 Method specifications 3.4.1 Precondition and postcondition Spend a few minutes perusing next lecture slides; bring them to next lecture.

Java OO
5

References to course text and JavaSummary.pptx Objects: B.1 slide 10-16 Text mentions fields of Calling methods: B.2-B.3 slide 18 an object. We cover these in next lecture Class definition: B.5 slide 11 public, private: B.5 slide 11, 12 Indirect reference, aliasing: B.6 slide 17 Method declarations: B.7 Text uses value-producing Parameter vs argument: B.12-B.14 method for function and slide 14 void method for procedure. Methods may have parameters Method calls may have arguments Get used to terminology: function and procedure

Drawing an object of class javax.swing.JFrame


6

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() JFrame 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 is an expression Procedure: does not return a value; call is a statement to do something

Evaluation of new-expression creates an object


7

Evaluation of

JFrame@25c7f37d new javax.swing.JFrame()

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

Class variable contains the name of an object


8

Type JFrame: Names of objects of class JFrame h= new javax.swing.JFrame(); Consequence: a class variable contains not an object but the name of an object. Objects are referenced indirectly.

If evaluation of new-exp creates the object shown, name of object is stored in h JFrame@25c7f37d h JFrame@25c7f37d ? JFrame

hide() show() JFrame setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() setSize(int,int)

Class variable contains the name of an object


9

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

Class definition
10

Class denition: 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. Youll see this when we demo Eclipse

First class definition


11

/** 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 C@25c7fd38 C

Class extends (is a subclass of) JFrame


12

/** An instance is a subclass of JFrame*/ public class C extends javax.swing.JFrame { } C: subclass of JFrame JFrame: superclass of C C inherits all methods that are in a JFrame Object has 2 partitions: one for JFrame methods, one for C methods C@6667f34e JFrame 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


13

/** An instance is a subclass of JFrame with an area function */ public class C extends javax.swing.JFrame { Spec, as a comment /** Return area of window */ 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()

JFrame C

Inside-out rule for finding declaration


14

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

Inside-out rule for finding declaration


15

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

JFrame

getWidth() getHeight()

JFrame

C area() { return getWidth() * getHeight(); }

C area() { return getWidth() * getHeight(); }

Class definition with a procedure definition


16

/** 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()); } } Call on It is a procedure because it has void procedure instead of return type setSize C@6667f34e
JFrame setSize(int, int) getWidth() getHeight() area()
setWtoH()

Using an object of class Date


17

/** 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 } } JFrame An object of class java.util.Date setSize(int, int) contains date and time at which setTitle(String) created. area() { } It has a function toString(), which C setWtoH() setTitleToDate yields the data as a String.

About null
18


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

C@16 C

getName()

Hello World!
19

/** A simple program that prints Hello, world! */ public class myClass { args is an array of String elements /** 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

You might also like