editing java programs with the bluej ide. working environments to develop (= write) programs there...

58
Editing Java programs with the BlueJ IDE

Upload: dwain-merritt

Post on 24-Dec-2015

223 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Editing Java programs with the BlueJ IDE

Page 2: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Working environments to develop (= write) programs

• There are 2 ways to develop (write) computer programs:

1. Using an editor (e.g. gedit) and a compiler (such as javac) separately.

• You have seen this method in the last webnote --- http://192.168.1.3/~cheung/teaching/web/170/Syllabus/02/BlueJ/java.html

2. Using an editor and a compiler in an integrated manner

Page 3: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Working environments to develop (= write) programs (cont.)

• In the second way, you will need to install a special application called an Integrated Development Environment (IDE)

Page 4: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Java IDEs

• There are a number of Integrated Development Environment IDE) available for Java

• Java IDEs:

• Eclipse -- Eclipse is highly extensible and customizable, but hard to learn (freely available)

• NetBeans -- created by Sun MircoSystem (original designer of the Java programming language) (freely available)

• JBuilder -- top commercial Java IDE; very costly...

• BlueJ -- easy to learn (freely available)

Page 5: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Java IDEs (cont.)

• In this webnote, you will learn to edit Java programs with BlueJ

• In the next webnote, you will learn how to:

• You will learn how to program in the Java programming language later in this course

• compile the Java program with BlueJ

• run the (compiled) Java program with BlueJ

Page 6: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Java IDEs (cont.)

• BlueJ is freely available and it can be obtained from this website: http://www.bluej.org/

Page 7: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Preparation

• Before you can use BlueJ, you must:

• Login to a computer in the MathCS lab

• Open a terminal window

• Change the current (working) directory to your cs170 directory This directory is used to store CS 170 labs and homework.

Page 8: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Information about this BlueJ tutorial

• The tutorial is described from the perspective of the user cheung

(Because it was developed by Professor Cheung)

• The directory used to hold the project is /home/cheung/cs170

For clarity, I have delete all files and folders from my cs170 directory.

Page 9: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Information about this BlueJ tutorial (cont.)

• We will write a simple Java program and store the program in a project directory called "TestProj".

The "TestProj" will be contained inside the /home/cheung/cs170 directory.

• In other words, the absolute path of the project directory is:

• /home/cheung/cs170/TestProj

Page 10: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Information about this BlueJ tutorial (cont.)

• Here is the Simple Java program that you will enter into BlueJ:

You don't need to understand this program right now; it will be explained later in the course.

public class Hello

