Object-Oriented Programming I - Introduction to Java

farrightΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 4 χρόνια και 8 μήνες)

477 εμφανίσεις

Azam

and
Rui

2

Lec


AA

M
-
W
----


13:30
-

16:00

H
-
520

Dr. Fung

Tut

AE

M
-
W
----


16:40
-

18:20

H847

Azam

Tut

AF

M
-
W
----


16:40
-

18:20

H967

Rui



You need an ENCS account in order to
access ENCS labs and to use the computers.


Please go to the ENCS Help Desk (H
-
960)
for your account.


Please go to the following link for the door
codes:


https://fis.encs.concordia.ca/helpdesk
-
cgi/doorcode.cgi

3

A Sample Java Program

4


The
main

method is first invoked when the
program is run.


Since Java is a pure object
-
oriented
programming language, even the
main

function has to be defined in a class.


The class name should be the same as the
name of the file in which the class is defined,
except that the file name has .java added to
the end.

Java Program Structure

5


To create a Java program, you have two
options:


Use any text editor, e.g.
Notepad
,
WordPad
,
vi
, and
emacs
. (may NOT be the best way)


Use IDEs, e.g.
NetBeans

and
Eclipse
.
(
Recommended
)

Where to create a Java Program

6


What is an IDE?


An
integrated development environment
(
IDE
) also known as
integrated design
environment
or
integrated debugging
environment

is a software application that
provides comprehensive facilities to
computer programmers for software
development.



Editing in an IDE

7


An IDE for Java normally consists of:


A
source code editor


A
compiler
, which
translates Java
programs into programs in byte
-
code


An
interpreter
, which further translates
byte
-
code programs into programs in
machine language


Build automation tools

?C
A
debugger


Editing in an IDE

8


Launching NetBeans:


Click on Start button in the bottom
left corner of the screen


Move the cursor on Programs and then on
NetBeans


Click on NetBeans IDE 6.7.1


Editing in an IDE

9


How NetBeans looks like?

Editing in an IDE

10


Create a program in NetBeans:


Go to File
-
> New Project...


In the New Project wizard, click on the Java category
and select Java Application, then click Next.


Editing in an IDE

11


Type a project name


Make sure that the project is located under your G:
drive, otherwise it may be deleted after you log off.


Click Finish


12

Change it to a directory under G: drive !


After the project is
created, a default
Main.java file with
a main class inside
is also created.


Now you can type
the source code in
the right part.

13

Edit here


How to change the file name/class name?


Recall that the names of the file and the
class have to be identical. So if you decide
to change one, don’t forget the other.


To change the class name, you can directly
edit it in the text editing area; for the file
name, you will need to right click the file
and then go to Refactor
-
> Rename...





Editing in an IDE

14


To compile/run a Java program, you also
have two options:


Use command lines if you have created your
program using a text editor.


Use IDEs.

How to compile/run a Java program

15


Save your code as (*.java) file with the same
name as the class.



Go to the directory where you created your
program using DOS commands. How?



Start
-
> Run
-
> type cmd, and then use cd
command).



16


To compile your program, type:



javac
filename
(e.g. javac Test.java)



To run your program, after compiling your
program, type:



java
filename_without_extension_name


(e.g. java Test)

Using command lines

17


For example

Using command lines

18


write your program in NetBeans


Save your program


Either go to Run
-
> Run Main Project






Or click the Run icon


19

Here


Now you should be able to see the output in
the output window.

20

Here


If you want to install NetBeans on your own
computer, please visit:


http://java.sun.com/javase/downloads/widge
t/jdk_netbeans.jsp



Note that you need
BOTH

Java SE
Development Kit (JDK) and NetBeans in order
to compile/run your program.

Install
NetBeans

and JDK

21

Please visit the web site:
http://www.netbeans.org/kb/

22


You need to capture snapshots of the output
of your programs to demonstrate the
correctness of your assignments.


How?


Press PrintScreen key (to capture the entire screen)
or Alt+PrintScreen (to capture the active window
only)


Open a Word file (or others), and right click to
choose paste.

Capturing snapshots

23