itm 172 l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
ITM 172 PowerPoint Presentation
Download Presentation
ITM 172

Loading in 2 Seconds...

play fullscreen
1 / 58

ITM 172 - PowerPoint PPT Presentation


  • 232 Views
  • Uploaded on

ITM 172 The Java Programming Language Chapter 1 – Part A Introduction to Java and the Development Tools Java: Why it is a popular programming language Main reason: Programs written in Java can run on any type of computer.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'ITM 172' - RoyLauris


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
itm 172

ITM 172

The Java Programming Language

Chapter 1 – Part A

Introduction to Java and the Development Tools

java why it is a popular programming language
Java: Why it is a popular programming language
  • Main reason: Programs written in Java can run on any type of computer.
  • A computer’s processor has an instruction set. (a list of all the instructions it understands).
  • A Pentium IV processor’s instruction set is not identical to DEC’s Alpha processor’s instruction set or Sun’s Sparc processor’s instruction set.
  • Therefore, if you write a program and then compile it so that it runs on a Pentium processor, it might not run on an Alpha or Sparc processor (because the program contains instructions that are not in that processor’s instruction set).
  • When Java programs are compiled, they can run on any type of processor (Therefore, Java programs have high portabilityand arearchitecturally-neutral).
the portability of java programs
The portability of Java programs
  • The way that Java programs must be compiled into bytecode and then interpreted by the JVM is what makes Java programs completely portable.
  • Portable means that a Java application can run on any hardware/operating system combination.
  • Since Java programs are not translated into a specific processor’s machine language until runtime, the program can ‘wait’ until it sees what platform it has ‘landed on’ before it finishes compiling. Then it can adapt itself by compiling into any specific processor’s machine language at runtime.
other advantages of java
Other advantages of Java
  • Simple. It’s easier to learn than C++
  • Object-oriented. Programming code is organized into modules/building blocks/objects. Huge libraries of these “building blocks” already exist. Programs can be built using standard building blocks.
  • Distributed. Java programs can interface (communicate with) other programs running on other computers using standardized communication methods.
  • Multi-threaded. The Java language allows the programmer to write programs that can “spawn processes simultaneously”.
other advantages of java5
Other advantages of Java
  • Dynamic. Java is an extensible language. That means, new functionality can be added indefinitely.
  • Robust. Java programs don’t crash as readily as programs written in other languages because the compiler does some “forward thinking” error detection and correction.
  • Secure. Viruses cannot embed themselves in Java programs.
java application development

No

Source Code

file

(*.java)

Compile

(using javac.exe)

Syntax

OK?

No

Yes

Logic

OK?

Interpret & execute

the bytecode

(using java.exe)

Bytecode Class

files

(*.class)

Yes

Turn it in!

Java Application Development

1

4

2

3

Write Source Code in Java

5

7

6

8

java.exe is the VIRTUAL MACHINE

the java platform
The Java platform
  • In order for an application program to run on a computer, certain resources must be available to it.
  • Resources consists of hardware and software. Collectively, these resources are called the ‘hosting’ platform.
  • The next slides will explain what a platform is, and then what platform Java applications require.
what is a computer platform
What is a computer platform?
  • In computing, a platform describes all the hardware and software programs that must be available (as resources) in order for an application to run.
    • Example: A personal computer (complete with a CPU and a RAM memory) running the Windows Vista operating system.
    • Example: A high-end computer (multiple processors, many GBs of RAM) running the Unix operating system.
what does the platform provide
What does the platform provide?
  • The application program cannot carry out all computing tasks on its own without help from other programs (we call ‘services’) that are included in the operating system software package and the hardware.
  • For example, Microsoft Word cannot carry out all the word processing tasks (such as printing, saving and opening files, etc.) without the help of the operating system’s helper programs (some may be in .dll files) and the hardware (CPU, RAM, disk drive, printer).
the java platform10
The Java Platform
  • A Java application cannot execute unless it is running on a computer hosting the Java platform.
  • The Java platform consists of these ‘components’ that must be available at runtime: the hardware, the operating system, and the JRE(the Java runtime environment).
  • Non-Java applications only need two components in their platform: the hardware and the operating system.
the java platform11
The Java Platform

So, to run a Java application, there must be:

  • Hardware (a CPU, RAM, etc.)
  • an operating system
  • The JRE

a. the JVM (the byte code interpreter and the program launcher)

b. the class library (helper modules – similar to .dll files that augment the functionality of the operating system).

jre part a the jvm java virtual machine
JRE: Part A: The JVM (Java Virtual Machine)
  • The JVM consists of two things: it uses its interpreter to translate the byte code into machine language and it uses its launcher to initiate the running of the application.
    • The JVM’s interpreter is the program that completes the translation process by translating the byte code into a specific processor’s machine language.
