Object-Oriented Programming with Java Exceptions, Strings and Things Lecture 4.

30
Object-Oriented Object-Oriented Programming with Programming with Java Java Exceptions, Strings and Exceptions, Strings and Things Things Lecture 4 Lecture 4
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    225
  • download

    0

Transcript of Object-Oriented Programming with Java Exceptions, Strings and Things Lecture 4.

Object-Oriented Object-Oriented Programming with JavaProgramming with Java

Exceptions, Strings and ThingsExceptions, Strings and Things

Lecture 4Lecture 4

More robust ClassesMore robust Classes

Declare fields as Private or ProtectedDeclare fields as Private or Protected Protected fields can be accessed by Protected fields can be accessed by

subclasses or members of the same packagesubclasses or members of the same package Declare public “get” and “set” methodsDeclare public “get” and “set” methods

with appropriate checks on the “set” methodswith appropriate checks on the “set” methods E.g.E.g.

public void setRadius(double radius) {public void setRadius(double radius) {

checkRadius(radius);checkRadius(radius);

this.radius = radius; }this.radius = radius; }

public class Circle {public class Circle { public static final double PI = 3.14159; // a constantpublic static final double PI = 3.14159; // a constant protectedprotected double radius; double radius; protectedprotected void checkRadius(double radius) { void checkRadius(double radius) { if (radius < 0.0) {if (radius < 0.0) { throw new IllegalArgumentExceptionthrow new IllegalArgumentException("radius must not be negative"); }("radius must not be negative"); } }} public Circle(double radius) {public Circle(double radius) {

checkRadius(radius);checkRadius(radius); this.radius = radius;this.radius = radius; }} public double getRadius() {return radius;}public double getRadius() {return radius;} public void setRadius(double radius) {public void setRadius(double radius) {

checkRadius(radius);checkRadius(radius); this.radius = radius;this.radius = radius;

}} public double area() {return PI*radius*radius; }public double area() {return PI*radius*radius; } public double circumference() {return 2*PI*radius; }public double circumference() {return 2*PI*radius; }}}

Catching ExceptionsCatching Exceptions

Use the following construction:Use the following construction:try {try {

// code that may throw exceptions// code that may throw exceptions

}}

catch (AnException e1) { catch (AnException e1) {

// Code to handle this kind of exception// Code to handle this kind of exception

}}

finally {finally {

// Code that will always execute// Code that will always execute

}}

Trying to succeedTrying to succeed

try {try {// Normally this block of code will be// Normally this block of code will be// executed successfully.// executed successfully.// However, if an exception is thrown// However, if an exception is thrown// execution will stop and the // execution will stop and the

interpreterinterpreter// will attempt to find an appropriate// will attempt to find an appropriate// “catch” statement.// “catch” statement.

}}

Catching ExceptionsCatching Exceptions

There may be several “catch” blocks for different There may be several “catch” blocks for different kinds of exceptionskinds of exceptions

catch (AnException e1) { catch (AnException e1) { // This block will be invoked if an // This block will be invoked if an // AnException kind of exception is // AnException kind of exception is // thrown, or a sub-type of AnException.// thrown, or a sub-type of AnException.// The block can refer to the exception// The block can refer to the exception// object by its name “e1”// object by its name “e1”

}}

And FinallyAnd Finally

finally {finally {// This block contains code that will// This block contains code that will// always be run// always be run

// - even if control leaves the “try” block // - even if control leaves the “try” block // because of a // because of a return, , continue or or// // break statement. statement.

// It will , however, be skipped if there is a // It will , however, be skipped if there is a // System.exit( ) clause in the “try” block// System.exit( ) clause in the “try” block

public class MakeCircle {public class MakeCircle { public static void main(String[ ] args) {public static void main(String[ ] args) { // details of input omitted// details of input omitted try {try { Circle circle = new Circle(radius);Circle circle = new Circle(radius); double area = circle.area( );double area = circle.area( );

JOptionPane.showMessageDialog(JOptionPane.showMessageDialog( null, "The area equals " + area,null, "The area equals " + area,

"Not a lot of people know that", JOptionPane.PLAIN_MESSAGE) ;"Not a lot of people know that", JOptionPane.PLAIN_MESSAGE) ; }}

catch(IllegalArgumentException e1) {catch(IllegalArgumentException e1) { JOptionPane.showMessageDialog(null, "Radius must not be negative",JOptionPane.showMessageDialog(null, "Radius must not be negative",

"Illegal argument", JOptionPane.ERROR_MESSAGE) ;"Illegal argument", JOptionPane.ERROR_MESSAGE) ; }}

finally {finally { System.exit(0);System.exit(0); }} }}}}

