Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Similar documents
Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

LECTURE 2 (Gaya College of Engineering)

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Classes Classes 2 / 35

CS 11 java track: lecture 1

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

JAVA: A Primer. By: Amrita Rajagopal

Programming Language Concepts: Lecture 2

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Chapter 4 Defining Classes I

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

CS 231 Data Structures and Algorithms, Fall 2016

Lecture 02, Fall 2018 Friday September 7

CS1004: Intro to CS in Java, Spring 2005

CS 335 Lecture 02 Java Programming

Kickstart Intro to Java Part I

CMSC202 Computer Science II for Majors

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Getting started with Java

An overview of Java, Data types and variables

Introduction to Programming Using Java (98-388)

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others

Lab5. Wooseok Kim

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Selected Questions from by Nageshwara Rao

BM214E Object Oriented Programming Lecture 8

Methods. Methods. Mysteries Revealed

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Anatomy of a Class Encapsulation Anatomy of a Method

Object Oriented Programming in C#

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Computer Science II (20082) Week 1: Review and Inheritance

CHAPTER 7 OBJECTS AND CLASSES

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CHAPTER 7 OBJECTS AND CLASSES

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

Who and what can help? Inf1-OP. Lecturer: Timothy Hospedales TA: Natalia Zon

Instance Members and Static Members

Introduction Basic elements of Java

1. Download the JDK 6, from

CS 335 Java Programming Controls. Fall 2007

Program Fundamentals

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

Preview from Notesale.co.uk Page 3 of 36

Inf1-OOP. Textbooks. Who and What. Organizational Issues. Why Java? Course Overview. Hello, World! in Java. Ewan Klein, Perdita Stevens

ITI Introduction to Computing II

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

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

Inf1-OOP. Textbooks. Who and What. Organisational issues. Why Java? Course Overview. Hello, World! in Java

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

} Each object in a Java program has an identifier (name) } This includes:

CS Week 2. Jim Williams, PhD

CS 251 Intermediate Programming Methods and Classes

Classes. Classes as Code Libraries. Classes as Data Structures

CS 251 Intermediate Programming Methods and More

Day 2. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Inf1-OP. Course Overview. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

1.00 Lecture 8. Using An Existing Class, cont.

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1.

Building Java Programs

Administrative Stuff. Inf1-OP. Additional help? Who to contact for help? Course Overview

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

CS1083 Week 2: Arrays, ArrayList

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Objects and Classes -- Introduction

Getting started with Java

Midterms Save the Dates!

CS 177 Recitation. Week 1 Intro to Java

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

Non-instantiable Classes

CS11 Java. Fall Lecture 1

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Programming for Engineers in Python

Classes Classes 2 / 36

Computer Science is...

Creating an object Instance variables

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

CSEN401 Computer Programming Lab. Topics: Introduction and Motivation Recap: Objects and Classes

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

Assignment 1 due Monday at 11:59pm

Final Exam Practice. Partial credit will be awarded.

Review: Using Imported Code. What About the DrawingGizmo? Review: Classes and Object Instances. DrawingGizmo pencil; pencil = new DrawingGizmo();

CS1004: Intro to CS in Java, Spring 2005

Transcription:

Administration Classes CS 99 Summer 2000 Michael Clarkson Lecture 7 Lab 7 due tomorrow Question: Lab 6.equals( SquareRoot )? Lab 8 posted today Prelim 2 in six days! Covers two weeks of material: lectures 5-7 labs 5-8 Review: Monday, 7:30-8:30pm, Philips 203 Exam: Tuesday, in class Upson 215 (10:00) Upson B17 (11:30) 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 2 Agenda Objects Part II Command Line Arguments Classes Objects Part II Object aliases Objects as arguments 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 3 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 4 Review: Object References Recall that declaring an object and creating (instantiating) it are two separate steps We declare a reference, and that reference refers to an object: Car mycar; Object Aliases More than one reference can refer to the same object, e.g.: Car mycar, yourcar; mycar = new Car(); yourcar = mycar; This is called aliasing 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 5 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 6 1