jre part b the class library
JRE Part B:The class library
  • The other component of the JRE is the class library (aka API - Application Programming Interface)
  • The API is a large collection of reusable ‘modules’ of code. Many of the modules in this library are used like ‘.dll’ files – providing ‘helper’ programs that augment the functionality of the operating system.
java development tools
Java Development Tools
  • We just finished discussing what ‘resources’ are needed to run a Java application.
  • Now we will discuss what resources are available to develop a Java application.
  • These resources are called ‘tools’:
    • The JDK (Java Development Kit)
    • An IDE (NetBeans)
the java development toolkit jdk
The Java Development Toolkit (JDK)
  • To create, compile, and run a Java program, you need a program called the Java Development Toolkit (JDK). The toolkit includes:
    • javac – The compiler, which converts source code into Java bytecode
    • jar – The archiver, which packages related class libraries into a single file (similar to a ‘zip’ program).
    • javadoc – The documentation generator, which automatically generates documentation from source code comments
    • jdb – The debugger
    • JRE (for testing purposes)
the integrated development environment ide
The Integrated Development Environment (IDE)
  • The IDE is like the ‘dashboard’ of your car: it makes several programming tools accessible from one “integrated”, GUI-style screen.
  • These tools include the text editor, compiler, run-time module, debugger, and help system. The IDE also ‘forces’ you to organize your files into projects and packages.
  • The name of our IDE is NetBeans. (Other products that are IDEs include: Forte by Sun, Jbuilder by Borland, Visual J++ by Microsoft, Visual Café by WebGain).
slide17

If you want to download and install the JDK and the IDE (Netbeans) on your home computer, go to www.java.sun.com and select this link.

getting started lab 1
Getting started (Lab 1)
  • The JDK version J2SE 6.3 (containing the JRE) and Netbeans 6.0 have already been installed on your PC.
lab 1 1 part a class welcome in package chapter1
Lab 1.1- Part A - Class Welcome in Package Chapter1
  • This program displays the message “Welcome to Java” on the screen (in the standard output window – not in a Window’s window).
slide21

Start the IDE (NetBeans): From the Window’s desktop, click [Start], [Programs], [NetBeans 6.3], [NetBeans IDE]…

slide23

If you use NetBeans, all programs must be encapsulated in a project. A project folder is created and default settings are applied to the project folder. To create a project, from the menu, click [File], [New Project]…

slide24

… chose [General] for the project category and [Java Application] for the type of project. Click [Next]…

slide25

Type the Project Name: Welcome

Type the Project Location: C:\251lastnameLABS

Unselect [Create Main Class]. Click [Finish].

slide26

See the project name appear in the Projects panel. At least two folders are created below the projects folder. Click the [+] to expand the ‘Welcome’ project folder, and then click the [+] to expand the ‘Source Packages’ folder. Just as a project is a ‘container’ for an application, a package is a ‘container’ (also a folder) for a collection of similar files within a project.

slide27

All Java code must be encapsulated in class files. Therefore, the next step is to create a class/file. From the menu, select [File], [New File]…

slide28

…in the “New File” window, click [Java Classes] as the Category and [Java Main Class] as the file type. Click [Next]…

slide29

welcome

…type the class name: Welcome (same as the project name). Type the package name: welcome. Click [Finish].

slide30

The class structure

The ‘main’ method structure

A file is created that contains some ‘startup’ code. It opens in the text editor window. A class structure is created (with the class name ‘Welcome’) and a method is created (with the name ‘main’).

slide31

Type your name and the lab number in the comment area in the top. Delete everything except what you see in this screen shot.

slide32

To create our first Java program, that displays the words, “Welcome to Java”, type in method main:

System.out.println(“Welcome to Java”);

slide33

To compile the program, from the menu, select [Build], [Compile “Welcome.java”] or press F9.

slide34

The output window shows if you have any syntax errors. If not, you will see the message “BUILD SUCCESSFUL”.

slide36

When the program runs, you will see the message ‘Welcome to Java’ appear in the output window.

slide37

Minimize the NetBeans application window to view the Windows desktop. Click on [Folders] view on the toolbar. Navigate to the source file that you created. It is under [Local Disk C], [251lastnameLABS], [Welcome], [build], [classes], [welcome]. See one file: Welcome.class

anatomy of a java program
Anatomy of a Java Program
  • Programming statements are grouped into methods.
  • Methods and variables are grouped into classes.
  • Each class is stored in its own unique file with the same name as the class.
  • Files are grouped into packages
  • Packages are grouped into projects.