Some kinds-of ExceptionSome kinds-of Exception

ExceptionExceptionClassNotFoundExceptionClassNotFoundExceptionIllegalAccessExceptionIllegalAccessExceptionRuntimeExceptionRuntimeException

ArithmeticExceptionArithmeticExceptionArrayStoreExceptionArrayStoreExceptionIllegal ArgumentExceptionIllegal ArgumentException……

A little bit of formattingA little bit of formatting

With double precision numbers you may With double precision numbers you may find you get outputs with an unrealistic find you get outputs with an unrealistic degree of precision degree of precision E.g. 7 figures after the decimal placeE.g. 7 figures after the decimal place

This looks messy and the precision is This looks messy and the precision is normally meaninglessnormally meaningless

The java.text PackageThe java.text Package

As an example, we will use the As an example, we will use the DecimalFormat class from this packageDecimalFormat class from this package

Create an instance of DecimalFormat with Create an instance of DecimalFormat with the required format patternthe required format pattern

Invoke the “format” method of this instance Invoke the “format” method of this instance to create a formatted string representationto create a formatted string representation

import javax.swing.*;import javax.swing.*;

import java.text.DecimalFormat;import java.text.DecimalFormat;

public class MakeCircle {public class MakeCircle {

public static void main(String[ ] args) {public static void main(String[ ] args) {

String input = JOptionPane.showInputDialog("Enter radius");String input = JOptionPane.showInputDialog("Enter radius");

double radius = Double.parseDouble(input);double radius = Double.parseDouble(input);

DecimalFormat twoDigits = new DecimalFormat( "0.00");DecimalFormat twoDigits = new DecimalFormat( "0.00");

try {try {

Circle circle = new Circle(radius);Circle circle = new Circle(radius);

double area = circle.area();double area = circle.area();

JOptionPane.showMessageDialog(JOptionPane.showMessageDialog(

null, "The area equals " + null, "The area equals " + twoDigits.format( area ),twoDigits.format( area ),

"Not a lot of people know that", JOptionPane.PLAIN_MESSAGE);"Not a lot of people know that", JOptionPane.PLAIN_MESSAGE);

} // Rest of code omitted} // Rest of code omitted

}}

One or more digits in front of the decimal point

Only two digits behind the decimal point

Informing ExceptionsInforming Exceptions

Remember that in the “main” method of Remember that in the “main” method of MakeCircle, we explicitly included the error MakeCircle, we explicitly included the error message:message:

catch(IllegalArgumentException e1) {catch(IllegalArgumentException e1) {

JOptionPane.showMessageDialog(null, "Radius must not be negative",JOptionPane.showMessageDialog(null, "Radius must not be negative",

"Illegal argument", JOptionPane.ERROR_MESSAGE) ;"Illegal argument", JOptionPane.ERROR_MESSAGE) ;

}}

Should not do this - there may be several Should not do this - there may be several different arguments that could throw the different arguments that could throw the same exception typesame exception type

Inside Circle.javaInside Circle.java

protected void checkRadius(double radius) {protected void checkRadius(double radius) {

if (radius < 0.0) {if (radius < 0.0) {

throw new IllegalArgumentException("radius must not throw new IllegalArgumentException("radius must not be negative");be negative");

}}

}} Inside MakeCircle.javaInside MakeCircle.java

catch(IllegalArgumentException e1) {catch(IllegalArgumentException e1) {

JOptionPane.showMessageDialog(null, JOptionPane.showMessageDialog(null, e1.toString(),e1.toString(),

"Illegal argument", JOptionPane.ERROR_MESSAGE);"Illegal argument", JOptionPane.ERROR_MESSAGE);

}}

toString methodstoString methods

Useful to define these for key classesUseful to define these for key classes Provides an easy way of summarising the Provides an easy way of summarising the

state of an instance of a classstate of an instance of a class Can by very useful for debuggingCan by very useful for debugging

public class Point {public class Point { // The notes show this class with more extensive commenting// The notes show this class with more extensive commenting protected double x, y;protected double x, y; // Coordinates of the point accessible to // Coordinates of the point accessible to

subclassessubclasses

public Point(double x, double y) { setPoint( x, y ); }public Point(double x, double y) { setPoint( x, y ); }

// method sets the location of a point// method sets the location of a point public void setLocation(double xCoordinate, double yCoordinate) {public void setLocation(double xCoordinate, double yCoordinate) {

x = xCoordinate;x = xCoordinate; y = yCoordinate;y = yCoordinate; }}

// conversion of a Point object to a string representation// conversion of a Point object to a string representation public String toString( ) {public String toString( ) {

return("[" + x + ", " + y + "]");return("[" + x + ", " + y + "]"); }}

}}

