Introduction to Java - Welcome | SUNY Fredonia

Download Report

Transcript Introduction to Java - Welcome | SUNY Fredonia

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
 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
 Hilo Game:
 http://mainline.brynmawr.edu/Courses/cs11
0/fall2003/Applets/HiLo/Hi.html
 Hangman Game:
 http://www.bodo.com/Applets/Hangman/ind
ex.html
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 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/cs
it225/csit225.html
 On this web page, you can also find the
JCreator v 3.
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
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
 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
“myfirst.java”
 Type the following program into the editor
window, then save, compile and run the
program
First Program
 Public class myfirst {
 Public static void main(String[] args)
{
 System.out.println(“Hi!! Nice Coffee”);
}
}
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
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\"");
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
 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
 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
 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 Results
 Try this line
 JOptionPane.showMessageDialog(null,"Go
od Morning "+username,"Greeting
Window",JOptionPane.INFORMATION_M
ESSAGE);
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
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
 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 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