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

Slides:



Advertisements
Similar presentations
CS/ENGRD 2110 SPRING 2015 Lecture 4: The class hierarchy; static components 1.
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
1 CS Sept. Customizing a class & testing Quiz 2 on Tuesday 15 Sept Purpose of a constructor (slide 5) Evaluating a new expression (slide 6) Fields;
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.
Introduction to Methods
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.
UML Basics & Access Modifier
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.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
0 Teaching Java —with OO first David Gries Computer Science Cornell University Ithaca, NY
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.
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.
Chapter 2 Getting Started with Java. Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java.
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.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
4.3.1 Non-void Methods Parameters are largely one-way communication.  Shared instances variables is one way to accomplish this. calling codemethod parameter.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
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 Class 1 Lecture Topic Concepts, Definitions and Examples.
© 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.
OOP Basics Classes & Methods (c) IDMS/SQL News
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 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/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
CS Feb 2010 Customizing a class
Inheritance ITI1121 Nour El Kadri.
Examples of Classes & Objects
Some Eclipse shortcuts
CS/ENGRD 2110 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
CS100J. Lecture 2, 24 September Objects & classes
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
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
Classes & Objects: Examples
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.
CS/ENGRD 2110 Fall 2018 Lecture 4: The class hierarchy; static components
CS100J 06 September 2005 The class definition
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
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Methods/Functions.
CS Feb 2011 Customizing a class
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

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

Thursday, 5 Sept. Lecture in Bailey Hall 2 Day Hall We are here Barton Hall Malott Hall Bailey Campus Road Tower Road East Ave Garden Ave

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. 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 Precondition and postcondition 3. 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 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 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 6 Object is associated with a window on your computer monitor hide() show() setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() setSize(int,int) … JFrame Name of object, giving class name and its memory location (hexadecimal). Java creates name when it creates object Function: returns a value; call is an expression Procedure: does not return a value; call is a statement to do something Object contains methods (functions and procedures), which can be called to operate on the object

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

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

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: hide() show() setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() setSize(int,int) … JFrame h ? Procedure calls: h.show(); h.setTitle(“this is a title”); Function calls: h.getX() h.getX() + h.getWidth()

Class definition 10 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 declarations of methods (in any order) 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 11 /** An instance (object of the class) has (almost) no methods */ public class C { } C k ? C Then, execution of C k; k= new C(); creates object shown to right and stores its name in k

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 hide() show() setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() … JFrame 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 13 /** An instance is a subclass of JFrame with an area function */ public class C extends javax.swing.JFrame { /** Return area of window */ public int area() { return getWidth() * getHeight(); } … getWidth() getHeight() area() JFrame C Spec, as a comment You know it is a function because it has a return type Function calls automatically call functions that are in the object

Inside-out rule for finding declaration 14 /** An instance … */ public class C extends javax.swing.JFrame { /** Return area of window */ public int area() { return getWidth() * getHeight(); } getWidth() getHeight() … area() { return getWidth() * getHeight(); } JFrame C 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.

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

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

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

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

Hello World! 19 /** 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