Sabtu, 07 Juli 2007

Simple example for object-oriented programming

An example that better demonstrates object-oriented programming:
// OddEven.java
 import javax.swing.JOptionPane;
 public class OddEven {
 private int input;
 public OddEven() { 
 input = Integer.parseInt(JOptionPane.showInputDialog ...
 ("Please Enter A Number"));
 }

 public void calculate() {
 if (input % 2 == 0)
 {JOptionPane.showInputDialog("Even"); }
  else      
 {JOptionPane.showInputDialog("Odd"); }
 }
 public static void main(String[] args) {
 OddEven number = new OddEven();
 number.calculate();
 }
 }  
  • 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. In this case, the constructor initializes the input field to the value entered into a JOptionPane input dialog. The dialog returns a String which 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 instantiating an instance of the OddEven class using the new keyword and then calling the OddEven() constructor to initialize the newly created object.
  • The statement number.calculate(); 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).
  • Result



Tidak ada komentar:

This blog just for java programing language....So you can learn more about java, java, java, and java....

Google