Objects & Classes Weiss ch. 3. So far: –Point (see java.awt.Point) –String –Arrays of various kinds –IPAddress (see java.net.InetAddress) The Java API.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Lecture 4: Objects and Classes Classes Objects Data fields (instance variables) Methods –Instance methods –Static methods –Constructors But first…
Java Syntax Primitive data types Operators Control statements.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
March 2005Java Programming1. March 2005Java Programming2 Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Writing Classes (Chapter 4)
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Recitation 2 Main Method, API & Packages, Java Basics.
BPJ444: Business Programming using Java Java basics Tim McKenna
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
CSC Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
10-Nov-15 Java Object Oriented Programming What is it?
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
Lecture Notes – Classes and Objects (Ch 7-8) Yonglei Tao.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Midterm 2 Review 1. 2 Object Oriented Programming Write a Date class. It should contain fields for day, month, year, number of months per year, and number.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
More on Objects Mehdi Einali Advanced Programming in Java 1.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Software Development Java Classes and Methods
CompSci 230 Software Construction
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
CMSC 202 Static Methods.
Exercise on Java Basics
An Introduction to Java – Part I, language basics
The Building Blocks Classes: Java class library, over 1,800 classes:
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
CS 302 Week 9 Jim Williams.
Classes and Objects Static Methods
Suggested self-checks: Section 7.11 #1-11
Java Programming Language
Using java libraries CGS3416 spring 2019.
Classes and Objects Object Creation
Chapter 5 Classes.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Objects & Classes Weiss ch. 3

So far: –Point (see java.awt.Point) –String –Arrays of various kinds –IPAddress (see java.net.InetAddress) The Java API “library” has many more, organized into packages: –java.net: InetAddress, URL, etc. –javax.swing: Icon, JButton, JCheckBox, JWindow, etc. –java.lang: String, Compiler, etc. –java.io: File, FileInputStream, StreamTokenizer, etc. –thousands more

Objects Vs. Primitives Problem: –Every object in Java is an Object –An Object has an equals method (and a few others) –Primitive types are not objects Common scenario: Works for objects of any type But not for primitive types (int, double, etc.) e.g. allEquals(new int[5]);  compiler error boolean allEquals(Object[ ] array) { Object first = array[0]; for (int i = 1; i < array.length; i++) { if (! array[i].equals(first)) return false; }

Objects Vs. Primitives One (ugly) solution: boolean allEquals(int[ ] array) { int first = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] != first) return false; } boolean allEquals(double[ ] array) { double first = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] != first) return false; } boolean allEquals(char[ ] array) { char first = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] != first) return false; } and 5 others!

Wrappers A better solution: wrap each primitive type in an object Integer i = new Integer(5); Integer j = new Integer(3); boolean b = i.equals(j); Now we can use original code to compare: Integer [ ] array; Double [ ] array; Character [ ] array; Also provides a convenient place to put methods relating to each primitive type: e.g., Integer.MAX_INT; Integer.parseInt( ), etc.

User Defined Types Objects are used by a program to encapsulate state and functionality Each object has a type Classes define the type of objects –specify per-object state and global state –specify per-object operations and global state –specify constructors to create new objects

Example Class public class Date { private int month; private int day; private int year; public Date( ) { month = 1; day = 1; year = 1998; } public Date( int theMonth, int theDay, int theYear ) { month = theMonth; day = theDay; year = theYear; } … } fields – one copy per Date instance zero-argument constructor – initializes with defaults three-argument constructor – initializes with given info

Instance Methods public class Date { … // Conversion to String public String toString( ) { return month + "/" + day + "/" + year; } public boolean equals( Object rhs ) { if( ! ( rhs instanceof Date ) ) return false; Date rhDate = ( Date ) rhs; return rhDate.month == month && rhDate.day == day && rhDate.year == year; } check to see if compared against another Date object change an Object reference into a Date reference instance method: operates on an instance of Date

Using Date public static void main( String [ ] args ) { Date d1 = new Date( ); Date d2 = new Date( 1, 1, 1998 ); Date d3 = new Date( 1, 1, 1999 ); System.out.println( "Date 1: " + d1 ); System.out.println( "Date 2: " + d2 ); System.out.println( "Date 3: " + d3 ); System.out.println( "Date1==Date2?: " + d1.equals( d2 ) ); System.out.println( "Date1==Date3?: " + d1.equals( d3 ) ); } String concatenation automatically calls Date.toString( ) This is equivalent to: “Date 2: “ + d2.toString() calling an instance method

Getter and Setter Methods public class Date { … // “getter” and “setter” methods public int getMonth( ) { return month; } public void setMonth(int m) { month = m; } } d1.setMonth(d1.getMonth() + 1) Instance variable month is private. Question: why bother?

this Often nice to use same name for parameter and for instance variable Often nice to have a reference to “the current object” inside an instance method public Date( int theMonth, int theDay, int theYear ) { … } public void setMonth(int m) { … } public Date( int month, int day, int year ) { this.month = month; this.day = day; this.year = year; System.out.println( " Just constructed a new Date object: " + this); } public void setMonth( int month) { if (month >= 1 && month <= 12) this.month = month; else … }

this Conceptually, dynamic calls: are translated into: public void setMonth(int month) { this.month = month; } d1.setMonth(5) public void setMonth(Date this, int month) { this.month = month; } setMonth(d1, 5) note: many languages do this literally e.g. Python

Mixing Static and Instance Members Rule: static members  one global copy instance members  one copy per instance Only one count variable, shared among all Dates Initialized once, when class is loaded static methods may access only static variables public class Date { private static int count = 0; public Date( int month, int day, int year ) { count++; this.month = month; this.day = day; this.year = year; } public static int howMany( ) { return count; } } accessing static variable new Date(1,1,2004); new Date(7,6,2004); int i = Date.howMany( ); static “getter” method

this as a constructor Bug in last example: forgot one Date constructor –Works: add the “count++” line to every constructor –Better: all defer to a single constructor, using this public class Date { private static int count = 0; public Date( ) { this(1, 1, 1998); } public Date( int month, int day, int year ) { count++; this.month = month; this.day = day; this.year = year; } … } public class Date { private static int count = 0; public Date( ) { count++; month = 1; day = 1; year = 1998; } public Date( int month, int day, int year ) { this( ); // begin with defaults this.month = month; // then modify this.day = day; this.year = year; } … Note: either way, must be first line of constructor.