Download presentation
Presentation is loading. Please wait.
Published byChristiana Stephens Modified over 9 years ago
1
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
2
Jaeki Song ISQS6337 JAVA Outline Object Oriented Programming Class Object Important Concepts Java Application
3
Jaeki Song ISQS6337 JAVA 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
4
Jaeki Song ISQS6337 JAVA 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
5
Jaeki Song ISQS6337 JAVA 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 Java Interpreter CPU JVM
6
Jaeki Song ISQS6337 JAVA 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
7
Jaeki Song ISQS6337 JAVA 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
8
Jaeki Song ISQS6337 JAVA 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 loaded in the browser and could be viewed by applet viewer using the html file in which the applet is placed.
9
Jaeki Song ISQS6337 JAVA Creating First Application The Java 2 Platform, Standard Edition JBuilder4 or 5
10
Jaeki Song ISQS6337 JAVA JBuilder: Interface Main menu Project toolbar Project pane Structure pane Content pane File tab File view tab
11
Jaeki Song ISQS6337 JAVA 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 89 th Street”); System.out.println(“ Lubbock, TX, 79413”); }
12
Jaeki Song ISQS6337 JAVA 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 */
13
Jaeki Song ISQS6337 JAVA 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
14
Jaeki Song ISQS6337 JAVA 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
15
Jaeki Song ISQS6337 JAVA 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) { … }
16
Jaeki Song ISQS6337 JAVA 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 { }
17
Jaeki Song ISQS6337 JAVA Methods and Method Header public static void main(String[] args) //method header { …… } The method as accessible to all classes This method is for class Three parts return value method name arguments lists Void means that this method does not return a value when it is called Method name is main. Main method is the usual starting point for all stand-alone Java program Piece of data. args is an identifier for any string or character argument
18
Jaeki Song ISQS6337 JAVA Body Code { System.out.println(“ Jaeki Song”); System.out.println(“ 1234 89 th Street”); System.out.println(“ Lubbock, TX, 79413”); } Out is the object that represents the default display System is the name of the class (program-defined class) Println is the name of a method that takes a string argument. It returns its value to the System.out device
19
Jaeki Song ISQS6337 JAVA 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
20
Jaeki Song ISQS6337 JAVA Output Dialog showMessageDialog ( null, “string”); –A method of class JOptionPane –Two arguments Syntax JOptionPane.showMessageDialog(null, “string”);
21
Jaeki Song ISQS6337 JAVA 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 }
22
Jaeki Song ISQS6337 JAVA Output
23
Jaeki Song ISQS6337 JAVA 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”);
24
Jaeki Song ISQS6337 JAVA 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); }
25
Jaeki Song ISQS6337 JAVA Output
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.