For this chapter, I will be showing how to write a simple program that:
1. Pops up a JOptionPane,
2. Shows a message on the pane.
Classes, Objects
Java, as some of you might already know, is a/an (almost) completely object-orientated programming language (OOP). Some of the experts out there may argue that Java is fully object-orientated, but I don't think that is absolute. Some programmers consider Ruby to be fully OOP and Java to be almost fully OOP because Java still uses primitive data types. (I don't know much on this, but if you do, please PM me. I love to learn.)
Object-orientated means that the program works with objects. An object is something that contains data and methods. A class defines the object, specifying what information the object should hold and what methods can be called. (methods are also known as functions)
An analogy:
"Humans" can be considered to be a class. Objects of this class should have a name, an age, a gender, and a bloodtype. The objects should be able to talk, sleep, eat, and see. "John" is an object. His name is John, age 18, male, bloodtype O. Like all humans, he talks, sleeps, eats, and sees.
Human -> Class
John -> Object
name, an age, a gender, and a bloodtype -> information
talk, sleep, eat, and see -> methods
The Code
To start coding, you need Java 2 Standard Edition Runtime Environment and Development Kit (J2RE and JDK, which can be downloaded from java.sun.com) and a compiler. I recommend Dr. Java, which can be downloaded from drjava.org.
In Java, everything is held within a class. Even the main method is enclosed within a class. So we start by typing this in:
CODE
/*
Lab 1:
File: Lab01.java
*/
public class Lab01 {
Lab 1:
File: Lab01.java
*/
public class Lab01 {
Now, we need to DECLARE the objects we need (e.g. we need to declare a human "john"):
CODE
javax.swing.JFrame window;
java.awt.Point position;
java.awt.Point position;
The above declares a window object and a point object, from the javax.swing package and the java.awt package.
Next, we need to CREATE the objects we need (e.g. creating John's body):
CODE
window = new javax.swing.JFrame ();
position = new java.awt.Point(200,500);
position = new java.awt.Point(200,500);
Then we give the characteristics:
CODE
window.setSize(300,300);
window.setTitle("My First Java Program in Lab");
window.setLocation(position);
window.setTitle("My First Java Program in Lab");
window.setLocation(position);
Finally, we make it visible:
CODE
window.setVisible(true);
The final code looks like this:
CODE
/*
Lab 1:
File: Lab01.java
*/
public class Lab01 {
public static void main (String [] args) {
javax.swing.JFrame window;
window = new javax.swing.JFrame ();
window.setSize(300,300);
window.setTitle("My First Java Program in Lab");
java.awt.Point position;
position = new java.awt.Point(200,500);
window.setLocation(position);
window.setVisible(true);
}
}
Lab 1:
File: Lab01.java
*/
public class Lab01 {
public static void main (String [] args) {
javax.swing.JFrame window;
window = new javax.swing.JFrame ();
window.setSize(300,300);
window.setTitle("My First Java Program in Lab");
java.awt.Point position;
position = new java.awt.Point(200,500);
window.setLocation(position);
window.setVisible(true);
}
}
Type the above into Dr Java, compile and hit F2 to run. There you go!. Do leave a message to ask for help if you need.

