Jump to content
xisto Community
Sign in to follow this  
dyknight

Java Lab 01 My first installment of Java tutorials

Recommended Posts

I have decided to come up with a series of Java tutorials teaching simple Java. This is my first chapter.

 

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:

 

/*   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"):

 

javax.swing.JFrame window;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):

 

window = new javax.swing.JFrame ();position = new java.awt.Point(200,500);

Then we give the characteristics:

 

window.setSize(300,300);window.setTitle("My First Java Program in Lab");window.setLocation(position);

Finally, we make it visible:

 

window.setVisible(true);

The final code looks like this:

 

/*   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.

Share this post


Link to post
Share on other sites

First of all, I see no JOptionPane. JFrames aren't the same thing. Second, why don't you import anything? It makes it less messy and you don't have to type the whole package tree every time you want to do something. You didn't setDefaultCloseOperation either, which can cause some problems.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.