{

public static void main(String[] args)

{

System.out.println("Hello Class"); System.out.println("How is

everyone doing so far ?");

}

}

Page 11: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Topics covered in this (short) tutorial

• Things you need to learn to get started with BlueJ

• Run the BlueJ application

• Create a new project in BlueJ

• Create a new program file

• Insert text into the file

• Delete text from the file

• Goto a certain line in the file

• Search for a pattern in the file

• Search and replace for a pattern with another pattern in the file

• Undo a change

• Save your work

• Quit without saving (because you made a mess)...

Page 12: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Starting the BlueJ IDE application

• Enter the following command in a terminal window:

• This will run BlueJ as a detached process

UNIX prompt>> bluej &

Page 13: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Starting the BlueJ IDE application (cont.)

• You will first see an announcement window:

Page 14: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Starting the BlueJ IDE application (cont.)

• When it's ready, you will see the main window:

Page 15: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Create a new project

• BlueJ requires that each project be stored in a different directory

When you create a new project, BlueJ will also create a new directory for you.

Page 16: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Create a new project (cont.)

• How to create a new project:

• Left click on the Project tab

• Then left click on the New Project tab:

Page 17: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Create a new project (cont.)

• A new window will pop up:

Page 18: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Create a new project (cont.)

• Enter the name of the new project directory (/home/cheung/cs170/TestProj) and click on the Create button:

Page 19: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Create a new project (cont.)

• When BlueJ has successful created an new project, it will show the following window:

Page 20: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Create a new program file

• Suppose we want to create a file that contains the following Java program (given above):

public class Hello

{

public static void main(String[] args)

{

System.out.println("Hello Class"); System.out.println("How is

everyone doing so far ?");

}

}

Page 21: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Create a new program file (cont.)

Notice that the program is called class Hello

This will be important in the creation procedure.

Page 22: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Create a new program file (cont.)

• How to create a Java program file:

• Left click on the New Class button:

Page 23: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Create a new program file (cont.)

• A new window will pop up:

Page 24: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Create a new program file (cont.)

• Type in the name of the "class" (which is Hello) and click OK:

• A new window will pop up:

Page 25: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Create a new program file (cont.)

• Final result:

You can see the new file Hello in the TestProj area.

Page 26: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Create a new program file (cont.)

• To see that BlueJ has created a file, we list the content of the TestProj directory from a terminal window:

The icon named Hello in BlueJ represents the program file Hello.java inside the TestProj directory.

Page 27: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Open a program file for editing

• If you want to edit a program file, do the following:

• Right click on the file icon

• Then left click on the Open Editor button:

Page 28: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Open a program file for editing • If you want to edit a program file, do the following:

• A new window will pop up:

Page 29: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Open a program file for editing

The new window contains the content of the file Hello.java

(To verify, use "cat Hello.java" in a terminal window)

BlueJ has already inserted a few things in the file Hello.java to help you start writing a Java program

Page 30: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Deleting text from a file • How to delete text from a file:

• Highlight the text in BlueJ that you want to delete:

Page 31: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Deleting text from a file (cont.)

• Press the backspace key You can also press the delete key or control-X

• Result:

Page 32: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Inserting text into a file

• Use the scroll bar on the right to find the location in the file where you want to insert text.

• Left click at the insert location

• Then type in the new text.

• Example:

Page 33: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Insert text by copy and paste

• You can insert text from another window into the document in BlueJ by using the copy and paste facility:

1. Highlight any text in a window (e.g., from a webpage) The highlighted text is automatically copied in UNIX

2. (On a Windows-based PC, you need to type control-C to copy)

3. Now click in the BlueJ window at the position where you want to insert the highlighted text

4. Type control-V (for paste)

Page 34: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Replacing some text

• How to replace text:

• Delete the text

• Insert new text

Page 35: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Undo a change

• When you make a edit mistake, you can undo the last change with the undo-command:

• control-Z    

Page 36: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Undo a change (cont.)

• Undo earlier changes:

• You can undo earlier changes by pressing control-Z multiple time

• The maximum number of changes can be undo is 25

Page 37: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Undo an undo

• Suppose you have undone a change that was in fact correct

• You can undo an undo operation using:

• control-Y (this is called a Redo operation)    

Page 38: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Goto a certain line in the file

• A feature that is very useful when you write computer programs is:

That is because compilers (an application that translates a program written in a high level language into machine code) always report an error along with the location (as a line number) in the file.

• Goto a certain line in a file

Page 39: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Goto a certain line in the file (cont.)

• How to go to line number n in a file:

1. Left click on the Tools tab

2. Then left click on the Go to Line tab

Page 40: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Goto a certain line in the file (cont.)

Example:

After this, a window will pop up and you can enter the desired line number

Page 41: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Goto a certain line in the file (cont.)

• Keyboard shortcut:

• The keyboard shortcut for the Go to Line function is control-L

Page 42: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Search for a text pattern

• Finding the next occurrence of a pattern in a file:

1. Left click on the Find tab The lower portion of the BlueJ window will change to the Find menu

Example:

Page 43: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Search for a text pattern (cont.)

Enter the search text pattern and click Next:

The text highlighted in yellow is the next matching pattern

All other matching patterned are highlighted in blue

Page 44: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Search for a text pattern (cont.)

Left click on the Next button to find the subsequent matching pattern

• Search forward:

• Left click on the Prev button to search forward

Page 45: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Search and Replace

• Finding the next occurrence of a text pattern in a file and replace it with some other pattern:

• Left click on the Replace tab

The lower portion of the BlueJ window will change to the Replace menu

Page 46: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Search and Replace (cont.)

Example:

Page 47: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Search and Replace (cont.)

2. Enter the replacement pattern in the Replace field:

Page 48: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Search and Replace (cont.)

3. Click on the Once button to replace the current occurrence (in yellow):

Page 49: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Search and Replace (cont.)

You can replace the next occurrence by clicking on Once another time.

Click on All to replace every occurrence

Page 50: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Search and Replace (cont.)

• Hint:

• If you do not want to replace the current occurrence and want to continue the Search and Replace operation, then do the following:

1. Click on the text immediately after the current occurrence

2. Click Next (to find the next occurrence)

3. Continue with replace if desire

Page 51: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Search and Replace (cont.)

Example:

Page 52: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Search and Replace (cont.)

Click on the text immediately after the current occurrence

Page 53: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Search and Replace (cont.)

Click Next

Continue with the Replace operation if so desired.

Page 54: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Saving the changes

• Auto saving:

• You do not need to save your work.

• When you quit (close) the BlueJ window, it saves your works automatically

Page 55: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Saving the changes (cont.)

• Save your work explicitly:

• You can choose to save your work explicit by clicking of Class and then Save:

Page 56: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Quit without saving your work...

• You do not have this option in BlueJ

Page 57: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Exit BlueJ

• Before you exit BlueJ, I would recommend that you save all your changes explicitly

You have learned saving your work above !!!

Page 58: Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using

Exit BlueJ (cont.)

• Exiting BlueJ:

• To exit BlueJ, click Project in the BlueJ's main window

• and select Quit: