google search

Custom Search

Friday, August 8, 2008

A more comprehensive example

// OddEven.java
import javax.swing.JOptionPane;

public class OddEven {
public static void main(String[] args) {
// This is the main method. It gets called when this class is run through a Java interpreter.
OddEven number = new OddEven();
/* This line of code creates a new instance of this class called "number" and
* initializes it, and the next line of code calls the "showDialog()" method,
* which brings up a prompt to ask you for a number
*/
number.showDialog();
}
private int input; // A whole number("int" means integer)
// "input" is the number that the user gives to the computer

public OddEven() {
/* This is the constructor method. It gets called when an object of the OddEven type
* is created.
*/
}

public void showDialog() {
try
/* This makes sure nothing goes wrong. If something does,
* the interpreter skips to "catch" to see what it should do.
*/
{
input = Integer.parseInt(JOptionPane.showInputDialog("Please Enter A Number"));
calculate();
/*
* The code above brings up a JOptionPane, which is a dialog box
* The String returned by the "showInputDialog()" method is converted into
* an integer, making the program treat it as a number instead of a word.
* After that, this method calls a second method, calculate() that will
* display either "Even" or "Odd."
*/
}
catch (NumberFormatException e)
/* This means that there was a problem with the format of the number
* (Like if someone were to type in 'Hello world' instead of a number).
*/
{
System.err.println("ERROR: Invalid input. Please type in a numerical value.");
}
}

private void calculate() {
if (input % 2 == 0)
System.out.println("Even");
/* When this gets called, it sends a message to the interpreter.
* The interpreter usually shows it on the command prompt (For Windows users)
* or the terminal (For Linux users).(Assuming it's open)
*/
else
System.out.println("Odd");
}
}

* The import statement imports the JOptionPane class from the javax.swing package.
* The OddEven class declares a single private field of type int named input. Every instance of the OddEven class has its own copy of the input field. The private declaration means that no other class can access (read or write) the input field.
* OddEven() is a public constructor. Constructors have the same name as the enclosing class they are declared in, and unlike a method, have no return type. A constructor is used to initialize an object that is a newly created instance of the class. The dialog returns a String that is converted to an int by the Integer.parseInt(String) method.
* The calculate() method is declared without the static keyword. This means that the method is invoked using a specific instance of the OddEven class. (The reference used to invoke the method is passed as an undeclared parameter of type OddEven named this.) The method tests the expression input % 2 == 0 using the if keyword to see if the remainder of dividing the input field belonging to the instance of the class by two is zero. If this expression is true, then it prints Even; if this expression is false it prints Odd. (The input field can be equivalently accessed as this.input, which explicitly uses the undeclared this parameter.)
* OddEven number = new OddEven(); declares a local object reference variable in the main method named number. This variable can hold a reference to an object of type OddEven. The declaration initializes number by first creating an instance of the OddEven class, using the new keyword and the OddEven() constructor, and then assigning this instance to the variable.
* The statement number.showDialog(); calls the calculate method. The instance of OddEven object referenced by the number local variable is used to invoke the method and passed as the undeclared this parameter to the calculate method.
* For simplicity, error handling has been ignored in this example. Entering a value that is not a number will cause the program to crash. This can be avoided by catching and handling the NumberFormatException thrown by Integer.parseInt(String).

0 comments:

 

blogger templates | Make Money Online