+ All Categories
Home > Documents > OOJ Lab01

OOJ Lab01

Date post: 28-Oct-2014
Category:
Upload: thu-thuong
View: 29 times
Download: 1 times
Share this document with a friend
Popular Tags:
21
OOJ/OJA – Object-Oriented Programming Using Java Laboratory Manual Laboratory 1: Unix To start press any key. Well where's the "any" key? I see Esc, Ctarl (ctrl), and PigUp (pgup). There doesn't seem to be any any key! Phew. All this computer hacking is making me thirsty. I think I'll order a Tab. Oop! No time for that now, the computer's starting. - Homer,The Simpsons (1989) Introduction This lab aims to introduce the Unix operating system, the vi editor and the steps involved in creating your first Java program. Students who are already familiar with Unix and the vi editor are still required to complete this lab; however, it should not take you long. For those students who are new to the Unix environment, it is recommended that you pay particularly close attention to the exercises and concepts covered, as they will be used in all the following labs. Read the explanations provided and try entering commands shown in Shadow Boxes. Complete the tables and ensure you answer all the questions to gain a better understanding of what you have read. Remember, do not hesitate to ask a demonstrator for help if you do not understand or get stuck on something. Objectives To introduce the Unix environment To learn how to use the Unix manual pages to gain help To introduce the vi editor To learn how to compile and run a simple Java program To learn Pine for reading and writing email. Laboratory 1 8
Transcript
Page 1: OOJ Lab01

OOJ/OJA – Object-Oriented Programming Using Java Laboratory Manual

Laboratory 1: Unix

To start press any key. Well where's the "any" key? I see Esc, Ctarl (ctrl), and PigUp (pgup). There doesn't seem to be any any key! Phew. All this computer hacking is making me thirsty. I think I'll order a Tab. Oop! No time for that now, the computer's starting.

- Homer,The Simpsons (1989)

Introduction

This lab aims to introduce the Unix operating system, the vi editor and the steps involved in creating your first Java program.

Students who are already familiar with Unix and the vi editor are still required to complete this lab; however, it should not take you long. For those students who are new to the Unix environment, it is recommended that you pay particularly close attention to the exercises and concepts covered, as they will be used in all the following labs.

Read the explanations provided and try entering commands shown in Shadow Boxes. Complete the tables and ensure you answer all the questions to gain a better understanding of what you have read.

Remember, do not hesitate to ask a demonstrator for help if you do not understand or get stuck on something.

Objectives

To introduce the Unix environment To learn how to use the Unix manual pages to gain help To introduce the vi editor To learn how to compile and run a simple Java program To learn Pine for reading and writing email.

Laboratory 18

Page 2: OOJ Lab01

OOJ/OJA – Object-Oriented Programming Using Java Laboratory Manual

Preliminary Work

Make sure that you have read and understood the initial lectures on Unix and compiling a simple Java program. Also ensure that you have read the preceding pages of this lab book.

Reading: Hanh, H., Student Guide to Unix – Chapters 24, 25 and 26.

a) What is a directory?

b) What is a subdirectory?

c) Given the following directory structure, what are the sub directories of the home directory? Note / is the root directory.

d) If you are in the home directory, what are the absolute and relative pathnames for mum?

Laboratory 19

user

homework

mum dad brother sister

/

Page 3: OOJ Lab01

OOJ/OJA – Object-Oriented Programming Using Java Laboratory Manual

Laboratory Exercises

The Unix Operating System

Start by logging on to your Unix account. (If you have trouble, look in the introduction section of this lab book.)

Once you have logged on, the Unix shell will appear:

The shell is an interface that allows the user to execute different commands. The user types in commands at the command line. Try typing in echo Hello [your name] at the command line and press the Enter Key. The shell will execute the command and display a greeting message to you.

HINT: Using the up and down arrow keys at the command prompt will cycle through previous commands.

Files and Directories

A computer stores all its information in files. These files are kept in a structure called a directory. A directory can hold any number of files and also other directories. Directories are used to group files together which have a common relationship.

Directories and files are arranged in a hierarchical tree structure with the top of the tree known as the root.

Laboratory 110

Page 4: OOJ Lab01

OOJ/OJA – Object-Oriented Programming Using Java Laboratory Manual

Note: When you first log on to your account, the directory you are currently in (current working directory) is your home directory.

Files and directories are referenced using their name. In Unix, both file and directory names are strings of characters with little restrictions on the available characters. You cannot use the '/' symbol as it already refers to a directory. However it is also recommended that you do not use spaces, tabs or any other non-printable characters in your file or directory names as later manipulation becomes difficult.

If at any time you wish to find out what directory you are in, you can type in pwd. Try it now.

What does it display?

Answer:

Listing FilesList the contents of your current directory using the commands below.

> ls> ls -l

The first command (ls) lists all files in your current directory that don't start with a dot (.). The second command lists the same files, but in a long format. Later in the lab you will learn how to find more information on each of the commands covered. Use the command below.

