Contents - Cultural View
Contents - Cultural View Contents - Cultural View
Java (programming language) 6 A more comprehensive example // OddEven.java import javax.swing.JOptionPane; public class OddEven { // "input" is the number that the user gives to the computer private int input; // a whole number("int" means integer) /* * This is the constructor method. It gets called when an object of the OddEven type * is being created. */ public OddEven() { /* * Code not shown for simplicity. In most Java programs constructors can initialize objects * with default values, or create other objects that this object might use to perform its * functions. In some Java programs, the constructor may simply be an empty function if nothing * needs to be initialized prior to the functioning of the object. In this program's case, an * empty constructor would suffice, even if it is empty. A constructor must exist, however if the one. } * user doesn't put one in then the compiler will create an empty */ // This is the main method. It gets called when this class is run through a Java interpreter. public static void main(String[] args) { /* * This line of code creates a new instance of this class called "number" (also known as an * Object) and initializes it by calling the constructor. The next line of code calls you for a number } * the "showDialog()" method, which brings up a prompt to ask */ OddEven number = new OddEven(); number.showDialog(); public void showDialog() { /*
Java (programming language) 7 dialog box * "try" makes sure nothing goes wrong. If something does, * the interpreter skips to "catch" to see what it should do. */ try { /* converted into instead of a word. calculate() that will * The code below brings up a JOptionPane, which is a * The String returned by the "showInputDialog()" method is * an integer, making the program treat it as a number * After that, this method calls a second method, * display either "Even" or "Odd." */ input = Integer.parseInt(JOptionPane.showInputDialog("Please Enter A Number")); calculate(); } catch (NumberFormatException e) { /* * Getting in the catch block means that there was a problem with the format of of a number. * the number. Probably some letters were typed in instead */ numerical value."); } /* } System.err.println("ERROR: Invalid input. Please type in a * 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) */ private void calculate() { } if (input % 2 == 0) { System.out.println("Even"); } 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
- Page 1 and 2: Contents Articles Java (programming
- Page 3 and 4: Michael Kölling 212 List of Java k
- Page 5 and 6: Java (programming language) 1 Java
- Page 7 and 8: Java (programming language) 3 Stand
- Page 9: Java (programming language) 5 Examp
- Page 13 and 14: Java (programming language) 9 drawS
- Page 15 and 16: Java (programming language) 11 to s
- Page 17 and 18: Java (programming language) 13 See
- Page 19 and 20: Java (programming language) 15 [41]
- Page 21 and 22: AgentSheets 17 year, per school. Of
- Page 23 and 24: AgentSheets 19 References [1] Cyber
- Page 25 and 26: Lightweight Java 21 Lightweight Jav
- Page 27 and 28: .properties 23 See also • java.ut
- Page 29 and 30: Apache Harmony 25 Sun answered on a
- Page 31 and 32: Apache Harmony 27 Component Descrip
- Page 33 and 34: Apache Harmony 29 [10] Gilbert, Dav
- Page 35 and 36: Apache Shiro 31 Apache Shiro Develo
- Page 37 and 38: Java applet 33 there are frequently
- Page 39 and 40: Java applet 35 } } g.drawString("He
- Page 41 and 42: Java applet 37 The 2002 Sun - Micro
- Page 43 and 44: Java applet 39 [18] W3.org (http:/
- Page 45 and 46: Associació d'Usuaris de Java de Ca
- Page 47 and 48: Associació d'Usuaris de Java de Ca
- Page 49 and 50: Associació d'Usuaris de Java de Ca
- Page 51 and 52: Associació d'Usuaris de Java de Ca
- Page 53 and 54: Bean Scripting Framework 49 Bean Sc
- Page 55 and 56: CJAN 51 Ostensibly, CJAN.org failed
- Page 57 and 58: Chainsaw (log file viewer) 53 Chain
- Page 59 and 60: clone (Java method) 55 Alternatives
Java (programming language) 7<br />
dialog box<br />
* "try" makes sure nothing goes wrong. If something does,<br />
* the interpreter skips to "catch" to see what it should do.<br />
*/<br />
try {<br />
/*<br />
converted into<br />
instead of a word.<br />
calculate() that will<br />
* The code below brings up a JOptionPane, which is a<br />
* The String returned by the "showInputDialog()" method is<br />
* an integer, making the program treat it as a number<br />
* After that, this method calls a second method,<br />
* display either "Even" or "Odd."<br />
*/<br />
input =<br />
Integer.parseInt(JOptionPane.showInputDialog("Please Enter A Number"));<br />
calculate();<br />
} catch (NumberFormatException e) {<br />
/*<br />
* Getting in the catch block means that there was a<br />
problem with the format of<br />
of a number.<br />
* the number. Probably some letters were typed in instead<br />
*/<br />
numerical value.");<br />
}<br />
/*<br />
}<br />
System.err.println("ERROR: Invalid input. Please type in a<br />
* When this gets called, it sends a message to the interpreter.<br />
* The interpreter usually shows it on the command prompt (For<br />
Windows users)<br />
}<br />
* or the terminal (For Linux users).(Assuming it's open)<br />
*/<br />
private void calculate() {<br />
}<br />
if (input % 2 == 0) {<br />
System.out.println("Even");<br />
} else {<br />
}<br />
System.out.println("Odd");<br />
• The import statement imports the JOptionPane class from the javax.swing package.<br />
• The OddEven class declares a single private field of type int named input. Every instance of the OddEven class<br />
has its own copy of the input field. The private declaration means that no other class can access (read or write) the