Download presentation
Presentation is loading. Please wait.
1
CSE 11 February 6, 2003
2
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any other purpose without the written permission of Walter Savitch. wsavitch@ucsd.edu
3
Midterm Statisatics Mean: 73.5/95 = 78% Median: 79/95 = 84% Remember No Scaling
4
The Class Object In Java, every class is a descendent of the predefined class Object
5
public class Species { private String name; private int population; private double growthRate; … MEANS
6
MEANS: public class Species extends Object { private String name; private int population; private double growthRate; …
7
Methods in the Class Object toString equals clone
8
toString public class Species { ….. public String toString() { return ( name + “\nPopulation = “ + population +”\nGrowth rate = “ + growthRate); }
9
If you add toString Species s = new Species(); …… System.out.println(s.toString()); //Not need writeOutput
10
equals Method public boolean equals(Object other)
11
public boolean equals(Object otherObject) { if (otherObject == null) return false; else if (!(otherObject instanceof Student)) return false; else { Student otherStudent = (Student)otherObject; return (this.sameName(otherStudent) && (this.studentNumber == otherStudent.studentNumber)); } }
12
Cloning (Appendix 9) Java demands a couple nuisance details. Must return a value of type Object. Class must implement the Cloneable interface, for example public class Species implements Cloneable Must account for CloneNotSupportedException.
13
Must return a value of type Object public class Species //will need something here { public Object clone() { … }
14
Class must implement the Cloneable interface For example public class Species implements Cloneable This has the syntax of implementing an interface but is not really being used like an interface (whatever that is). Just a way of saying “Yes, I really mean to do cloning.”
15
Class must implement the Cloneable interface public class Species implements Cloneable { public Object clone() { … }
16
Must account for CloneNotSupportedException Just a magic formula until we cover exceptions in Chapter 8.
17
CloneNotSupportedException public Object clone() { //This gives you the a simple bit map copy try { return super.clone(); } catch(CloneNotSupportedException e) {//This should never happen return null; //to keep compiler happy }
18
public class Species implements Cloneable {//Should work if all instance variables are //of primitive type or of type String public Object clone() { //This gives you the a simple bit map copy try { return super.clone(); } catch(CloneNotSupportedException e) {//This should never happen return null; //to keep compiler happy }
19
If Class has Class Instance Variables (other than String) public class Neighbor implements Cloneable { private String name; private int numberOfChildren; private PetRecord pet; //A real class public Object clone() { …..
20
public Object clone() { try { Neighbor copy = (Neighbor)super.clone(); copy.pet = (PetRecord)pet.clone(); return copy; } catch(CloneNotSupportedException e) { return null; } }
21
Warning Java Compiler may not enforce all the cloning rules.
22
Alternative to Cloning Copy Constructor = Constructor with one parameter of the class type that makes a “deep copy.” Really is cloning, but you avoid the baggage of the Java cloning stuff. Down side, may have trouble interacting with software that depends on your having a clone method
23
public class Species { private String name; private int population; private double growthRate; public Species(Species original) { name = new String(original.name); population = original.population; growthRate = original.growthRate; } |
24
Exception Handling Way to separate handling of “exceptional cases” from the “ordinary” cases. First example of event driven programming. Throwing an exception is a trigger event Handling the exception is the action produced in reaction to this trigger event.
25
public class ExceptionDemo { public static void main(String[] a) { int donutCount, milkCount; double donutsPerGlass;
26
try { System.out.println(“N of donuts:"); donutCount = SavitchIn.readLineInt( ); System.out.println("number of milk:"); milkCount = SavitchIn.readLineInt( ); if (milkCount < 1) throw new Exception("No Milk!"); donutsPerGlass = donutCount/(double)milkCount; System.out.println("You have " + donutsPerGlass + " donuts for each glass of milk."); }
27
try { … if (milkCount < 1) throw new Exception("No Milk!"); … } catch(Exception e) { System.out.println(e.getMessage( )); System.out.println("Go buy some milk."); } System.out.println("End of program.");
28
try { … if (Something) throw new Exception(“Bla Bla"); … } catch(Exception e) { Handle Exception Action }
29
How Commonly Used: try { … someMethod(…);//Might throw exception … } catch(Exception e) { Handle Exception Action }
30
How Commonly Used: public void someMethod(…) throws Exception { if (something) handle things else//handling depends on context throw new Exception(“Bla Bla”); }
31
How Commonly Used: public int readInt()throws Exception { String s = SavitchIn.readLine(); if (s is well formed) return Integer.parseInt(in); else//handling depends on context throw new Exception(“Syntax error”); }
32
Throws Clause public int readInt()throws Exception Warns user of method that method might throw an exception Enforced by compiler More details later
33
Exception Classes throw new Exception(…); new Exception(…) is an invocation of a constructor for the class Exception Equivalent to Exception e = new Exception(…); throw e;
34
throw Operator throw new Exception(…); Equivalent to Exception e = new Exception(…); throw e; throw e is a trigger event, a signal to wake up the catch-exception portion of your code.
35
More Exception Classes CloneNotSupportedException NumberFormatException NullPointerException You can define new exception classes
36
Defining Exception Classes Must be a derived class of an existing exception class, typically a derived class of Exception. Typically all it has and all it needs for methods are constructors and inherited methods.
37
public class DivideByZeroException extends Exception { public DivideByZeroException( ) { super("Dividing by Zero!"); } public DivideByZeroException(String message) { super(message); }
38
getMessage() Method Inherited Also inherited a private instance variable of type String. getMessage() is an accessor method that returns the string in this private instance variable.
39
try { … if (milkCount < 1) throw new Exception("No Milk!"); … } catch(Exception e) { System.out.println(e.getMessage( )); System.out.println("Go buy some milk."); } System.out.println("End of program.");
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.