14.07.2013 Views

Contents - Cultural View

Contents - Cultural View

Contents - Cultural View

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Java (programming language) 6<br />

A more comprehensive example<br />

// OddEven.java<br />

import javax.swing.JOptionPane;<br />

public class OddEven {<br />

// "input" is the number that the user gives to the computer<br />

private int input; // a whole number("int" means integer)<br />

/*<br />

* This is the constructor method. It gets called when an object of<br />

the OddEven type<br />

* is being created.<br />

*/<br />

public OddEven() {<br />

/*<br />

* Code not shown for simplicity. In most Java programs<br />

constructors can initialize objects<br />

* with default values, or create other objects that this object<br />

might use to perform its<br />

* functions. In some Java programs, the constructor may simply be<br />

an empty function if nothing<br />

* needs to be initialized prior to the functioning of the object.<br />

In this program's case, an<br />

* empty constructor would suffice, even if it is empty. A<br />

constructor must exist, however if the<br />

one.<br />

}<br />

* user doesn't put one in then the compiler will create an empty<br />

*/<br />

// This is the main method. It gets called when this class is run<br />

through a Java interpreter.<br />

public static void main(String[] args) {<br />

/*<br />

* This line of code creates a new instance of this class<br />

called "number" (also known as an<br />

* Object) and initializes it by calling the constructor. The<br />

next line of code calls<br />

you for a number<br />

}<br />

* the "showDialog()" method, which brings up a prompt to ask<br />

*/<br />

OddEven number = new OddEven();<br />

number.showDialog();<br />

public void showDialog() {<br />

/*

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!