Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CIT 590 Missing pieces. Exception handling TryExample2.java in the dropbox folder Shows you how try, catch and finally work together."— Presentation transcript:

1 CIT 590 Missing pieces

2 Exception handling TryExample2.java in the dropbox folder Shows you how try, catch and finally work together

3 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

4 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

5 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

6 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.

7 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)

8 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;

9 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 3 rd alternative is an anonymous class. ListenerExampleWithAnonymousClasses.java (in the dropbox GUI folder).


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

Similar presentations


Ads by Google