Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
ISYE 7210 Simulation of Interactive, Real-Time Discrete Systems (in Java) Initial Notes Q: 2.day.2 9/20/2005 9:13 PM Christine M. Mitchell Center for Human-Machine Systems Research School of Industrial and Systems Engineering Georgia Institute of Technology Atlanta GA 30332-0205 office: ISyE Groseclose, Rm. 334, 404 894-4321 chmsr lab: ISyE “Main” Building, Rm. 426, 404 385-0363 404.385-0360 (fax) {cm}@chmsr.gatech.edu (8.24.05) CHMSR1 7210 Initial Flow Introduction to Java Object-Oriented Programming Java Fundamentals Java Classes Variable names Style guidlines Primitive data types Flow of control (8.24.05) 2 Building a Real-Time Interactive Simulator Real-Time Interactive Simulator Java Classes Java classes Subclasses Inheritance Packages Timing LinkedList Interfaces Simulator Tools Random number generator (RNG) Statistical distributions Event timing Simulation event list Simulation(s) Sim0 Sim1 Sim2 Sim3 (8.24.05) 3 The Java Technology Phenomenon About Java Technology Java is both Programming language Platform Java programming language High-level language characterized by the following buzzwords Simple Interpreted Architecture neutral Multithreaded Object oriented Robust Portable Dynamic Distributed Secure High performance (8.24.05) 4 The Java Technology Phenomenon (cont’d) The buzzwords are explained in The Java Language White paper: James Gosling & Henry McGilton http://java.sun.com/docs/white/langenv/ Java programming language is unusual as it is both Compiled Interpreted Compiler translates source code into Java bytecode Java bytecode Machine-independent Intermediate (8.24.05) code code 5 The Java Technology Phenomenon (cont’d) Java compiler Parses runs source code line, myProgram.java Produces bytecode instructions, myProgram.class Compilation happens once myProgram.java //source code javac myProgram.java //compile source code into bytecode Java interpreter Translates bytecode into machine instructions Intepreter can run a program many times java myProgram //produces output (8.24.05) 6 The Java Technology Phenomenon (cont’d) Java bytecode ‘Write once; run anywhere” Runs on any platform that has a Java compiler Platform-independent code Java Virtual Machine (VM) Interprets bytecode on any implementation of Java VM Creates platform-dependent code (8.24.05) 7 The Java Technology Phenomenon (cont’d) (8.24.05) Platform Hardware &/or software environment in which a program runs Windows 2000 Linux Solaris MacOS Java platform differs Software-only environment Runs on top of hardware-based platforms Java platform has two components: Java Virtual Machine (Java VM) Java Application Programming Interface (Java API) 8 The Java Technology Phenomenon (cont’d) Java “Advantages” Develop programs more quickly Java development may be twice as fast as C++ Why? Fewer lines of code Simpler language Avoid platform dependencies with 100% pure Java Portable Use standard Java libraries Avoid use of libraries written in other languages. (8.24.05) 9 The Java Technology Phenomenon (cont’d) Java “Advantages” (cont’d) Get started quickly “Easy” to learn, especially for programmers of C or C++ Eliminates common C/C++ errors No pointers No array overflow Minimizes memory leakage Write less code Java program can be 4 times smaller than a C++ program Metrics (counts) Number of classes Number of methods Write better code Encourages good coding practices Garbage collection helps avoid memory leaks Object oriented: extensible & reusable code (8.24.05) 10 The Java Technology Phenomenon (cont’d) The Java platform has two components: Java Virtual Machine (Java VM) Java Application Programming Interface (Java API) Large collection of ready-made software components (8.24.05) Includes many capabilities Pre-defined classes, Integer Graphical user interface (GUI) widgets Libraries of related classes 11 Object-Oriented Programming Programming Types (not inclusive) Procedural: FORTRAN, BASIC, C, Pascal int a, b; a = 5; b = -21; Object-oriented: (COBOL*), Smalltalk, C++, Java Class (template) SavingsAccount Instance of a class SavingsAccount chrisSavingsAccount = new SavingsAccount( ); SavingsAccount yourAccount = new SavingsAccount( ); *Common Business Oriented Language (8.24.05) 12 Java Basics A Java program consists of Java source files, Part.java, Inventory.java All Java source files must have the extension, *.java, Part.java Each Java source file contains exactly one Java class A Java source file and a Java class file, which contains the class file, must have exactly the same name, file Part.java implements (contains) class Part Java is case sensitive Java syntax conventions should be used to re-enforce meaning of Java entities, e.g., ClassName, anObject, anInstanceVariable (8.24.05) 13 Java Variables Names & Conventions Variable Names Example declaration & initialization of double variable, balance, set to 0.0 double balance = 0.0; Variable names should Start with a lowercase letter, balance Be short yet meaningful, balance Mnemonic--designed to indicate the intent of use, firstName Written with mixed case, String firstName = “Chris”; One-character variable names should be avoided The exception is for temporary "throwaway" (local) variables (8.24.05) Common names for temporary variables are i, j, k, m 14 First Java Program & Class: Part.java & Part.class Part.java illustrates a self-contained Java program Part.java contains one Java class: Part Part is a self-contained, or complete, because it contains Class declaration Class body main(String[ ] args) method (function) **************** Start Part.java Listing Here************** //Part.java (First Java class example) (see Part) public class Part { //class body starts here //Variables (class & instance) for class Part //Constructors for class Part //Methods (class & instance) for class Part //Part (cont’d) (8.24.05) 15 Part Class //Part public static void main (String[ ] args) { int parts = 50; //local variable System.out.println ("Current number of parts is " + parts); System.out.println ("Current number of parts is " + 5 + " " + parts); //numeric literal parts = parts + 10; //add ten to local variable, parts System.out.println ("Current number of parts is " + parts); System.out.println ("Ends Part class demonstration"); } //end main method } //end class body & end class Part Output Current number of parts is 50 Current number of parts is 5 50 Current number of parts is 50 Current number of parts is 60 Ends Part class demonstration (8.24.05) 16 Compiling & Running a Java Program from a Command Line Open a command window: START | RUN | CMD Go to the directory that contains your java file(s), C:\7210.05\example1\Part.java Compile a Java class, Part.java javac** Part.java //if the results is a “clean compile,” there will be a new file, Part.class Run the Part program java Part //this only works with if the compiler produced a Part.class file (8.24.05) ** javac –g Part.java //the –g provides debugging information 17 Creating, Compiling, & Running Java is Easier in an ISE Tools comprising an IDE (integrated development environment) Create Java source code: myNewClass.java Compile Java source code: javac myNewClass.java Run Java program: java myNewClass Examples (free or lite versions) 1. Most basic--not really integrated editor (vi or emacs) javac on command line java on command line 2. (8.24.05) TextPad Set variables for Java programs Open & create Java file, with indenting & highlighting Compile java file Run java file 18 Creating, Compiling, & Running Java is Easier in an ISE 3. drjava--a lightweight IDE Ongoing development at Rice University used as a teaching tool Free sourceforge.net http://drjava.sourceforge.net/ (8.24.05) See on-line instructions Download Documentation How to Run QuickStart 19 Creating, Compiling, & Running Java is Easier in an ISE 3. drjava (cont’d) Interaction pane (8.24.05) 20 Creating, Compiling, & Running Java is Easier in an ISE 4. NetBeans 4.1 http://www.netbeans.org/ See link to cmm/sva NetBean 4.1 tutorial for GUI development (8.24.05) 21 Creating, Compiling, & Running Java is Easier in an ISE 5. JCreator (Lite) http://www.jcreator.com/ 6. JGrasp (CoC uses this IDE for CS introductory courses) http://www.jgrasp.org/ 7. Eclipse (state of the art) http://www.eclipse.org/ drjava plug-in GUI development plug-in (8.24.05) 22