system out println
System.out.println
  • To display a line of text on a computer screen, we invoked the pre-existing method: println( ).
  • When you want your program to display a string of text in the standard output window, invoke the println method and “pass” it the string you want displayed (“Welcome to Java”).
  • The println( ) method is method belonging to the ‘outstream’ class, and ‘out’ is an instance of an outstream. ‘out’ is declared in the System class.
  • The System class is in the java.lang package, which is in the API (the collection of class files included in the JRE). Recall that the JRE contains a collection of ‘helper’ modules that can be ‘called upon’ to perform certain processes on behalf of your program.
comments
Comments
  • Lines that begin with // or multiple lines enclosed in /* …. */

Example:

// This is a comment.

/* This is also a comment. */

  • Comments are ignored by the compiler.
reserved words
Reserved Words
  • Reserved or key words have special meaning to the compiler and cannot be used for other purposes in a program
  • Example: words like class, package, public, main, static, void.
modifiers
Modifiers
  • Modifiers are key words that specify properties of data, methods, and classes. For example, the key word public specifies whether other methods may invoke the methods and/or access the data (variables that are) declared in this class.
blocks
Blocks
  • A pair of curly braces { } form a block. There are class blocks, method blocks, and if blocks, while blocks, etc.
the main method
The main( ) method
  • All executable classes must contain a method named main( ).
  • Main( )will be the first method invoked when a class (or an instance/object of that class) is loaded into memory.
  • Because main( ) is method, it must have a header and a body. Its header looks like this:

public static void main(String[ ] args)

  • Later, this will make more sense to you.
lab 1 1 part b modify class welcome in package chapter1
Lab 1.1 – Part B – modify class Welcome in Package Chapter1
  • In this program, we simply modify the way we display the output.
  • The output of this program is a text string: “Welcome to Java”.
  • Instead of displaying the string in the “standard output window”, we’ll display it in a messageDialog “box”.
using gui objects in a java program
Using GUI objects in a Java Program
  • If you don’t specify otherwise, the output (“Welcome to Java”) of a Java program (such as Welcome.java) will display in the “standard output window”.
  • Modern day programs generally display their output in a Window’s window called a GUI (Graphic User Interface) component. An example of a GUI component that might be used to display output is a message dialog box.
  • We will modify Welcome.java so that its output displays in a message dialog box instead of the ‘standard output window’.
slide48

Start the IDE (NetBeans): From the Window’s desktop, click [Start], [Programs], [NetBeans 6.0], [NetBeans IDE]…

slide49

welcome;

The last file that we edited should display in the editing window. (If not, click [File], [Open Project].

slide51

To compile the program, from the menu, select [Build], [Compile “Welcome.java”] or press F9.

slide52

The output window shows if you have any syntax errors. If not, you will see the message “BUILD SUCCESSFUL”.

slide54

When the program runs, you will see the message ‘Welcome to Java’ appear in the Message Dialog window.

slide55

The window title is “Example 1.2 output”

The text string is “Welcome to Java”

The INFORMATION MESSAGE icon.

Click the [ok] button to dismiss the dialog box.

joptionpane showmessagedialog
JOptionPane.showMessageDialog( )
  • When method showMessagedialog( ) is invoked, it displays a dialog box on the screen. (A dialog box is one type of GUI component).
  • showMessageDialog( ) is a method of static class JOptionPane, therefore, to invoke this method, we use the format classname.methodname:

JOptionPane.showMessageDialog(…)

  • Method showMessageDialog( ) requires 4 arguments:
    • Null: means none. (This argument won’t be explained now.)
    • Text to be displayed: “Welcome to Java”
    • Title of box: “Example 1.2 output”
    • Type of icon to display in the box: INFORMATION.MESSAGE
import javax swing joptionpane
Import javax.swing.JOptionPane
  • If you want your program to invoke an external method (method showMessageDialog( )), the class that the method belongs to (class JOptionPane) must be included (imported) into your program.
  • Import statements must include the ‘package name’. Class JOptionPane is in package javax.swing (like most of the other GUI component classes).
  • Where class System (in the Java.lang package) is automatically imported into every Java program, class JOptionPane is not implicitly imported; it must be explicitly imported.
system exit 0
System.exit(0);
  • exit( ) is another method that belongs to class System.
  • Since System is a static class, we use the format classname.methodname to invoke method exit( ).
  • Method System.exit( ) terminates the program. You should explicitly terminate a program so that all “threads” “spawned” by your program will be terminated.
  • Method System.exit( ) take a single integer argument (0). The zero is used as a ‘signal’ to indicate to the operating system that the program terminated “normally”.