Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Lecture 03
Introduction to Java
-The First Java Application-
ISQS6337 JAVA
Jaeki Song
Outline
•
•
•
•
•
Object Oriented Programming
Class
Object
Important Concepts
Java Application
ISQS6337 JAVA
Jaeki Song
What is Java?
• Designed in the early of 1990s by Sun
Microsystems
• Provide animation and interactivity on the
World Wide Web
– Web browsers have provided the
opportunities to run Java applets
• The fastest growing language
ISQS6337 JAVA
Jaeki Song
Java Language
• Standard language used for programming,
creating applets, servlets, JavaBeans, and
enterprise components
• Java is simple
• Java is object-oriented language
• Java is distributed
ISQS6337 JAVA
Jaeki Song
Java Language
• Java is interpreted
– Need an interpreter to
run Java program
– The program are
compiled into Java
Virtual Machine (JVM)
code called bytecode
Java Source Code
Java compiler
Java Bytecode Code
JVM
Java Interpreter
CPU
ISQS6337 JAVA
Jaeki Song
Java Language
• Java is robust
– Reliabile
• Detect many problems
• Java is secure
• Java is platform-independent
• Java is portable
– Can be run on any platform without being recompiled
• Java is multithreaded
ISQS6337 JAVA
Jaeki Song
Java Virtual Machine (JVM)
• Interpreter for the Java programming
language
– a simple platform that all Java applications
run on.
• Comes with Java Development Kit (JDK)
– Contains JVM and run-time system
– Java 2 SDK
• www.sun.com
ISQS6337 JAVA
Jaeki Song
Java Environment
• Editor
– Integrated Development Environment (IDE)
• Jbuilder, J++, Forte, Visual Cafe
• Compiler
– Translate into bytecode
• For Sun Microsystems- javac (included in SDK)
• Class loader produces .class file
• Loading
– Applications
• loaded and executed using Java Interpreter java
example.class
– Applet
ISQS6337 JAVA
• loaded in the browser and could be viewed by applet viewer
using the html file in which the applet is placed.
Jaeki Song
Creating First Application
• The Java 2 Platform, Standard Edition
• JBuilder4 or 5
ISQS6337 JAVA
Jaeki Song
JBuilder: Interface
File tab
Main menu
Project
toolbar
Content
pane
Project
pane
Structure
pane
ISQS6337 JAVA
File view tab
Jaeki Song
Example
/* Assignment 1 Printing on the screen
Programmer: Jaeki Song
Student ID : 999-99-9999
Date
: September 2001
Program Name: Address
*/
public class Address
{
public static void main(String[] args) //method header
{
System.out.println(“ Jaeki Song”);
System.out.println(“ 1234 89th Street”);
System.out.println(“ Lubbock, TX, 79413”);
}
}
ISQS6337 JAVA
Jaeki Song
Documentation
• Comments
– Block comment
/* ….. */
– Line comment: //
– e.g.
/* Assignment 1 Printing on the screen
Programmer: Jaeki Song
Student ID : 999-99-9999
Date
: September 2001
Program Name: Address
*/
ISQS6337 JAVA
Jaeki Song
Java Class
• Java program consists of pieces called
classes
– Existing classes in Java Class Libraries
• Known as Java APIs (Applications Programming
Interfaces)
• Classes consists of pieces called methods
– Perform tasks and return information
ISQS6337 JAVA
Jaeki Song
Java Class
• A single class resides in a single Java source file
with extension .java
• public class Address
{
….
}
• The source code is Address.java.
– The name of the class is the name of the file
• Class name and file name must match
ISQS6337 JAVA
Jaeki Song
Main Method
• The class which contains the main()
method is the class that starts running the
program
• Every Java application (not applet) has a
main class
public class Address
{
public static void main(String[] args)
{
…
}
}
ISQS6337 JAVA
Jaeki Song
Access Modifier
• Specifies the circumstances in which the class
can be accessed
– E.g.: public class Address
{
…….
}
• Public indicates that this code can be access by all objects
and can be extended or used as a basis for another class
• The contents of the class must be enclosed in braces { }
ISQS6337 JAVA
Jaeki Song
Methods and Method Header
public static void main(String[] args) //method header
{
……
The method
as accessible
to all classes
Three parts
• return value
}
This method is for class
ISQS6337 JAVA
Void means that this method
does not return a value when
it is called
• method name
Method name is main. Main
method is the usual starting
point for all stand-alone
Java program
• arguments lists
Piece of data. args is an
identifier for any string or
character argument
Jaeki Song
Body Code
{
System.out.println(“ Jaeki Song”);
System.out.println(“ 1234 89th Street”);
System.out.println(“ Lubbock, TX, 79413”);
}
System is the name of the class (program-defined class)
Out is the object that represents the default display
Println is the name of a method that
takes a string argument. It returns its value to the
System.out device
ISQS6337 JAVA
Jaeki Song
Using Java Swing Class
• Refers to the new library of GUI
– A component set that makes up all the objects of GUI
• Displays output using windows or dialog boxes
– Input Dialog and Output Dialog
• Use packages
– Predefined classes grouped into categories of related
classes called packages (sometimes called java class
libraries or java applications programming interface
(API))
– JOptionPane
• Defined in a package called javax.swing
ISQS6337 JAVA
Jaeki Song
Output Dialog
• showMessageDialog ( null, “string”);
– A method of class JOptionPane
– Two arguments
• Syntax
JOptionPane.showMessageDialog(null, “string”);
ISQS6337 JAVA
Jaeki Song
Example: Output Dialog
import javax.swing.JOptionPane; //import class JOptionPane
public class Address
{
public static void main(String[] args) //method header
{
JOptionPane.showMessageDialog(
null, " Jaeki Song\n1234 89th Street\n
Lubbock, TX, 79413");
System.exit(0); //terminate program
}
}
ISQS6337 JAVA
Jaeki Song
Output
ISQS6337 JAVA
Jaeki Song
Input Dialog
• Uses predefined dialog box from class
JOptionPane called input dialog
– Allows the user to input a value for use in the
program
– Syntax
JOptionPane.showInputDialog(“ Enter first integer”);
ISQS6337 JAVA
Jaeki Song
Example: Add Integer
import javax.swing.JoptionPane;
public class AddInt
{
public static void main (String args[])
{
String number1, number2; //first and second string entered by user
int numInt1, numInt2, sum;
number1 = JOptionPane.showInputDialog(“Enter first number”);
number2 = JOptionPane.showInputDialog(“Enter second number”);
numInt1 = Integer.parseInt(number1);
numInt2 = Integer.parseInt(number2);
sum = numInt1 + numInt2;
JOptionPane.showMessageDialog(null, “The sum is “ + sum, “Results”,
JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}
ISQS6337 JAVA
Jaeki Song
Output
ISQS6337 JAVA
Jaeki Song