Java-Exceptions2-Spr - Error Handling with Exceptions Examples COP 3330 © Dr David A Workman School of Computer Science March 4 2004 2 Point Class

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Error Handling with Exceptions Examples COP 3330 © Dr. David A. Workman School of Computer Science March 4, 2004 2 Point Class import java.io.*; import java.util.*; public class Point3 { private double x; //instance data (horizontal coordinate) private double y; //instance data (vertical coordinate) public class PointException extends Exception { public PointException(String errmsg){ super(errmsg);}} public Point3() { x = y = 0.0; } //default constructor (coordinates at the origin) public Point3( double X, double Y ) { x = X; y = Y;} public Point3( StringTokenizer st ) throws PointException { //parse image: "( x , y )“ this(); // call default constructor if( st.countTokens() == 5 ){ try{ if( !st.nextToken().equals("(") ) throw new PointException(“Invalid Point: missing ‘(‘ “) ; x = Double.parseDouble( st.nextToken().trim() ); if( !st.nextToken().equals(",") ) throw new PointException(“Invalid Point: missing ‘,‘ “) ; y = Double.parseDouble( st.nextToken().trim() ); if( !st.nextToken().equals(")") ) throw new PointException(“Invalid Point: missing ‘)‘ “) ; }catch( NumberFormatException e ){ throw new PointException(“Invalid Point (coordinate value missing or invalid!)” + e.getMessage()); }//catch return; //exit here! Object is valid! }//if throw new PointException( “Incomplete Point specification! Missing tokens.”); }//Point3 public double xCoord() {return x; } public double yCoord() {return y; } public boolean isDefined() {return (x >= 0.0 || y >= 0.0); } public void put( PrintStream ps ) { // outputs the Point description to an output stream (ps) ps.print( " ( " + x + " , " + y + " ) " ); }//put }//end Point3 3 Summary of Design Principles 1. Each class should define a set of methods for parsing an object image that is...
View Full Document

This note was uploaded on 07/04/2011 for the course COP 3330 taught by Professor Staff during the Summer '08 term at University of Central Florida.

Page1 / 14

Java-Exceptions2-Spr - Error Handling with Exceptions Examples COP 3330 © Dr David A Workman School of Computer Science March 4 2004 2 Point Class

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online