Transcript Lecture 3

Internet Software
Development
Java with added Swing
Lecture 3
// file: Welcome.java
public class Welcome {
public static void main(String[ ] args) {
System.out.println(“Welcome to JavaWorld!”);
}
}
 javac
Welcome.java
 java Welcome
Welcome to JavaWorld!
// file: Welcome.java
// Always introduce the class
public class Welcome {
// Class name must match file name for public classes
public static void main(String[ ] args) {
// So, “args” is an array of Strings.
// “void” means no value is returned
System.out.println(“Welcome to JavaWorld!”);
// System is a class that interfaces to system facilities
// “out” is the standard output stream of System
String Handling is Simple
System.out.println(“Welcome to JavaWorld\n” +
“\tObject-Oriented Programming\n” +
“\tPLUS\n” +
“\tConnectivity!”);

Java Welcome
Welcome to JavaWorld
Object-Oriented Programming
PLUS
Connectivity!
A Java Class
// Rectangle.java
// A straightforward implementation of the Java Rectangle class
Rectangle
public class Rectangle
{
public double length;
public double
height;
length
height
public Rectangle(double length, double height) {
this.length = length;
area = height;
this.height
}
circumference
public double area( ) { return length*height; }
public double circumference( ) { return 2*(length+height); }
}
public class MakeRectangle {
public static void main(String[ ] args) {
double length = Double.parseDouble(args[0]);
// parse first argument to double
double height = Double.parseDouble(args[1]);
// parse second argument to double
Rectangle rectangle = new Rectangle(length,
height);
double area = rectangle.area(); // calculate area
System.out.println(area); // display it to the user
}
}
 javac
Rectangle.java
 javac MakeRectangle.java
 java MakeRectangle 3.0 2.0
6.0

Java Foundation Classes
 Provides
Graphics and GUI capability for
Java applications
 See: Java Foundation Classes In a
Nutshell, by David Flanagan

Provides a reference, but its not good as an
introduction
Data Entry with Swing
import javax.swing.*;
public class MakeRectangle {
public static void main(String[] args) {
String input =
JOptionPane.showInputDialog("Enter length");
A Class in Swing that can be
used to display simple dialog
boxes to the user
A method in JOptionPane
that displays a simple
data entry dialog
Modified MakeRectangle
import javax.swing.*;
public class MakeRectangle {
public static void main(String[ ] args) {
String input = JOptionPane.showInputDialog("Enter length");
double length = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter height");
double height = Double.parseDouble(input);
Rectangle rectangle = new Rectangle(length, height);
double area = rectangle.area();
System.out.println(area);
}
// This will turn out to be an unsatisfactory termination
}
GUI application termination
import javax.swing.*;
public class MakeRectangle {
public static void main(String[ ] args) {
String input = JOptionPane.showInputDialog("Enter length");
double length = Double.parseDouble(input);
...
Rectangle rectangle = new Rectangle(length, height);
double area = rectangle.area();
System.out.println(area);
System.exit(0); // terminates the program - this is required
// for any program using a GUI
}
}
Prettier Output
 Swing
to the rescue again!
 Need to display a different kind of dialog:


present a message
no field for the user to edit
 Use
JOptionPane again:
JOptionPane.showMessageDialog(null, "The area is " + area);
Parent component if
relevant (“null” here)
The message to be displayed
Note use of “+” to
concatenate strings
// MakeRectangle.java
// Application with “complete” GUI
import javax.swing.*;
public class MakeRectangle {
public static void main(String[ ] args) {
String input = JOptionPane.showInputDialog("Enter length");
double length = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter height");
double height = Double.parseDouble(input);
Rectangle rectangle = new Rectangle(length, height);
double area = rectangle.area();
JOptionPane.showMessageDialog(null, "The area is " + area);
System.exit(0);
}
}
Improving the dialog
 This
was the simplest method for creating
a message dialog
 But we can:


alter the icon
specify the title bar string
The message
JOptionPane.showMessageDialog(
null, "The area is " + area,
"Result", JOptionPane.PLAIN_MESSAGE);
Title bar string
Icon type
Message dialog types
 JOptionPane.ERROR_MESSAGE

Indicates an error to the application user
 JOptionPane.INFORMATION_MESSAGE

Displays an informational message - the user
simply dismisses the dialog when ready
 JOptionPane.WARNING_MESSAGE

Warns the user of a potential problem
Message dialog types
 JOptionPane.QUESTION_MESSAGE

Poses a question - normally requires a
Yes/No response from the user
 JOptionPane.PLAIN_MESSAGE

Dialog that simply contains a message with
no icon
public class Circle {
protected double radius;
protected void checkRadius(double radius) {
if (radius < 0.0) {
JOptionPane.showMessageDialog(null,
"Radius must not be negative",
"Illegal argument", JOptionPane.ERROR_MESSAGE);
System.exit(-1);
}
}
public Circle(double radius) {
checkRadius(radius);
this.radius = radius;
}
public double area() {return PI*radius*radius; }
public double circumference() {return 2*PI*radius; }
}
More robust Classes
 Declare

Protected fields can be accessed by
subclasses or members of the same package
 Declare

fields as Private or Protected
public “get” and “set” methods
with appropriate checks on the “set” methods
 E.g.
public void setRadius(double radius) {
checkRadius(radius);
this.radius = radius; }
public class Circle {
public static final double PI = 3.14159; // a constant
protected double radius;
protected void checkRadius(double radius) {
if (radius < 0.0) {
throw new IllegalArgumentException("radius must not be negative"); }
}
public Circle(double radius) {
checkRadius(radius);
this.radius = radius;
}
public double getRadius() {return radius;}
public void setRadius(double radius) {
checkRadius(radius);
this.radius = radius;
}
public double area() {return PI*radius*radius; }
public double circumference() {return 2*PI*radius; }
}