Introduction to java
1 / 21

Introduction to Java - PowerPoint PPT Presentation

  • Updated On :

Introduction to Java. A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia. Introduction to Java. Java Applications and Applets Java Development kit JCreator Console Programs in Java Applet Development Graphics. Java Applications and Applets.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Introduction to Java' - vian

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Introduction to java

Introduction to Java

A lab course by

Dr. Junaid Ahmed Zubairi

SUNY Fredonia

Introduction to java1
Introduction to Java

  • Java Applications and Applets

  • Java Development kit

  • JCreator

  • Console Programs in Java

  • Applet Development

  • Graphics

Java applications and applets
Java Applications and Applets

  • In Java, you can develop applications and applets

  • Applications can be executed by themselves

  • Applets are executed in a controlled environment, usually within web browsers

  • Let us see the example of an applet embedded in a web page

Some applets
Some Applets

  • Hilo Game:


  • Hangman Game:


Applet mechanism
Applet Mechanism

  • Java applets are programs that are stored with a web page

  • When a user requests that web page, the applet embedded with it is sent from the server to the user’s computer

  • Then the applet is executed in a “sandbox” preventing it from corrupting the user’s computer

Jdk java development kit
JDK (Java Development Kit)

  • JDK or SDK packages are released by Sun Microsystems

  • A recent version Java2 SDK v 1.4.3 is available through the URL:


  • On this web page, you can also find the JCreator v 3.


  • JCreator is a nice free package available through the web

  • We are using its older version (2.5) on lab machines. However, I have made version 3.0 available on my homepage

  • JCreator is an IDE (Integrated Development Environment) for developing Java applications and applets

On your home machine
On Your Home Machine

  • Download and install Java 2 SDK v1.4.3

  • (Optional) Download and install Java Documentation

  • Download and install JCreator version 3

  • Now you are all set to start using Java

  • If Java compiler does not run, Use ConfigureOptions and check JDK profile

Console programs in java
Console Programs in Java

  • You can start by typing a simple program that will work through the console window

  • Open JCreator, Choose FileNewFile and choose “Java File”. Give it the name “”

  • Type the following program into the editor window, then save, compile and run the program

First program
First Program

  • Public class myfirst {

  • Public static void main(String[] args)

  • {

  • System.out.println(“Hi!! Nice Coffee”);

  • }

  • }

Java rules
Java Rules!!

  • Each source file can contain only one public class

  • The name of the public class must match the name of the file in which it is stored

  • Each Java application must have a method named main()

  • main() starts running first so make sure that you put the initial code in it


  • Try replacing the single statement in the first program by the following statements one by one and note the output:

  • System.out.println(3+4);

  • System.out.print(3+4);

  • System.out.println(3+4);

  • System.out.println("\"Hi!! Nice Coffee\"");

Opening a little window
Opening A Little Window

  • Now is the time to open a small colorful window in our application

  • We would like to read the name of the user and generate a greeting for the user

  • Reading from the keyboard in Java requires adding an import line on top of the program

  • “import javax.swing.JOptionPane”

  • Java is case sensitive!!

Using input window
Using Input Window

  • Please add the following line to capture and store the keyboard input into a string variable

  • String username = JOptionPane.showInputDialog("What is your name?");

  • You can display the stored name by the following line:

  • System.out.print(username);

  • Please complete the program by adding a greeting

Showing the results in a window
Showing the Results in a Window

  • In order to get rid of the black and white console completely, we learn how to show the results in a colored window

  • We use another method from the goodies bag!!

  • This method is named showMessageDialog

  • It takes four parameters

Showing results
Showing Results

  • Leave the first parameter as null

  • After comma, write the text string that you want to be displayed

  • The third parameter is the title of the window

  • The fourth one defines the type of message. You may choose EROR_MESSAGE to show errors and INFORMATION_MESSAGE to display normal results

Showing results1
Showing Results

  • Try this line

  • JOptionPane.showMessageDialog(null,"Good Morning "+username,"Greeting Window",JOptionPane.INFORMATION_MESSAGE);


  • Change the output window to error message type window

  • Swap the greeting to occur after the name

  • Add “How are you” to the greeting. Strings can be added to other strings with + sign

  • Change the title to your own

  • Give a demo

Reading numbers from the keyboard
Reading Numbers from the Keyboard

  • Numbers cannot be directly read from the keyboard because the methods used by us read everything as text

  • If the user has typed a number, we have to extract it from the string

  • Assume the user has entered a number in the string username

  • Type the following to extract and store the number into variable “age”

  • int age = Integer.parseInt(username);

Programming exercise
Programming Exercise

  • Use the comparison statements to display the appropriate message

  • For example, use the following statement to display the message that the user can drive

  • if (age>=16)

  • JOptionPane.ShowMessageDialog(null, “You can drive”,”Info”,JOptionPane.INFORMATION_MESSAGE);

Programming exercise1
Programming Exercise

  • Add a statement that displays the fact that the user’s age is less than 16 and thus the user cannot drive

  • Add another pair of statements showing the decision if the user can or cannot vote, given the voting age is 18