import javax.swing.*;import javax.swing.*;

public class MakePoint {public class MakePoint {

public static void main(String[ ] args) {public static void main(String[ ] args) { String input;String input; input = JOptionPane.showInputDialog("Enter x Coordinate");input = JOptionPane.showInputDialog("Enter x Coordinate"); double x = Double.parseDouble(input);double x = Double.parseDouble(input); input = JOptionPane.showInputDialog("Enter y Coordinate");input = JOptionPane.showInputDialog("Enter y Coordinate"); double y = Double.parseDouble(input);double y = Double.parseDouble(input);

try {try { Point point = new Point( x, y);Point point = new Point( x, y); JOptionPane.showMessageDialog(JOptionPane.showMessageDialog( null, "New point created: " + null, "New point created: " + point.toString( ),point.toString( ), "Pointed Message", JOptionPane.PLAIN_MESSAGE);"Pointed Message", JOptionPane.PLAIN_MESSAGE); } // rest of class definition omitted} // rest of class definition omitted

Adding properties to PointsAdding properties to PointsPoint

xy

setLocationtoString

Location

Circle

radius

areacircumference

plus Extension

public class Circle extends Point {public class Circle extends Point {

public static final double PI = 3.14159; // a constantpublic static final double PI = 3.14159; // a constant

protected double radius;protected double radius;

protected void checkRadius(double radius) { // details omitted }protected void checkRadius(double radius) { // details omitted }

public Circle(double x, double y, double radius) {public Circle(double x, double y, double radius) {

super(x, y);super(x, y);

checkRadius(radius);checkRadius(radius);

this.radius = radius;this.radius = radius;

}}

public String toString( ) {public String toString( ) { // Override Point.toString to add info.// Override Point.toString to add info.

return "This is a Circle object with the following properties\n" +return "This is a Circle object with the following properties\n" +

"Location: " + super.toString() + "\n" +"Location: " + super.toString() + "\n" +

"Radius = " + radius + "\n";"Radius = " + radius + "\n";

}}

}}

main method of MakeCirclemain method of MakeCircle

public void main(String[ ], args) {public void main(String[ ], args) {

// … details omitted// … details omitted

Circle circle = new Circle(x, y, radius);Circle circle = new Circle(x, y, radius);

JOptionPane.showMessageDialog(JOptionPane.showMessageDialog(

null, null, circle.toString( ),circle.toString( ),

"Something New", JOptionPane.PLAIN_MESSAGE);"Something New", JOptionPane.PLAIN_MESSAGE);

// and the rest ...// and the rest ...

}}

InterfacesInterfaces

Sometimes we may find two or more Sometimes we may find two or more different subclasses share some common different subclasses share some common behaviourbehaviour

In this case, they are not strictly “kinds of” In this case, they are not strictly “kinds of” some common parentsome common parent

Rather, they “behave like” some common Rather, they “behave like” some common pattern (under certain circumstances)pattern (under certain circumstances)

We say that they both implement a We say that they both implement a common interfacecommon interface

A “Cashier” InterfaceA “Cashier” Interface

Person Machine

ATMEmployee

«interface»Cashier

withdrawdeposit