> ls -a

What files does it display that the ls command didn't?

Answer:

Making DirectoriesNow it is time to create your first directory. Try entering the following command and check the result by using the ls command covered previously.

> mkdir OOJ

Create another directory called mistake.

Laboratory 111

root

user

OOJSYS

Lab1 Lab2 Lab12

………..

Page 5: OOJ Lab01

OOJ/OJA – Object-Oriented Programming Using Java Laboratory Manual

Removing DirectoriesIf you wish to delete a directory you can use the following command:

> rmdir [directory name]

Write down how you would remove the directory named mistake.

Answer:

Now remove the directory called mistake from the system.

Changing DirectoriesUnix has the following commands available to navigate through the directory structure.

> cd .. Change to the parent directory.> cd . Change to the current directory (no effect).> cd name Change to the directory called name.> cd / Change to the top-level system directory (root directory).> cd Change to your home directory from anywhere in the system.

Starting in your home directory, type in the commands in the table below and write down the pathname after each command. (Remember to use pwd to display the pathname.)

Command Pathnamecd .cd ..cd /cdcd OOJ

You should now be in your OOJ directory. In this directory, create another directory called lab1 and change into it.

Paths

From your lab1 directory, type the following command:

> ls ~

From which directory are the files that are listed?

Answer:

The ~ character allows you to reference your home directory from anywhere in the system.

Laboratory 112

Page 6: OOJ Lab01

OOJ/OJA – Object-Oriented Programming Using Java Laboratory Manual

What is the pathname of the directory you would be in if you typed in the following command?

> cd ~/..

Answer:

Change back into your home directory.

What is an absolute pathname?

Answer:

What is the absolute path name to your lab1 directory?

Answer:

What is a relative pathname?

Answer:

What is the relative pathname from your home directory to your lab1 directory?

Answer:

Files

To manipulate files we need to be able to copy them, move them, delete them and view their contents.

Copying FilesMake sure you are in your home directory.

Copying of files is done with the cp command; cp is followed by the source file and then the destination. The destination may be a directory or a file. Copy the file lab1.txt from the library directory using the cp command.

Laboratory 113

Page 7: OOJ Lab01

OOJ/OJA – Object-Oriented Programming Using Java Laboratory Manual

> cp /home/1st/csilib/<subject code>/lab1/lab1.txt .

What does the dot at the end mean?

Answer:

Type in the following commands:

> cp lab1.txt lab1copy.txt> ls

What has happened in your directory?

Answer:

Moving FilesThe move command mv copies a file from the source to a given destination and then deletes the source file.

> mv lab1.txt ~/OOJ/lab1.txt> ls

Removing FilesTo remove (delete) a file you can use the rm [filename] command.

Write down the command to remove the lab1copy.txt file.

Answer:

Remove the lab1copy.txt file.

Displaying FilesChange to the directory into which you moved the lab1.txt file.

To display the contents of a text file, we can print to the screen using the concatenate command cat. The cat command displays one or more files to the screen with no breaks in between.

> cat lab1.txt

Laboratory 114

Page 8: OOJ Lab01

OOJ/OJA – Object-Oriented Programming Using Java Laboratory Manual

Sometimes when a file is large, cat displays the information to the screen too fast to read it. We can display the information to the screen one page at a time using the less command.

> less /home/1st/csilib/<subject code>/lab1/large.txt

When in less, you can see how far through the file you are. You can move up and down in the file using u to go up half a page, b to go back a full page, d to go down a half page and f or Enter to go forward a full page. To quit before reaching the end, type q. When viewing multiple files :n goes to the next file, and :p goes to the previous file.

Revision

Provide definitions for the following commands:

Command Definition

cat

cd

cd [directory name]

cd ~

cp

ls

mkdir

mv

pwd

rm

The vi Editor

There are a number of different text editors available for the Unix operating system. For this course we will focus on using one of these, the vi editor. vi is a powerful text editor with a wide range of features. Whilst it may seem difficult to navigate at first, the ease with which vi can edit and manipulate files makes learning it well worth the time and patience required.

There are many interesting variants of vi. The flavour of vi we will be using is called vim (vi iMproved).

There are two main modes in vi: command mode and input (or insertion) mode. When vi is in command mode everything you type is interpreted as commands, while in input mode everything you type is inserted into the buffer. The fastest way to move around the buffer is in command mode, and text can only be added in input mode.

Laboratory 115

Page 9: OOJ Lab01

OOJ/OJA – Object-Oriented Programming Using Java Laboratory Manual

Starting viTo start vi simply type at the Unix prompt:

> vim [filename]

If the file name you supply doesn't exist in the directory in which you are working, then vi will start a new file. However if the file exists then vi will edit the existing file. When you start vi, it will be in command mode initially.

