Download Lesson 1: Introduction

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Contents
Part 1: Object Oriented Principles and Java main structures
Lesson 1: Introduction
Lesson 2: Variables and Expressions
Lesson 3: Control Structure
Lesson 4: Class Fields and methods
Lesson 5: Instance fields and methods
Lesson 6: Constructor methods
Lesson 7: Protect instance Fields
Lesson 8: Inheritance
Lesson 9: super and this
Lesson 10: abstract classes and interfaces
Lesson 11: arrays
Lesson 12: packages and access Modifier
Part 2: Some Java Classes
Lesson 13: Exception Handling
Lesson 14: Vector Class
Lesson 15: Strings Class
Lesson 16: Wrapper Classes
Lesson 17: Input and Output
Lesson 18: Useful classes
Part 3: AWT Classes
Lesson 19: AWT Classes
Lesson 20: Layout Classes
Lesson 21: AWT Components
Lesson 22: Events
Lesson 23: Graphics
Lesson 24: Menus
Lesson 25: Applet
Lesson 26: Images
Part 4: Advance Topics
Lesson 27: Threads
Java Programming
Lesson 1
Lesson 1
Introduction
Features
Java has captured the attention of programmers because of certain features, happen to
make Java the ideal language for building programs for use on the Internet. Such
features:
1.
Run on a wide variety of HW platforms (portable)
2.
Supports applet, which can be executed using browsers.
3.
It’s a completely OOPL.
4.
Can work on multiple tasks simultaneously.
5.
Java automatically recycles memory.
Byte-code
Java compiler translates programs into byte-code. This byte code seam to be written
in the instruction set of typical computer. Byte-code is executed by a program called
Java virtual machine (JVM) or Java byte code interpreter, which it has been
implemented for a particular computer. For windows environment, two programs can
be found: jvc.exe (Java compiler) and jview.exe (JVM interpreter).
Java programs can be executed alone as an application, or executed in the browsers as
an applets. In the first part of this book, we will concentrate on applications, while the
second part on the applets.
Class
Main unit in Java project is a class. A class is construction that contains a set of fields
and a set of methods. The syntax of a class is:
[Class modifier] class class-name
{
// Set of fields and methods
}
Its preferred to write each class in a separate file. This file must have the same name
as the class name and with the same case (Java is a case sensitive language). The file
has “Java” as an extension.
Class modifier: none (friendly), public, private, abstract, final
Applications
Application is a set of classes one of them has the main method. Java application must
have one and only one method with the name “main”. The program starts run at the
first statement inside the main method. Main method syntax:
Public static void main (String args[])
{
// Statements
}
How to compile and run your application?
We will use visual studio version 6.0. Java is found in the second CD. And you will
use also the MSDN (CD 5,6). The steps are:
1.
Start VJ++ 6.0
2
Java Programming
Lesson 1
2.
Select new Project from file menu. A dialog with two parts (left and right)
will appear.
3.
From Left window, select application.
4.
From Right windows, select console application.
5.
Write the name and it’s location, and then click on Open. This will create a
folder for your project. This folder contains a file called “class1.java” which
contains the main method. You can use it, or delete it and create another file.

To add a new file:
1.
R-click the project windows on the project name. A context window will
appear.
2.
Select add, add class, new tab, then choose (main class, class, interface)
3.
Write the name of the class and the open.

To specify the main class:
1.
R-click on the project name in project explorer.
2.
Select Properties, Launch tab, now choose the name of the file.

To compile programs use the build menu.

To run programs use the debug menu.
How to stop the output window?
After you execute Java application, the output disappears directly and returns to the
visual studio environment. To stop the output window in order to see the result: add at
the end of the main method: System.in.read (); this statements throws an Exception,
so you have to add at the end of main method header: throws Exception
Public static void main (String args[]) throws Exception
{
// Statements
System.in.read ();
}
Note: Each statement must be end with semi colon. ({ … }; error)
3