Defining New Types of Objects, part 3

Slides:



Advertisements
Similar presentations
1 Building Java Programs Chapter 8: Classes These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: #1-9 exercises: #1-4.
Based on slides at buildingjavaprograms.com Objects and Classes, take 2 Managing Complexity with Programmer-Defined Types  Classes are programmer-defined.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: #1-9 exercises: #1-4.
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
CSCI 162 Classes.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Topic 21 arrays - part 1 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from "Should.
Defining Your Own Classes II
Building Java Programs
Building Java Programs
Dr. Kyung Eun Park Summer 2017
Midterm Review Problems
Building Java Programs
Building Java Programs
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
this keyword this : A reference to the implicit parameter Syntax:
Building Java Programs
Building Java Programs Chapter 7
this keyword this : A reference to the implicit parameter Syntax:
CSE 143 Lecture 22 The Object class; Polymorphism read
Building Java Programs
slides created by Ethan Apter
Object initialization: constructors
Building Java Programs
Chapter 14 A List ADT.
CSE 143 Lecture 22 The Object class; Polymorphism read
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
slides created by Ethan Apter
python.reset() Also moving to a more reasonable room (CSE 403)
CSE 143 Lecture 4 More ArrayIntList:
References and objects
Building Java Programs
CSE 142 Lecture Notes Defining New Types of Objects
Building Java Programs
Building Java Programs
Using Objects (continued)
CSE 142 Lecture Notes Inheritance, Interfaces, and Polymorphism
Go to pollev.com/cse143.
Suggested self-checks: Section 7.11 #1-11
CSE 143 Lecture 5 More ArrayIntList:
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
slides created by Marty Stepp
Special instance methods: toString
Building Java Programs
Building Java Programs
Building Java Programs
CSE 143 Lecture 23 Polymorphism; the Object class read
File output; Arrays reading: 6.4 – 6.5, 7.1
Building Java Programs
Building Java Programs
Suggested self-checks: Section 7.11 #12-29
slides created by Ethan Apter and Marty Stepp
Building Java Programs
Topic 29 classes and objects, part 3
CMSC 202 Constructors Version 9/10.
Building Java Programs
Presentation transcript:

Defining New Types of Objects, part 3 CSE 142 Lecture Notes Defining New Types of Objects, part 3 Suggested reading: 4.7, 5.6, 6.5, 7.8 These lecture notes are copyright (C) Marty Stepp 2005. May not be rehosted, copied, sold, or modified without Marty Stepp's expressed written permission. All rights reserved.

Default initialization If you do not initialize an object's data field in its constructor, or if there is no constructor, the data field is given a default 'empty' value. Recall the initial version of the Point class: public class Point { int x; int y; } Example (using the above class): Point p1 = new Point(); System.out.println(p1.x); // 0 This is similar to the way that array elements are automatically initialized to 'empty' or zero values.

Null object data fields What about data fields that are of object types? Recall the initial version of the Point class: public class Circle { Point center; double radius; } Example (using the above class): Circle circ = new Circle(); System.out.println(circ.center); // null Java prints the bizarre output of 'null' to indicate that the circle's center data field does not refer to any Point object (because none was constructed and assigned to it).

The keyword null null: The absence of an object. The Java keyword null may be stored into a reference variable (a variable intended to refer to an object) to indicate that that variable does not refer to any object. Example: Point p1 = new Point(-4, 7); Point p2 = null; +---------------------+ +---+ | +----+ +----+ | p1 | --+--> | x | -4 | y | 7 | | +---+ p2 | / |

NullPointerException If you try to call a method on a variable storing null, your program will crash with a NullPointerException. Example: Point p = null; System.out.println("p is " + p); System.out.println(p.getX()); // crash Output: p is null Exception in thread "main" java.lang.NullPointerException at UsePoint.main(UsePoint.java:9) To avoid such exceptions, you can test for null using == and != . if (p == null) { System.out.println("There is no object here!"); } else { System.out.println(p.getX()); }

Arrays of objects Recall: when you construct an array of primitive values like ints, the elements' values are all initialized to 0. Null is the equivalent of 0 (an empty value) for objects. When you construct an array of objects (such as Strings), each element initially stores null. Your program will crash if you try to call methods on the null array elements. Example: String[] words = new String[5]; +---+ words | --+--> index 1 2 3 4 value null length 5

Avoiding 'null pointers' Null array elements often lead to program crashes: Example: String[] words = new String[5]; System.out.println("the word is: " + words[0]); words[0] = words[0].toUpperCase(); // crash Output: the word is: null Exception in thread "main" java.lang.NullPointerException at StringExample.main(StringExample.java:8) The array elements should be initialized somehow: for (int i = 0; i < words.length; i++) { words[i] = "this is string #" + (i + 1); } words[0] = words[0].toUpperCase(); // okay now

Pre/postconditions precondition: Something that you expect / assume to be true when your method is called. postcondition: Something you promise to be true when your method exits. Pre/postconditions are often documented as comments on method headers. Example: // Sets this Point's location to be the given (x, y). // Precondition: x, y >= 0 // Postcondition: this.x = x, this.y = y public void setLocation(int x, int y) { this.x = x; this.y = y; }

Violated preconditions What if your precondition is not met? Sometimes the author of the other code (the 'client' of your object) passes an invalid value to your method. Example: // in another class (not in Point.java) Point pt = new Point(5, 17); Scanner console = new Scanner(System.in); System.out.print("Type the coordinates: "); int x = console.nextInt(); // what if the user types int y = console.nextInt(); // a negative number? pt.setLocation(x, y); How can we scold the client for misusing our class in this way?

Throwing exceptions exception: A Java object that represents an error. When an important precondition of your method has been violated, you can choose to intentionally generate an exception in the program. Example: // Sets this Point's location to be the given (x, y). // Precondition: x, y >= 0 // Postcondition: this.x = x, this.y = y public void setLocation(int x, int y) { if (x < 0 || y < 0) { throw new IllegalArgumentException(); } this.x = x; this.y = y;

Exception syntax Throwing an exception, general syntax: throw new <exception type> (); or, throw new <exception type> ("<message>"); The <message> will be shown on the console when the program crashes. It is common to throw exceptions when a method or constructor has been called with invalid parameters and there is no graceful way to handle the problem. Example: public Circle(Point center, double radius) { if (center == null || radius < 0.0) { throw new IllegalArgumentException(); }