SavingWhen you edit a file in vi, all the data is kept in an editing buffer. Therefore when you make changes to the data, you are making changes to the editing buffer and not the file itself. It is not until you save your data that the changes you made are stored in the file. Therefore if you mess up the editing buffer, you simply choose to discard the editing buffer and keep the original file.

To save the changes you have made, simply type :w in the command mode and press the enter key.

ExitingThe command to quit vi without saving is :q! then the enter key. If you would like to save the work you have done and quit, the command is :wq.

Getting Back to the Command ModeAs mentioned before, you can either be in the command mode or the insertion mode. If you are in the insertion mode (you will learn more about the insert mode shortly) and you wish to get back to the command mode, simply press the Esc key.

Moving AroundWhen you start working with the editor, you will need to be able to move around the text in which you are working.

Positioning the CursorThe arrow keys can be used to move the cursor around the text.

ScrollingScrolling Up

One screen at a time: <ctrl b>Half a screen at a time: <ctrl u>

Scrolling DownOne screen at a time: <ctrl f>Half a screen at a time: <ctrl d>

Jumping to a Particular LineYou are able to jump to a particular line by pressing : then typing in the line number followed by the enter key.

Example: :54 This will take you to line 54.

Moving Within One LinePressing 0 (zero) will take you to the beginning of the line.Pressing $ will take you to the end of the line.

Laboratory 116

Page 10: OOJ Lab01

OOJ/OJA – Object-Oriented Programming Using Java Laboratory Manual

Text Manipulation

vi allows a number of ways to insert text; the two most common methods, insert and append, will be discussed here. When you choose to insert or append, you change vi from the command mode to the insertion mode.

The insert command (i) will allow you to start typing text to the left of the cursor.

The append command (a) will allow you to start typing text after the character the cursor is on.

DeletingIf you make a mistake while in insertion mode, you can simply press the BackSpace key to delete text.

If you are in command mode, the options that follow are available to you.

Deleting a WordPressing d then w (i.e. dw) will delete the word that is forward of the cursor.

Deleting an Entire LineTo delete an entire line, place the cursor on the line you wish to delete and press d twice (i.e. dd).

To Delete a Single CharacterTo delete a single character, place the cursor on the character you wish to delete and press x.

UndoIf you do something to the text by mistake, you can press u to undo the changes that were just made.

There are a lot of other commands that make vim very powerful. One way to learn vim is through vim tutor (at the prompt type learn vi), and there are many resources available on the internet.

Your First Program

Using the commands you have just learnt, start a new vi session with a file called HelloWorld.java.

Now enter the following Java program, then save and exit vi.

public class HelloWorld{ public static void main(String[] args) { System.out.println("Hello World!"); }}

Laboratory 117

Page 11: OOJ Lab01

OOJ/OJA – Object-Oriented Programming Using Java Laboratory Manual

Compiling and Executing your Programs

To compile and run the program you have just written, you need to use the following two commands. The first, javac is used to compile your code; the second, java is used to execute your program. Use these commands to execute your program.

> javac HelloWorld.java> java HelloWorld

HINT: When compiling your program, you need to use the full filename, including the '.java' extension, but when executing the program, only the letters before the extension are required.

If your program contained no errors, your directory should now contain an additional file Hello.class. This file is the result of the compilation process and is then used to execute your program.

Getting Help

In addition to numerous suitable published references, many Unix commands contain electronic manual pages. These manual pages can be viewed by using the man command. Often these pages are quite complex, however they are very thorough.

To use the electronic manual pages to find information on a specific command, use the Unix command man cmnd where cmnd is the command you wish to query. Once viewing the manual pages, you can use the up and down arrow keys to scroll through the information and when you are finished, pressing q will return you to the command prompt.

Try entering the following:

> man ls> man man

When you don't know the name of a command, you can search the subjects of the manual pages using the -k option of the man command.

> man –k date

Other Informational Commands

The cal command displays a calendar for the current month, or another month depending on the arguments we give it. Try the following commands and use the manual pages to confirm what effect they have had.

> cal> cal 2000> cal 3 1999

Laboratory 118

Page 12: OOJ Lab01

OOJ/OJA – Object-Oriented Programming Using Java Laboratory Manual

Another command similar to the cal command is the date command. It is used to display the current time and date.

> date

Using the above commands, answer the following questions:

What day of the week was 19-05-1988 ?

Answer:

Briefly explain what the who command does.

Answer:

Email Contact

During your time at La Trobe University you will be given an email account that you can access via the Web (http://students.latrobe.edu.au). Your email address is of the form shown below where 'yourlogin' is your username.

[email protected]

Students are expected to inspect their account regularly as it is one of the primary ways the lecturers and tutors are able to contact you.

Note that it is the department's policy that all electronic communication with students is done through this university account only.

Email Exercise1. Send an email to yourself.

(Your email address is [email protected]. Send an email message to the person sitting next to you in the laboratory. (You

will need to ask them for their username.)

Laboratory 119


Recommended