9
CIT 590 Missing pieces

CIT 590 Missing pieces. Exception handling TryExample2.java in the dropbox folder Shows you how try, catch and finally work together

Embed Size (px)

Citation preview

Page 1: CIT 590 Missing pieces. Exception handling TryExample2.java in the dropbox folder Shows you how try, catch and finally work together

CIT 590Missing pieces

Page 2: CIT 590 Missing pieces. Exception handling TryExample2.java in the dropbox folder Shows you how try, catch and finally work together

Exception handling

TryExample2.java in the dropbox folder

Shows you how try, catch and finally work together

Page 3: CIT 590 Missing pieces. Exception handling TryExample2.java in the dropbox folder Shows you how try, catch and finally work together

Equals and toString

The most foolproof way of writing them in an IDE like Eclipse is

Write the word equals or toString. Hit CTRL + Space (or whatever your auto completion suggestion shortcut is)

For the exam remember these two signatures

• public String toString() {}• public boolean equals(Object obj) {}

Remember to cast the object in your equals method

Page 4: CIT 590 Missing pieces. Exception handling TryExample2.java in the dropbox folder Shows you how try, catch and finally work together

Reading from a file• The best way to do it in ‘modern’ Java is to use the

Scanner class

• See FileRead.java in the Dropbox JavaExperiments project

Page 5: CIT 590 Missing pieces. Exception handling TryExample2.java in the dropbox folder Shows you how try, catch and finally work together

Writing to a file• This can be done using a PrintWriter class.• Annoyingly Java wants you to catch a ton of exceptions• Most developers will use some class that ‘wraps’ the

exceptions

Page 6: CIT 590 Missing pieces. Exception handling TryExample2.java in the dropbox folder Shows you how try, catch and finally work together

What does static mean

Static methods

A static method means the method exists at the class level and is not specific to the instance.

The one static method that you have in a lot of your classes is main.

Used when you do not need an instance of the object. Example Math.sqrt is a method that computes the square root of a number. You do not need an instance of mathematics before you know how to compute the square root.

Page 7: CIT 590 Missing pieces. Exception handling TryExample2.java in the dropbox folder Shows you how try, catch and finally work together

Static versus non static

Consider the Rational number class

To reduce 2/4 to 1/2 a common thing to do is to compute the greatest common divisor. To compute the gcd you do not need an instance of a fraction.

However, if you want to add a rational number to this number you obviously need an instance.

public static int gcd (int a, int b)

public Rational add(Rational otherRational)

Page 8: CIT 590 Missing pieces. Exception handling TryExample2.java in the dropbox folder Shows you how try, catch and finally work together

Static instance variables• Static instance variables are used if you want some

information to be shared by every instance of a class• A common use case is constants

• For constants you also get to see the keyword final. Final meaning you do not get to override this value in any manner.

• public static final int MAXSCORE = 100;

Page 9: CIT 590 Missing pieces. Exception handling TryExample2.java in the dropbox folder Shows you how try, catch and finally work together

Anonymous classes

So far we have seen two ways of ‘listening’ to a button

The main class (the one that has a Jframe) implements ActionListener

An inner class (class declaration within the same file) implements ActionListener

A 3rd alternative is an anonymous class.

ListenerExampleWithAnonymousClasses.java

(in the dropbox GUI folder).