Object Aliases [2] Object Aliases [3] mycar yourcar Anything that happens to an alias, happens to the real object So: yourcar.honkhorn(); Honks my car s horn! 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 7 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 8 Object Equality References are why the == operator doesn t work like you might suspect on objects ==returns whether the references are equal, not whether the data contained by the objects are equal In general, you have to use the equals method of an object to determine equality Objects as Arguments Objects can be used as arguments to methods This is nothing new: static void System.out.println(String s) There is a difference between passing primitive types and objects to methods 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 9 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 10 Objects as Arguments [2] Calling Semantics public static void main(string[] args) { int a = 0; v1(a); System.out.println(a); String s = Hello ; v2(s); System.out.println(s); static void v1(int i) { i++; static void v2(string s) { s= Goodbye 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 11 Call by value: changes to value are not reflected outside of method used for primitive types used for objects (i.e., references) reference itself is not changed, BUT changes to object are reflected outside of method 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 12 2

Objects as Arguments [3] class Car { String color = Blue ; public static void main(string[] args) { Car car = new Car(); repaint(car); System.out.println(car.color); Command Line Arguments What Metrowerks is hiding from you More of main revealed static void repaint(car car) { car.color = Red ; 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 13 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 14 What Metrowerks is Hiding Without an IDE like Metrowerks, you compile a Java program on the command line: C:\> javac HelloWorld.java That produces a file named HelloWorld.class that you then run by typing: C:\> java HelloWorld Running Java Programs You can also provide more information than the name of the program: C:\> java ReverseWords red green der neerg Words after the name of the program are called command line arguments Another way of getting input to a program 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 15 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 16 main public static void main(string[] args) Command line arguments are passed to your main method as an array of Strings Each word after the name of the program becomes one element of the array So given: java ReverseWords red green args[0] is red args[1] is green 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 17 Classes Class anatomy Fields Methods Files this Accessing fields and methods Constructors Instance data Modifiers public, private, static 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 18 3

Class Anatomy Recall that classes have two types of members: Fields Methods class Car { int speed; int numdoors; void accelerate(int amount) {... 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 19 Class Anatomy [2] Classes are usually defined in separate.java files Files have the same name as the class they contain So for labs where you create classes, you ll have several.java files to turn in 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 20 Scope of Fields Fields have scope throughout the entire class Can be used in any method of the class by writing their name, e.g.: void accelerate(int amount) { speed += amount; Can be used outside of the class by preceding the field name with an object reference, e.g.: Car mycar; mycar.speed = 55; Invoking Methods To call a method from outside of the class, use an object reference, e.g.: mycar.turnleft(); To call a method of a class from inside the same class, no reference is needed, e.g.: void turnleft() { accelerate(-10);... // slow down 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 21 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 22 Constructors Constructors are special methods defined inside a class A constructor is called whenever an object of the class is created Constructors are used to initialize an object s fields properly Also used for convenience 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 23 Constructors [2] A constructor has the same name as the class it is in no return type whatever parameter list you want class Point { double x, y; Point(int initx, int inity) { x = initx; y = inity; Point p = new Point(1, 2); 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 24 4

Constructors [3] Very simple constructors don t even have any parameters default constructor class RentalCar { int gasleft; RentalCar() { gasleft = FULL_TANK; RentalCar car = new RentalCar(); 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 25 Constructors [4] You can include as many constructors as you want inside a class Java picks the right one to call based on the types of the arguments you use when constructing an object If you provide no constructors, Java automatically provides a default constructor that takes no arguments and basically does nothing 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 26 Instance Data The data contained by one particular object is called instance data So for Points, the x and y coordinates are instance data Each object has its own separate memory reserved for its instance data It can be different than the data contained by any other object of the same class Instance Data [2] Instance data is not shared between objects Point p1 = new Point(0, 0); Point p2 = new Point(-1, 4); p1.x!= p2.x p1.y!= p2.y 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 27 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 28 Instance Data [3] Objects are responsible for managing their own instance data Users of the object usually aren t allowed access to instance data, except through methods defined by the object s class Objects are self-governing 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 29 Visibility Modifiers Visibility modifiers are keywords used in declaring members They make members either visible or invisible from outside the class public A public member is accessible outside of the class using the reference.member syntax private A private member is not accessible outside of the class It is accessible from inside any method of the class 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 30 5

tostring() One method that is common to have in many classes is tostring() It is called whenever an object of the class is used in a concatenation operation public String tostring() { return ( + x +, + y + ) ; main public static void main(string[] args) main is public to make it accessible from outside the class it is declared in Otherwise, Java couldn t call your main method System.out.println(p1); 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 31 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 32 Publicity Guidelines Usually, all fields are private Public methods are for users of the class Private methods are called from public methods to help implement them But aren t meant for users of the class to call static static is a modifier Indicates that the member is shared by all instances of a class For fields: the data is shared by all objects if one object changes it, it s changed for every object best use is for class-wide constants (static final) 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 33 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 34 static Methods Static methods have no associated object No instance data involved Not invoked using the reference.method() syntax Invoked with Class.method() syntax static Methods [2] Examples: Math.sqrt() Console.readInt() Since they have no associated object, not allowed to use instance variables 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 35 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 36 6

main public static void main(string[] args) main is static because it belongs to no particular object belongs to entire program We now know what every part of main means! 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 37 System System is a class that contains useful methods for working with the computer system on which a program runs out is a static field of System with the type PrintStream, e.g.: class System { public static PrintStream out;... 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 38 println() So when we invoke the println method, we re saying: In the System class, Get the out object, and Invoke the println() method on it Next Time Some theory behind object-oriented programming Advanced usage of classes 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 39 7/19/00 CS 99 Summer 2000 Michael Clarkson Lecture 7 40 7