introduction to java a lab course by dr. junaid ahmed zubairi suny fredonia

21
Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

Post on 21-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

Introduction to Java

A lab course by

Dr. Junaid Ahmed Zubairi

SUNY Fredonia

Page 2: 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

Page 3: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

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

Page 4: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

Some Applets

Hilo Game: http://mainline.brynmawr.edu/Courses/cs11

0/fall2003/Applets/HiLo/Hi.html Hangman Game: http://www.bodo.com/Applets/Hangman/in

dex.html

Page 5: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

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

Page 6: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

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:

http://www.cs.fredonia.edu/~zubairi/s2k4/csit225/csit225.html

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

Page 7: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

JCreator

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

Page 8: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

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

ConfigureOptions and check JDK profile

Page 9: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

Console Programs in Java

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

Open JCreator, Choose FileNewFile and choose “Java File”. Give it the name “myfirst.java”

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

Page 10: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

First Program

Public class myfirst { Public static void main(String[] args) { System.out.println(“Hi!! Nice Coffee”); } }

Page 11: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

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

Page 12: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

Exercises

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\"");

Page 13: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

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!!

Page 14: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

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

Page 15: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

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

Page 16: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

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

Page 17: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

Showing Results

Try this line JOptionPane.showMessageDialog(null,"Go

od Morning "+username,"Greeting Window",JOptionPane.INFORMATION_MESSAGE);

Page 18: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

Exercises

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

Page 19: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

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);

Page 20: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

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);

Page 21: Introduction to Java A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

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