CS 235 - Week 5 Lab - 2015-09-23
* you can specify whether the contents of a textfield
should be left, right, or center justified using
the method setHorizontalAlignment --
playing with this a bit in DrJava Interactions window:
Welcome to DrJava. Working directory is /Users/smtuttle/humboldt/f15cs235/235labs/235lab05
> import javax.swing.*;
> import java.awt.*;
> import javax.swing.event.*;
> JFrame aFrame = new JFrame();
> aFrame.setSize(500, 500);
> JPanel aPanel = new JPanel();
> aFrame.add(aPanel);
> JTextField aField = new JTextField(20);
> aPanel.add(aField);
> aFrame.setVisible(true);
>
> // turns out that JTextField has a way to specify its
> // horizontal alignment -- by default, they are left-justified
>
> aField.setHorizontalAlignment(JTextField.RIGHT);
>
[and now you should see that any text within aField is
indeed right-justified]
> // two useful methods for formatting strings:
>
> // one is a method printf -- which System.out does have --
> // allowing you to specify a format string with formatting directives
> // and values to format in that string, and it outputs the result
> // to standard output (for System.out)
> //
> // and the other is a static String method named format, which uses
> // the same approach except it *returns* the resulting formatted
> // String
>
> // to format a double value with a specified number of fractional places,
> // you'd use format directive %x.yf
> // where x is the total number of characters to print, and
> // y is how many of them are fractional
>
> System.out.printf("I want to see this to 2 fractional places: %5.2f!!!", 0.3);
I want to see this to 2 fractional places: 0.30!!!> System.out.printf("<%5.2f>\n", 0.3);
< 0.30>
// right -- printf DOESN'T output a newline at the end -- can include
// \n in the format string if you'd like, though!
// %<num>s formats a String value right-justified in a field of size <num>
// and
// %<num>d formats an int ("decimal") value right-justified in a field of size <num>
> System.out.printf("<%8s>", "moo");
< moo>>
> String.format("<%5d", 13)
"< 13"
// CAREFUL -- it can be easy to miss in Interactions window,
// but System.out.printf does not return anything, it has the
// side effect of printing to standard output --
// and String.format *returns* a String --
// (DrJava Interactions window lets you run a statement OR
// see the value of an expression --
// System.out.printf("hi, %s!\n", name); // is a statement
// String.format("hi, %s!\n", name) // is an expression
// )
> String lookity = String.format("<%5d>,<%5.1f>,<%5s>", 999, 5.2, "moo");
> lookity
"< 999>,< 5.2>,< moo>"
> String.format("%5.1f", 1.99999)
" 2.0"
// Java REQUIRES value formatted using %f to *be* a floating-point/double type!
> String.format("%5.1f", 234567)
java.util.IllegalFormatConversionException: f != java.lang.Integer
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2806)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2753)
at java.util.Formatter.format(Formatter.java:2520)
at java.util.Formatter.format(Formatter.java:2455)
at java.lang.String.format(String.java:2940)
> String.format("%5.1f", 234567.0)
"234567.0"
* catching exceptions using try-catch
try
{
// statements that might throw an exception
// statements that depend on the preceding statements
}
catch(ExceptionType1 exc)
{
// what happens if ExceptionType1 is thrown in try
}
catch(ExceptionType2 exc)
{
// what happens if ExceptionType2 is thrown in try
}
...
finally /* this is optional */
{
// to be done ALWAYS whether exception occurred or not
}
SEMANTICS of this:
* do the statements in the try block. If no exception
occurs, proceed to the finally block (if it is there),
then whatever follows that
* IF an exception is thrown, however,
IMMEDIATELY jump to the FIRST catch block
matching the type of the exception thrown;
do that catch block's actions, then
proceed to the finally block (if it is there),
then whatever follows
* (note that the class Exception should be either
a superclass or an ancestor of all exception
types -- so
catch(Exception exc)
{
...
}
...should match any exception thrown)
* see TryMe3.java for a small example using
try-catch for exception-handling;
* some example calls of TryMe3 from DrJava interactions
window (slightly edited for readability):
Welcome to DrJava. Working directory is /Users/smtuttle/humboldt/f15cs235/235labs/235lab05
> java TryMe3
MUST have 2 integer arguments!
> java TryMe3 67
MUST have 2 integer arguments!
> java TryMe3 67 68 69
MUST have 2 integer arguments!
> java TryMe3 3 7
3 + 7 = 10
Whew!
Good day and Goodbye!
> java TryMe3 3 moo
Both arguments must be integers!
Good day and Goodbye!
> java TryMe3 moo 3
Both arguments must be integers!
Good day and Goodbye!
> java TryMe3 moo 3.0
Both arguments must be integers!
Good day and Goodbye!
> java TryMe3 3 4.0
Both arguments must be integers!
Good day and Goodbye!
> java TryMe3 45 100
45 + 100 = 145
Whew!
Good day and Goodbye!
> java TryMe3 34567 12121212
34567 + 12121212 = 12155779
Whew!
Good day and Goodbye!
> java TryMe3 34567 oink
Both arguments must be integers!
Good day and Goodbye!