730 likes | 743 Views
Learn about Java programming, syntax, OOP, GUI, and unit testing. Discover Java's platform independence and usage in different operating systems. Develop your first Java program.
E N D
Lesson 1The Java WorldAUBG ICoSCIS TeamAssoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad
Time Schedule March 23, Saturday - March 24, Sunday • 10:00 – 10:45 09:00 – 09:45 • 11:00 – 11:45 10:00 – 10:45 • 12:00 – 12:45 11:00 – 11:45 Lunch Break (45’) Lunch Break (45’) • 13:30 – 14:15 12:30 – 13:15 • 14:30 – 15:15 13:30 – 14:15 • 15:30 – 16:15 14:30 – 15:15
Lessons Schedule Lesson 1: The Java World Lesson 2: The Java PL – basic syntax Lesson 3: The Java PL – StrProg and OOP Lesson 4: Exception Handling in Java Lesson 5: Java and GUI Programming Lesson 6: Event Driven Programming in Java Lesson 7: Unit Testing in Java Lesson 8: Java Graphics
Lesson contents • Why Java? Or Java popularity • How Java works: • JRE, JVM, JDK • Windows Environment • Java IDEs • UNIX/Linux Environment • First Java Program
Why Java? Java enables users to develop and deploy applications on the Internet for servers, desktop computers, and small hand-held devices. • Java is a general purpose programming language. • Java is O-O programming language • Java is the Internet programming language. • Java programs may be applications or applets or servlets. • Applications are standalone programs, similar to .NET Console and Windows applications. • Applets are similar to applications, but they do not run as standalone programs. • - Instead, applets adhere to a set of conventions that lets them run within a Java-compatible browser (client-side). • - You can only run an applet from an HTML page. 5
The Java popularity • TIOBE Programming Community Index for January 2013 • http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
The Java World • Software aspects • Operating Systems • Language Processors • Development Environments
Operating Systems • Java implemented under • UNIX • Linux • Windows • MAC OS • MS-DOS
Language Processors • In order to run /to execute/, any program written in HLL (incl. Java) should be transformed to executable form • Three ways to convert source code into executable form: • Compiler – generate object code – see slide+1 • Interpreter – interpret source code – see+2 • Compiler of hybrid (semi-interpreting) type - +3
data Object program Executing computer Results Source program compiler Run time Compile time
Data Interpreter Results Source program Compile time Run time
data Object program in IL form Interpreter Results Source program compiler Compile2 time Run time Compile1 time
How Java works Java compiled to intermediate form – byte code. Byte code further processed: by interpreter named JVM OR by JIT compiler. Reminder and Refresh The SDLC concept - see next slide Java Programming: From Problem Analysis to Program Design, 4e 13
Problem-Analysis-Coding-Execution Cycle Java Programming: From Problem Analysis to Program Design, 4e 14
How Java Works • Java's platform independence is achieved by the use of the Java Virtual Machine (JVM). • A Java program consists of one or more files with a .java extension • these are just text (i.e. source) files. • When a Java program is compiled, the .java files are fed to the compiler which produces a .class file for each .java file. • The .class file contains Java bytecode. • Java byte code is like machine language, but it is intended for the Java Virtual Machine, not for a specific processor.
Java Components of special interest • Pure Java includes 3 software facilities: • JRE (includes JVM) • JVM (a part of JRE) • JDK
JRE • The Java Runtime Environment (JRE) provides • the predefined class libraries, • the Java Virtual Machine, and • other components to run applets and applications written in Java.
JVM - Overview • Java Virtual Machine is a program which executes programs, namely those containing Java bytecode instructions. • JVM is distributed along with Java Class Library, a set of standard class libraries (in Java bytecode) that implement the Java application programming interface (API). These libraries, bundled together with the JVM, form the Java Runtime Environment (JRE). • The use of the same bytecode for all JVMs on all platforms allows Java to be described as a write once, run anywhereProgLan, versus write once, compile anywhere, which describes cross-platform compiled languages. • Oracle Corporation, the owner of the Javatrademark, produces most widely used JVM, named HotSpot, that is written in C++
JVM • JVM architecture. Source code is compiled to Java bytecode, which is verified, interpreted or JIT-compiled for the native architecture. The Java APIs and JVM together make up the Java Runtime Environment (JRE).
JDK contents The JDK has as its primary components a collection of programming tools, including:
JDK contents (most often used utilities) • appletviewer – this tool can be used to run and debug Java applets without a web browser • java – the loader for Java applications. This tool is an interpreter and can interpret the class files generated by the javac compiler. • javac – the Java compiler, which converts source code into Java bytecode • javadoc – the documentation generator, which automatically generates documentation from source code comments • jar – the archiver, which packages related class libraries into a single JAR file. This tool also helps manage JAR files.
JDK contents (full list of utilities) appletviewer – this tool can be used to run and debug Java applets without a web browser apt – the annotation-processing tool4 extcheck – a utility which can detect JAR-file conflicts idlj – the IDL-to-Java compiler. This utility generates Java bindings from a given Java IDL file. java – the loader for Java applications. This tool is an interpreter and can interpret the class files generated by the javac compiler. javac – the Java compiler, which converts source code into Java bytecode javadoc – the documentation generator, which automatically generates documentation from source code comments jar – the archiver, which packages related class libraries into a single JAR file. This tool also helps manage JAR files. Full list of utilities is too long and is not a subject of this lesson 26
A Simple Java Program Listing 1.1 //This program prints message “Welcome to Java!“ // braces in end-line style public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }
A Simple Java Program Listing 1.1 //This program prints message “Welcome to Java!“ // braces in new-line style public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }
Creating, Compiling & Running Java Programs • From the Command Window • Details follow
To open command window • Click Start • Select All Programs > • Select Accessories • Click Command Prompt
Compiling and Running Java programs • Using any text editor , /notepad, edit, write/ type the source text of a Java demo program like this: public class prog4 { public static void main(String[] args){ System.out.println("Hello from Java"); } } • Save the Java program as a prog4.java file.
Compiling and Running Java programs • Run compiler with statement like this: javac prog4.java • Run application with stmt like this: java prog4
Creating, Compiling & Running Java Programs • From within IDE jGRASP • Details follow http://www.jgrasp.org http://www.jgrasp.org/tutorials187/02_Getting_Started.pdf
jGRASP – introduction • jGRASP is a lightweight development environment, implemented in Java, and runs on all platforms with a Java Virtual Machine (Java version 1.5 or higher). • jGRASP is an academic style IDE. • jGRASP is developed by the Department of Computer Science and Software Engineering in the Samuel Ginn College of Engineering at Auburn University.
jGRASP – JGrasp02_Getting_Started.pdf 2.1 Starting jGRASP jGRASP virtual desktop gets displayed: menu bar tool bar left pane – Browse tab, Find tab, Debug tab large pane – for UML and CSD windows lower pane – Messages tab, run I/O tab
jGRASP – JGrasp02_Getting_Started.pdf 2.2 Opening a program, compiling and running File > Open > select a file in a folder Build > Compile Build > Run |> Run as Application |> Run as Applet 2.3 Creating a New File File > New File > Java 2.4 Saving a File File > Save | Save As
jGRASP – JGrasp02_Getting_Started.pdf 2.5 Building Java programs - Recap
jGRASP – JGrasp02_Getting_Started.pdf 2.10 Compiling a Program: A Few More Details When you compile the program, it is automatically saved. Settings > check box Auto Save
jGRASP – JGrasp02_Getting_Started.pdf 2.11 Running a Program: Additional Run > check box Run in MSDOS Window Run > Arguments 2.14 Closing a File File > Close |Close All
Exercises/Tasks • Type, compile, run a program: Lesson01_a.java public class Lesson01_a { public static void main(String args[]) { System.out.print("ICosCIS partner AUBG greets"); System.out.println(" ICosCIS partner SWU."); } // end of main } // end of class
Exercises/Tasks • Write a Java program Proba3.java: • To display 5 times the string “Good luck, dear ICoSCIS student!!” • To enter two numeric integer/real values and To display their sum and product.
Exercises/Tasks for(int i=1; i<=5; i++){ System.out.print("Good luck, dear"); System.out.println(" ICoSCIS student!!"); }
Exercises/Tasks import java.util.Scanner; … Scanner cin = new Scanner(System.in); System.out.println("Enter two numeric values:"); double inp1, inp2, result1; inp1 = cin.nextDouble(); inp2 = cin.nextDouble(); result1 = inp1 + inp2; System.out.println(" result is = " + result1);
Exercises/Tasks import java.util.Scanner; public class Proba3 { /** * @param args the command line arguments */ public static void main(String[] args) { for(int i=1; i<=5; i++) System.out.println("Good luck, dear ICoSCIS student!!"); Scanner cin = new Scanner(System.in); System.out.println("\n Enter two real numeric values:"); double inp1, inp2, result1; inp1 = cin.nextDouble(); inp2 = cin.nextDouble(); result1 = inp1 + inp2; System.out.println(" result of addition is = " + result1); } // end of main } // end of class
Creating, Compiling & Running Java Programs • From within IDE NetBeans • Details follow • NetBeans IDE Java Quick Start Tutorial (http://netbeans.org/kb/docs/java/quickstart.html) • Introduction to GUI Building (http://netbeans.org/kb/docs/java/gui-functionality.html
NetBeans – introduction • NetBeans refers to both • NetBeans Platform • NetBeans IDE • NetBeans Platform is a reusable framework for simplifying the development of Java Swing desktop applications. • NetBeans IDE is an open-source industry style integrated development environment. NetBeans IDE supports development of all Java application types. • Current installed NetBeans IDE version - 7.0.1 • Runs on Windows, Linux, Mac OS X and Solaris.
To create an IDE project: • 1. Start NetBeans IDE. • 2. In the IDE, choose File > New Project… (Ctrl+Shift+N), as shown in the figure below.
To create an IDE project: • 3. In the New Project wizard, expand the Java category and select Java Application as shown in the figure below. Then click Next.