Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Hands on Java Lab 1 Exercise Lab 1: Getting Started Introduction The main purpose of this lab is to introduce you to the computing environment at UNE. You will use the ideas in this lab again and again throughout this unit, so you should make every effort to understand not only what, but also why you are doing what you are doing at each step. Each tutorial consists of A Lab Exercise Questions Accompanying the Exercise Files to Accompany the Lab Exercise All students are strongly encouraged to complete the Questions Accompanying the Exercise for every tutorial as part of their revision. It is also good practice for the exam. On-campus students Students who are using the computer labs at UNE, should read the following information regarding starting and ending your computer lab session. The computers in the computer labs should remain on all of the time. For the duration of this course we will be using Linux for all our lab sessions. Connect to turing computer and log on to your UNE account using your UNE username and password. When you are finished, you will need to log out. To do so, select the logout option from the menu panel (i.e., left-click on the "red hat" image found in the menu panel, and select logout). NOTE: Do NOT shut down the computer and/or do NOT turn off the power to the computer. About this Manual The distinction between typing something and entering something is as follows: to type the letter y, simply press the keyboard key marked y. to enter the letter y, press the keyboard key marked y, followed by the key marked Return (it may be marked Carriage Return, CR, or perhaps Enter on your particular machine). Getting Started (for external/internal students) Students who are NOT using our computer labs (i.e., MCL1, MCL2 or MCL3) at UNE, need to access turing (turing is the name of the main computer system located in the Mathematics, Statistics and Computer Science building) via one of the following methods (use your UNE username and password): use the X2Go client program to log into turing (This is the most prefered way for an off-campus Comp131 student to connect to turing) or open an xterm window in their Linux box or terminal program on Mac OSX and then log into turing with the ssh username@turing.une.edu.au OR use putty.exe (a secure shell program for MS-Windows) or some similar program, to log into turing. (Students can find this programs via: the FAQs link of the cosc120 home page) Note: When connecting from a remote location to turing, you may need to specify turing's full address, i.e. turing.une.edu.au to be able to get a connection. For students using MCL1, or MCL2, you need to firstly select the "Linux Terminal" option (to start a Linux session) and then log into your UNE account on turing. The computers in MCL1 and MCL2 are dual booting i.e., they can be booted into Linux or MS-Windows. The computers in MCL3 are Linux only. Getting Started with LINUX To begin this lab you will need to work from within a terminal window, (after you have logged into turing). For X2GO client (Linux/MS-Windows/Mac OSX) users to start a terminal window (after having logged into turing): using the mouse, right-click on the desktop, and then select the "Konsole" (or "Open Terminal") option. A command-line terminal window should now appear. For Linux users (who are not using X2Go), to open an xterm window using the mouse, right-click on the X-Window desktop, and then select the "New Terminal" option. A command-line terminal window should now appear. For MS-Windows users (who are not using X2Go) who are using putty Run the putty program by selecting the putty icon (after having installed the putty.exe on your system) A command-line terminal window should now appear. Below, in Image 1, is an example of what a terminal window would look similar. Image 1. Once you have a terminal window and have logged into your UNE account at turing, you are ready to begin this tutorial. So let's begin. When you see a blinking rectangle (called the cursor) sitting to the right of something like this: turing % or: % or perhaps this: [adams@turing ~]$ (where  adams  is the users' username) or something similar, then your computer is ready for your session to begin. The text being displayed ( i.e., turing % ) is called the system prompt -- a message from your computer's operating system (LINUX) that it is ready for you to enter your first command. (N.B. the prompt is easy for a user to customise; as a result, the same UNIX system may display different prompts for different users.) Any time that you see this prompt, you can enter a command following it. Note: on your terminal the system prompt may be represented by another set of characters e.g.,   turing %   or   [username@turing ~]$   or   . . . (N.B. the prompt is easy for a user to customise; as a result, the same LINUX system may display different prompts for different users.) the blinking (non-blinking) rectangle sitting to the right of the system prompt is called the cursor. When the cursor is a filled-in rectangle (as shown above), the window is said to be active and you can input commands When the cursor is an oultine of a rectangle, that means the window is not active, and won't accept keyboard commands (Note to make the window active, just move the mouse pointer over the window and then press the left mouse button.) Any time that you see the system prompt (and the window is active), you can enter a command following it. Throughout this manual, we will use the character  %   to refer to the system prompt. Interacting with the Computer Next, we will learn to interact with your computer. A LINUX interaction consists of three steps: The computer displays a prompt and waits for you to enter a command. You enter a command. The computer executes (performs) that command (if it is able to), and when it is finished, displays another prompt. At that point you're at step 1 again, ready for another interaction. The commands we'll ask the computer to perform take different amounts of time to complete. Some will take a fraction of a second. Others may take a several minutes. Others will continue to execute indefinitely, until we specifically ask them to stop. The way in which you interact with a computer depends upon the computing environment of its operating system. For example, an environment in which you repeatedly: are prompted for a command, enter a command, and the machine displays the result of the command is called a command-line environment -- an environment in which you interact with the machine by entering commands following the system prompt. When you first turn on (or boot) a LINUX workstation, it typically comes up as a command-line environment, though they can be configured otherwise (see below). The LINUX environment is case-sensitive (i.e., lower-case letters and upper-case letters are distinct), and virtually all LINUX commands use lower-case letters, so your caps-lock key should be off. Another common LINUX environment created at the Massachusetts Institute of Technology (MIT) is the X-window system. The X-window system provides an environment in which you use the mouse to interact with on-screen windows, menus and icons. Such environments are called graphical user interface, or GUI (pronounced "gooey") environments. One of the kinds of windows available in the X-window environment (called an x-term) provides a separate command-line environment. The X-window environment is thus a combination environment, combining the convenience of a GUI with the power of a command-line, making it a favorite of computer scientists around the world. Since both plain LINUX and X-windows require knowledge of command-line commands, we will concentrate on them in this exercise. In order to use a command-line environment, you must learn those commands that the environment "understands." Let's first see what happens if you type something that the environment does not recognise. Enter the following: % qwerty That is, at the system prompt type the word qwerty, followed by the key marked Return or Enter Do NOT type the  %  character -- this represents the system prompt as stated earlier How does the system respond? You should see something similar to; qwerty: Command not found. appear on the screen. If you try other nonsense words, you'll find that the system responds in the same way. It's thus safe to make mistakes and mistype commands -- you won't harm the computer. Directories and Files In this lab, we'll be entering and running a simple Java program. Once we've entered a program, we'd like to be able to save it somewhere "safe" -- somewhere that, if the power goes off, the program can still be retrieved. To save your program safely, the computer stores it on magnetic media (usually a hard disk) in a container called a file. During this unit, we'll create dozens of files, and so some means of organising them is needed. Just as the documents in a filing cabinet are often kept in labelled manila folders, we can create a named container in which to keep related files, called a directory. Linux File system organisation Before we start creating files and directories at will, let's get an understanding of how the Linux file system is structured. Linux uses one of the most common file system organisation structures - a tree-structure. A tree has a main trunk, which branches into subtrunks, which branch again, and so on until the final elements are the leaves themselves. The Linux machine has a root organisation, or directory, which may be subdivided into subdirectories. each of which in turn may be subdivided into sub-subdirectories and so on. The final elements are individual files. The figure below shows a typical tree structure that depicts how directories and files are organised in the linux file system. Tree figure. Note that tree structures are drawn with the root node at the top of the diagram (i.e., take our tree and turn it upside down) The root directory is at the top of our tree. There are no directories above the root directory. The tree extends downwards from the root to the next level which in our figure contains two directories and a file (subdirectory1 and subdirectory2 and file1). That is the root directory contains subdirectory1 and subdirectory2 and file1 within it. From subdirectory1 the tree extends downwards to the next level. In our figure, the next level contains sub-subdirectory1 and file3. file3 is the last element in this branch From the sub-subdirectory1 extends downwards to the next level. In our figure, the next level contains file4 and file5. file5 is the last element in this branch file6 is the last element in this branch From suddirectroy2 our tree extends downwards to file4 i.e., file4 is inside suddirectroy2. file4 is the final element in this branch. file1 is the final element in this branch. It's worth noting at this time that unlike a manila folder that can contain several copies of the same document, a directory in a computer filesystem cannot have more than one file of exactly the same name. For example, from the tree diagram, file5 and file6 cannot have exactly the same filename - one file will overwrite the other. Creating directories on the Linux File system This section was added in the 22/07/2010 (nx-client users need to enter the following command to place you in your home directory, -- initially nx-client places you in your Desktop directory) Let's begin by making sure that we are currently in our home directory. Enter the following at the system prompt (lower-case letters only) % cd (This command will take us to our home directory - more about  cd  latter) Let's create a directory named "projects" Enter the following after the system prompt use lower-case letters only for commands in the command-line e.g., the make-directory mkdir command are all lower-case letters % mkdir projects That is, having logged into turing, type the above command into your terminal window. Remember to type the command as you see it above. That is, all lower-case letters and the command mkdir followed by a space (press the space bar on your keyboard) followed by projects and then press the enter key on your keyboard. The mkdir command is the LINUX command to make-a-directory. By placing the word projects after the mkdir command (seperated by a space), we tell the system that we want to make-a-directory named "projects". Practice by using the mkdir command to make four more directories: one named "practice", (i.e., type the command mkdir practice) one named "labs", one names "assignments", and one named "myLib". Note: the last directory name has an upper case letter "L". Viewing the Contents of a Directory Next, let's learn how to look at what's inside of a directory. At the system prompt, enter the following: % ls The ls command stands for list the contents of a directory. If all is well, you should see (at least) four things listed: the four directories you just created! projects labs myLib practice Note: You may also see one or two directories that already existed in your account, for example the directory public_html and Desktop. For the purpose of this tutorial, we will ignore directories and files that we have not created ourselves. Many people like to visualise the relationships of these directories as depicted in Figure 1: Figure 1. where: Ovals are used to represent directories (with the directory name inside them) $HOME represents our home directory $HOME  an environment variable that is used by Linux to define the home directory for a user i.e.,  $HOME refers to your home directory it represents the location on the filesystem where LINUX places us at the beginning of a session i.e., when we first log into the system. as there can be many different users, the value of $HOME is different for each individual user (more about $HOME later) assignments, labs, myLib and practice are directories that exist inside the home directory - as indicated by the arrows and the fact that they are below the home directory Note: the fact that the diagram does not extend any further indicates that the assignments, labs, myLib and practice directories are empty. The ls command can also be used to find out what is in a particular directory. Try entering: ls labs If you follow the ls command with the name of a directory, it displays the contents of that directory. Since each of the directores assignments, labs, myLib and practice are empty (i.e., they do not contain directories or files within them), there is nothing for ls to display. To get a more detailed listing of the current directory, enter the following command at the system prompt: % ls -l The  - l  is called a switch that causes the  ls  command to generate a long (i.e., detailed) listing. The LINUX On-line Manual Detailed information about all of the switches that can be used with a given LINUX command (such as ls) is available in the LINUX on-line manual. The name of the command to search this manual is man. For example, if you enter % man ls the manual entry for ls will be displayed, providing exhaustive information about that command, including all of its switches, the meaning of the long listing information, and other information. (NOTE: within a man page, you can skip to the next page by pressing the SPACE bar. While in the man (manual) pages, you can return to the system prompt at any time by pressing the  q  key.) Don't be discouraged if much of the manual does not yet make sense, reading the LINUX manual is an acquired skill that requires much practice, and you will become more accomplished at it over time. (NOTE: the details of many commands can also be viewed with the info command. In particular, info often provides more extensive documentation than man.) Removing a Directory Since we made the directory practice just for practice, let's get rid of it (i.e., erase it). At the system prompt, enter: % rmdir practice The rmdir command is the remove directory command, so this changes our diagram (that depicts our directory structure) from that shown in Figure 1 to that shown in Figure 2: Figure 2. Whatever directory you specify following the command will be removed from the current directory, provided that the directory being removed is empty. To remove a non-empty directory named DirectoryName, the following command: % rm -r DirectoryName can be used (but this should only be used when DirectoryName contains nothing important). NOTE: the -r switch (recursive) means remove everything stored in the directory named DirectoryName including other sub-directories and files stored with them. Use this command with care because once you remove a file or directory, you will not be able to recover it. Using the ls command, verify that the practice directory no longer exists. That is, at the system prompt, type the  ls  command - the output should be similar to: projects labs myLib Identifying the Working Directory When you begin a computing session, UNIX always starts you out in the same place -- a special directory called your home directory. As we shall see shortly, you need not remain in your home directory, but can "change location" to a different directory. The directory in which you are working at any given time is called your working directory. Your home directory never changes, but your working directory can. To find out the name of the working directory, you can use the pwd command (which stands for path to the working directory). For the rest of this tutorial, I will use the fictitious username of adams to demonstrate some of the commands we will be using. At the system prompt, type the following command: % pwd For someone who's UNE username is adams, the output (after entering the pwd command) should look like this: /home/adams Try it for yourself and note what is displayed. You should see something like: /home/YOUR_USERNAME Where YOUR_USERNAME is substituted by what your UNE username is. As stated previously, the Linux file system is organised as a tree-structure. This tree structure utilises what is called a hierarchical directory system, meaning that directories can contain other directories, and that there is one root directory named   /   (slash or forward-slash) that contains all of the other directories in the system. At the system prompt, type the following command: % ls -al / That is, at the system prompt type: the command ls followed by a space (press the space bar on your keyboard) followed by  -al  and then followed by a space followed by a   /   (forward-slash character) and then press the enter key on your keyboard. to list the contents of your system's (turing's) root directory. Within / (i.e., the root directory) there are a number of subdirectories, including usr sys bin home .automount and so on, each of which contains other directories and/or files. On the turing (Linux/UNIX) system, the users' (students') home directories are stored within one of these subdirectories, specifically the directory named .automount Note the '.' (dot) character before the word automount. In the Linux file system, directory (as well as file) names that begin with the dot character are normally invisible to the ls command. That is why we needed to include the -a option (this option/switch allows the ls command to list invisible directories and files). If for example, my username was adams, my home directory would be located somewhere further down the hierarchy starting at the home directory. Figure 3 depicts a part of the hierarchy of turings' file system, and includes the absolute path from the top (root) of the hirerarchy, all the way down to our fictious user adams: (NOTE:john, mary and kate are depicted in Figure 3 to represent other fictious users) Figure 3. Note: Your home directory is different from the directory named home that is depicted in Figure 3. Now, if you compare Figure 3 to the previously mentioned output from the pwd command, ie., /home/adams you can see that pwd begins by displaying: /  (starts with a slash character i.e., the root directory) followed by the directory  home then followed by a  /  (slash) character (Linux/UNIX unfortunately uses the  /  (slash) character both for the name of the root directory and to separate directory names) then finally by the directory named adams That is, pwd lists the sequence of directories (which is called the path), between the root directory and my working directory with slashes between the directory names. Since my working directory is currently my home directory, the last name that pwd displayed happens to be my home directory (in my case adams). If we use shading to indicate the working directory, then we can visualise the directory structure and the fact that the working directory is adams as shown in Figure 4. Figure 4. Apply this to the output from your pwd command. What is the name of your home directory? A last word on the home directory: UNIX treats the tilde character (~) as an abbreviation for your home directory, which can save you a lot of typing if the path to your home directory has lots of characters. Changing to a Different Directory If we created all of our files in our home directory, it would soon become cluttered. By grouping related files within a directory, one's files can be kept more organised. Thus, we created labs in which we'll store the files for our lab exercises, and projects in which you can store your programming projects files. Since we are working on a lab exercise, let's change the working directory to directory labs. At the system prompt, enter: % cd labs The cd command is the change directory command that changes the working directory to the directory whose name follows the command. Next, use the pwd command and note its output: How is it different than it was before? The cd command changes our location within the directory hierarchy, effectively changing the working directory. The effect is to change the preceding picture (Figure 4) to Figure 5. Figure 5. Since pwd outputs the path (from the root directory) to the working directory, the output of pwd changes each time you cd to a different directory. To see everything in a directory, enter ls -a The -a switch tells ls to list all of the contents of a directory. The output should look something like this: . .. You will notice two odd items in the output of ls -a command: a period (.) and a double-period (..) In the UNIX file system, A single period  .  is another name for the working directory (whatever directory you are currently in); A double period  . .   is another name for the parent of the working directory (i.e., the directory above the working directory) For example: We created labs within our home directory. The cd labs command took us from that directory; "down" into the labs directory. We can get a listing of the current directory by typing the command: % ls -a How do we get a listing of our home directory from the labs directory? - i.e., How do we get a listing of our home directory from the directory we are currently in? Enter the command: % ls .. This command will now lists the contents of the parent directory of the directory we are currently in. Our current directory is labs, so the parent directory is our home directory. We should now see a listing of our home directory. How do we get from labs back "up" to the directory where we began? Enter the command: % cd .. This command moves us "up" one directory because  . .  is always a synonym for the parent of the working directory, regardless of its actual name. We've thus changed the working directory back to what it was previously as shown in Figure 4 (that is, our working directory is now our home directory). Note We could have issed the command: % cd instead of % cd .. and achieved the same thing. The reason being, we were only one level down from our home directory - in the directory labs, so typing the command: % cd .. has the effect of taking us up one directory level - in this case our home directory. While typing the command % cd has the effect of taking us back to our home directory, no matter where we are. Use pwd to verify that you are currently "in" your home directory. Next, we will create the directory L1 inside our labs directory. Do the following: Change directory back "down" into  labs % cd labs Create a new directory (inside labs) named  L1 % mkdir L1 Use ls to verify that  L1  was created successfully % ls Change directory from  labs  "down" into L1. % cd L1 Use the  pwd  command to check that your working directory really is L1 Use the pwd command to check that your working directory really is L1. Enter the following command at the system prompt: pwd The output should look something like: /home/YOUR_USERNAME/labs/L1 Where YOUR_USERNAME is substituted by your specific UNE account username. The above output can be graphically represented by Figure 6, with the L1 directory being the working directory as depicted by the shaded L1 oval. Figure 6. Although knowing your absolute path from the root directory to your current working directory (as shown in Figure 6) is useful , most of the time you will be working only within your home directory and the various subdirectories contained within it. Figure 6a depicts a more compact way to refer and visualise your filesystem by using the $HOME environment variable instead of the full path from the root directory. Figure 6a. Linux by default sets up many environment variables for you. $HOME is automatically set when you log into the system and is different for each individual user. To see what your $HOME variable has been set to, use the  echo   command as follows: % echo $HOME You should see something like the following output: /home/YOUR_USERNAME To wrap up this section, let's apply some of the concepts we have just learned: Now that we are in the L1 directory, 1st Question: How can we list the contents of our home directory from here? (that is from inside the L1 directory) There are several options available to us: We could use the absolute path: We could use the absolute path starting from the root directory, i.e., enter the command: % ls /home/YOUR_USERNAME Another option is to use tilde (~) character i.e., use the absolute path starting from our home directory % ls ~ As previously mentioned, UNIX treats the  ~  (tilde) character as an abbreviation for your home directory. Yet another option is to use the UNIX $HOME variable % ls $HOME As previously mentioned $HOME is where Unix/Linux places us after we log into the system, i.e., our home directory. We could also use relative paths: For example we could enter the command: % ls ../.. NOTE how the two sets of  . .  characters are seperated by a  /  (slash) character - that is, list the contents of the directory two levels up from our current directory. 2nd Question: How can we list the contents of our labs directory from here (i.e., from the labs directory)? There are several options available to us: We could use the absolute path: We could use the absolute path from the root directory, i.e., enter the command: % ls /home/YOUR_USERNAME/labs Another option is to use tilde (~) character, i.e., use the absolute path starting from our home directory % ls ~/labs - that is , list the contents of our the directory labs that is in our home directory. Yet another option is to use the UNIX $HOME variable i.e., again use the absolute path starting from our home directory % ls $HOME/labs We could also use relative paths: For example we could enter the command: % ls .. - that is, list the contents of the directory one levels up from our current directory. Things to come In the sections to come, we will create a text file in the newly created L1 directory. Editing Files So far we have been working with directories. Yet we also need to know how to create and edit files containing text (i.e., characters). You may be familiar with word processors to create documents. Along with alphanumeric characters, these documents generally contain all sorts of additional (often hidden) characters for formatting purposes. Java programs must be written as plain text files. To avoid the problems of using a word processor, we will use another tool, a text editor, for creating plain text files. The text editor used throughout this manual is named emacs. It is a freely available text editor that is a collaborative effort of a number of companies and universities. GNU Emacs is one component of the GNU/Linux operating system, and is the one we will use in this tutorial. GNU Emacs can be downloaded from http://ftp.gnu.org/pub/gnu/emacs/, or from a GNU mirror. (GNU stands for “GNU's Not Unix”). GNU Emacs is an extensible, customizable text editor—and more NOTE: There are other text editors are available on turing such as: bluefish, kate, kwrite and others - but for the purpose of programming in Java, emacs is a good choice (and the one that will be used in this tutorial) Creating a Text File Once you are "in" the directory L1 (i.e., L1 is your current directory), you are ready to create your first text file. This sample file will simply contain a few lines of description. We can use emacs to create this file by entering: % emacs MyDetails.txt & Students who are using putty.exe in MS-Windows instead of the X-window environment, will need to omit the ampersand (&) character from the end of the previous command. That is, at the system prompt type the command: emacs MyDetails.txt Note that when emacs starts up via X2Go Client, it displays two buffers (as shown in Image 1 below). The text you are editing in Emacs resides in an object called a "buffer". Each time you visit a file, a buffer is created to hold the file's text. At any time, one and only one buffer is "selected". It is also called the "current buffer". Often we say that a command operates on "the buffer" as if there were only one; but really this means that the command operates on the selected buffer (most commands do). When Emacs has multiple windows, each window has a chosen buffer which is displayed there, but at any time only one of the windows is selected and its chosen buffer is the selected buffer. In this case the buffer containing MyDetails.txt is selected as is shown by the solid cursor inside it. Each window's mode line displays the name of the buffer that the window is displaying Since the file MyDetails.txt does not exist in the working directory, emacs will create a new, blank editing window (called a buffer) named MyDetails.txt into which you can type a program. If a file named MyDetails.txt existed in the working directory, then emacs would create a blank buffer, open that existing file and read the file into the buffer. Image 1 below shows the emacs program started with a blank editing buffer on top, and the second buffer below contains some links to emacs tutorials and manuals ... Image 1. Note that emacs displays the name of the buffer status bar near the bottom of each buffer, and that the name of a buffer is the name of the file it is displaying. Editing MyDetails.txt Within the MyDetails.txt buffer, enter (and personalise) the following: Name: Janet Whorfin Location: Back of Bourke Hobbies: Swimming, volleyball and eigth-dimensional physics Quote: "It is only with the heart that one can see rightly; what is essential is invisible to the eye." If you are working from a terminal or via ssh, you will have to type in this text; but if you are using the X-window environment, you can instead copy-and-paste the text above from your browser's window into the emacs window. Let's learn how to do this next. In the X-window system, a block of text can be selected by dragging (holding down the left mouse button while moving the mouse) from the beginning to the end of the desired text. Most X environments support a copy-paste short-cut: drag the mouse over the text you want to copy, selecting it; position the cursor where you want to paste by pointing the mouse there and clicking the left mouse button; and paste by pressing the middle mouse button (or both buttons for a two button mouse). Image 2 below shows the emacs program with the copied text Image 2. If your environment does not support this short-cut, selecting the text in the browser, then click the right-mouse button - a menu should appear providing cut, copy and paste menu choices. Using one of these methods, copy the description shown above into the emacs MyDetails.txt buffer. Personalising the Text As given above, MyDetails.txt contains some information about a fictitious person. Edit the text to reflect your details and interests. If you are using emacs in the X-windows environment, you can use the mouse to position the cursor at an arbitrary point by pointing at that point and clicking the left mouse button. Emacs commands generally involve the CONTROL key (sometimes labeled CTRL or CTL) and/or the META key (sometimes labeled Esc). Rather than write that in full each time, we'll use the following abbreviations: C -  means hold the CONTROL key while typing the character Thus, C - f  would be: hold the CONTROL key and type f M -  means hold the META key (usually the Esc key) down while typing If there is no META, EDIT or ALT key, instead press and release the ESC key and then type . We write for the ESC key. The editor command: C-d (typing the Control and d keys simultaneously) can be used to delete the character beneath the cursor, and the command: C-k can be used to delete from the cursor to the end of the line. If you are running emacs outside of the X-windows environment, you can move the cursor by the editor commands: C-p (previous line) to move up one line, C-n (next line) to move down one line, C-b (back) to move left one character, and C-f (forward) to move right one character. Alternatively, you can use the arrow keys on your keyboard to move the cursor. If you mistype something, it can be erased using the delete (or backspace) key. If you make a mistake, you can always undo the effects of any emacs command, either: by using the undo command: C-x u (i.e., press the Control and x keys simultaneously and then release them, at the left hand bottom of the Xemacs window you should see the characters    C-x - then type the u key by itself), or by using the Undo button on the emacs menu bar. The undo command can be used to undo the effects of an emacs command that has completed. However some emacs commands take a significant length of time, and you may wish to get out of the command while it is still being performed. For this, you can use the get-out command: C-g which we find to be useful in a wide variety of situations. Saving Your Work. When the MyDetails.txt buffer contains the MyDetails.txt text, you can store this program in a file by entering the editor save files command: C-x s (hold down the Control and x keys simultaneously, (and then release them both); then type the  s  key separately). emacs will ask you to confirm the operation (in the mini-buffer at the bottom of the window): Save file .../labs/L1/MyDetails.txt? (y, n, !, ., q or C-h) To save the contents of the buffer in a file named MyDetails.txt, answer 'yes' by typing the letter  y  Save file .../labs/L1/MyDetails.tx? (y, n, !, ., q or C-h) y emacs  will then confirm the save operation by displaying the Wrote file ... message shown above. This can be graphically represented by Figure 7, with the L1 directory being the working directory as depicted by the shaded L1 oval. the rectangle labelled  MyDetails.txt  represents the file you have just created and saved. Figure 7. Miscellaneous Other Useful Commands For the remainder of the exercise, we will examine some other useful commands. Quitting emacs To quit emacs (as opposed to suspending it), you can type the editor quit command: C-x C-c (Cntrl and x, followed by Cntrl and c). Copying a File It is sometimes useful to be able to make a copy of a file, so that we can modify a copy without modifying the original. To make a copy of MyDetails.txt to a different file named MyDetails.2, enter the following command at the system prompt: cp MyDetails.txt MyDetails.2 The cp command makes a copy of the file with the first name (i.e., MyDetails.txt), and names the copy the second name (i.e., MyDetails.2). Use the ls command to verify that cp command worked. Graphically this can be represented by Figure 8, with the L1 directory being the working directory as depicted by the shaded L1 oval. the rectangle labelled  MyDetails.txt  represents the first file you created and saved. the rectangle labelled  MyDetails.2  represents the file you just copied. Figure 8. It's more often the case that we need to make a copy of a file in a directory different from the one in which the file currently resides. How are we to do this? The key is to learn a bit about how files and directories are named. Both of the names MyDetails.txt and MyDetails.2 are the relative names of these files. To use these names, we must be in the same directory as they are. However, every file or directory also has an absolute name: a name that starts with the root directory (i.e., /), and lists each directory between there and the file name, just as we saw in the output from pwd. For example, the absolute name of MyDetails.txt found on a student whoose username is xxxx is as follows: /home/xxxx/labs/L1/MyDetails.txt or more briefly ~/labs/L1/MyDetails.txt To use a file's (or directory's) absolute name, you need not be in the same directory as that file! For example, try entering: mkdir ~/projects/P1 What did we just do? We used the mkdir command to make a new directory, but where did we make it? Recall that earlier in this exercise, we made three new directories (we also made the fourth practice but we erased it earlier) within our home directory: labs, projects and myLib. Since ~ (the tilde character) is another name for our home directory, we just made a new directory inside projects, called P1, using an absolute path name. Graphically this can be represented by Figure 9, with the L1 directory being the working directory as depicted by the shaded L1 oval. the rectangle labelled  MyDetails.txt  represents the first file you created and saved. the rectangle labelled  MyDetails.2  represents the file you copied. the oval labelled P1 represents the directory you just created inside the directory projects Figure 9. Almost all of the LINUX file manipulation commands accept either relative or absolute file names. For example, you don't need to be "in" a directory to see its contents: Enter ls ~/projects and you should see the directory you just made. Similarly, to put a copy of MyDetails.txt in that new directory, you can enter: cp MyDetails.txt ~/projects/P1 and a copy of MyDetails.txt will be stored in that directory. The cp command thus behaves differently, depending on whether you specify a file or a directory as the second name: if you specify a file as the second name, then the cp command will store the copy in a file with that name. if you specify a directory as the second name, then cp will place a copy in that directory with the name as the original. Using ls and an absolute path name, verify that there is a copy of MyDetails.txt in ~/projects/P1. Graphically this can be represented by Figure 10, with the L1 directory being the working directory as depicted by the shaded L1 oval. the rectangle labelled  MyDetails.txt  represents the first file you created and saved. the rectangle labelled  MyDetails.2  represents the file you copied. the oval labelled P1 represents the directory created inside the directory projects the rectangle labelled  MyDetails.txt  shown below the oval labelled P1, represents the file you copied. Figure 10. Renaming a File Sometimes we decide after the fact that a file would be better off with a different name. To change the name of MyDetails.2 to MyDetails.old, enter: mv MyDetails.2 MyDetails.old Graphically this can be represented by Figure 11, with the rectangle that was labelled  MyDetails.2  now is labelled  MyDetails.old  Figure 11. The mv command changes the name of (or moves) the name of the file on the left to the name on the right. It can be used with both relative and absolute file names, and can be used to rename directories. Copying from other user accounts In general, you do not have access to other accounts on a LINUX system. However, it is possible to set access permissions so that some directories are accessible. In particular, the account for comp131 has some portions that are publicly accessible. As more practice, let's copy a file from the comp131 account to your account. We are going to copy a file named linexRef.txt linuxRef.txt is inside a directory named files the files directory is inside another directory named Lab_01 the Lab_01 directory is inside another directory named Tutorials Tutorials directory is inside the comp131 directory You can enter: cp ~comp131/Tutorials/Lab_01/files/linuxRef.txt . Notice that: the  ~  prefaces the comp131 account. Here, too, it serves as a short-hand way of specifying the 'home directory of'. the full stop (.) is a short hand way of saying "Copy the source file to the current directory and give the copy the same name as the source file." (HINT:You can use the Tab key to auto-complete words as you are typing them in.) That is, type: cp ~comp131/Tu at this point, press the TAB key located on your keyboard. Notice that the directory name Tutorials is completed for you, and a forward-slash character is also added. So that the following now appears: cp ~comp131/Tutorials/ now type in the uppercase letter L thus: cp ~comp131/Tutorials/L and then press the TAB key again. Notice that auto-complete only completes upto the underscore ( _ ) character as shown below: cp ~comp131/Tutorials/Lab_ This is because there are other directory names that begin with the characters "Lab_" (i.e., for example Lab_02, Lab_03 ... etc). At this point you need to type in the "01" ( i.e., the number zero followed by the number one) manually cp ~comp131/Tutorials/Lab_01 and again press the TAB key. Now the auto-complete will add a forward-slash ( / ) character. cp ~comp131/Tutorials/Lab_01/ Now type the letter f and press the TAB key, the following should now appear: cp ~comp131/Tutorials/Lab_01/files/ Type the letter l and press the TAB key, the following should now appear: cp ~comp131/Tutorials/Lab_01/files/linuxRef.txt finally add a space character (press the space key on your keyboard) followed by a period character ( . ) as shown below: cp ~comp131/Tutorials/Lab_01/files/linuxRef.txt . And finally press the enter key. Check that the copy operation worked by listing the contents of your directory (i.e., use the ls command). Graphically this can be represented by Figure 12, with the rectangle labelled  linuxRef.txt  represents the file that you just copied from the comp131 account. Figure 12. NOTE: The only place you have permission to create, modify or delete files is in your own directory space. If you are having difficulty copying files, check that you have correctly specified the name (and location) of the source and that the destination is your directory. In particular, you are only able to copy (rather than move) linuxRef.txt from the ~comp131 directory to your directory. Viewing a file's contents Display the contents of the file linuxRef.txt one page at a time using the command less linuxRef.txt To scroll up and down a file viewed with less, use the following keys: j Scroll down one line SPACE Scroll down a page at a time y Scroll up one line b Scroll up a page at a time q Quit Edit linuxRef.txt to include the command less. Our First Java program Traditionally, when learning a new computer language, the first program to write is one that outputs the text message Hello World!. So lets do that. (Note Linux is case sensitive so only use capital letters in the Hello World! part of the listing below). Use emacs to copy the following lines of text to a file named HelloWorld.java (i.e., at the system prompt, type the command: emacs HelloWorld.java & and enter the following lines of text into your emacs window public class HelloWorld { public static void main(String[] args) { String message = "Hello World"; System.out.println(message); } } It should look something like this: emacs image of HelloWorld program. After you have finished entering the lines above, save your file (i.e., save the file HelloWorld.java). Check that the  HelloWorld.java  does exist in your current directory, by using the  ls  command. Next, at the system prompt, type the following command: % javac HelloWorld.java and then press the enter key. This command will translate (compile) your  HelloWorld.java source file and generate a byte code file named  HelloWorld.class  (Use the  ls  command to check that both files exist) Now lets run our  HelloWorld  program. To do this, at the system prompt type: % java HelloWorld If all went well, the message: Hello World! should now appear on your computer screen. For external students NOT attending the tutorials at UNE For you to print a document to your local printer, you may have to transfer the file you wish to print from your UNE account on turing to your computer, then print the file to your printer. For Linux users, you can use the  gftp  program scp (or something similar), to transfer the file from your UNE account to your local computer (or vice-versa) MS-Windows can use the Wsftp program, to transfer the file from your UNE account to your local computer Note: ftp stands for "file transfer protocol" You can find installation instructions for this program in the FAQ page Practice Submitting Assignments All of your programming assignments will need to be submitted electronically from your UNE account to comp131. All programming assignment submissions for Comp131 must be made to comp131 using the submit program (email attachments and other methods will not be accepted). So lets practice using the submit program to submit one of the files you have created. This will be a practice assignment submission that will not be marked. We will be submitting two files we have just created, to comp131 as assignment prac (prac stands for practise). Firstly, our current directory should be the same directory where the files MyDetails.txt and HelloWorld.java are located. Next, at the system prompt, type: submit You will now be prompted for several inputs, the first one being: Unit name for submission: The unit name is comp131(even if you are doing comp530), so type it in Unit name for submission: comp131 and then press the enter key. The display should now look like this: **************************************************************************** Current assignments for comp131: prac on p1 off p2 off p3 off p4 off p5 off Assignment: **************************************************************************** Submit your assignment as assignment prac That is, type the word prac - as shown below **************************************************************************** Current assignments for comp131: prac on p1 off p2 off p3 off p4 off p5 off Assignment:prac **************************************************************************** and then press the enter key). You will now be presented with a list of the following options: **************************************************************************** Assignment Submission Procedure Unit : comp131 Assignment: prac 1 Record (script) your assignment at work and add to submission list 2 Add files to submission list 3 Send final submission (Overwrites previous submission) 4 View previous submission 5 Help 6 Quit 7 View plagiarism declaration NOTE: By selecting option 3 you will be deemed, in effect, to have signed the Faculty of the Sciences plagiarism declaration form. Enter selection: **************************************************************************** What we want to do is submit the files  MyDetails.txt  and  HelloWorld.java  as our  prac  assignment. To do this we need to: Add your file (MyDetails.txt) and HelloWorld.java to the submission list. Select option 2 from the submit menu. Enter  MyDetails.txt  as the "File to submit", then press the enter key Enter  HelloWorld.java  as the "File to submit", then press the enter key Enter a blank (i.e., just press the enter key) when there are no more files to be submitted. The submit program should now echo something like: You are ready to submit the following files: MyDetails.txt HelloWorld.java [Press return to continue] Press the enter key. You should now be back to the list of options Send the final submission. (Select option 3 from the submit menu.) You will now be prompted for your tutorial group with the following display. WARNING: No script of sesssion submitted The tutorial groups of comp131 are: ext int Enter your tutorial group: (If you are an external student, your tutorial group is ext, and int if you are an internal student.) Enter the tutorial group you belong to, i.e., enter int or ext ext and then press the Enter key. The following (or something close to it) should now be visible. -rw-r----- xxxxx/ug 415 YYYY-MM-DD 15:24:35 submit.YYYYMMDD -rw------- xxxxx/ug 90 YYYY-MM-DD 12:26:18 HelloWorld.java -rw------- xxxxx/ug 90 YYYY-MM-DD 12:26:18 MyDetails.txt Your files have been submitted to comp131, prac for grading. A record of this session has been left in submit.YYYYMMDD Submission Complete Press the Enter key to return to the submit options. Quit the submit program. (Select option 6 from the submit menu.) Your system prompt should now be visible again. List the contents of your directory (using the ls command). There should be a new file named submit.YYMMDD. This file was generated by the submit program when you sent your submission. NOTE: The YYMMDD is substitued by the date that the submission was made. Look at the contents of this file (you can use the emacs program or the less command) and see what it contains. Did you successfully submit your files? Cleaning Up Your Directory The various files you have created take up valuable disk space, and so we will remove all of the files from L1 except for our initial text file MyDetails.txt. To remove MyDetails.old, enter rm MyDetails.old The  rm  command removes (or deletes) whatever file you specify. Note that it does not ask you for confirmation! (If you want such confirmation, call rm with the -i switch.) Earlier we saw that when rm is invoked with the -r switch, it recursively removes a directory and everything within it. Use the rm command to remove the file MyDetails.txt~ (a back-up file created by emacs). Then use the ls command to verify that all of the files except MyDetails.txt are gone. If there are other files remaining in your L1 directory that you wish to delete, use the rm command to remove them, as well. You can also remove any files from the directory ~/projects/P1 or for that matter remove the  projects  directory and all directories and files stored under it, by typing the following command at the system prompt: rm -r ~/projects Will delete the direcory  projects  (that is located in your home directory) as well as all the files inside it. rm -r ~/myLib Will delete the direcory  myLib  that was created earlier. (the next command will delete the  practice  directory rm -r ~/practice Do  NOT  erase the  labs  directory - you will need it for the next Tutorial Mail As much of the correspondance from comp131 to you will be made via email, you need to be able to read your UNE email. The simplest way to check your mail at UNE, you can use your web browser (Netscape,Mozilla, Internet Explorer, or ...) to read and send mail from your computer account. To check your mail at UNE, you can use your web browser (FireFox, Netscape, Mozilla, Internet Explorer, or ...) to read and send mail from your computer account. Goto the university home page located at URL: http://www.une.edu.au/ Then select the "Current Students" link located at the top of the page and then scroll down the page and select "Webmail" link and then enter your username/password combination. -- THE ABC'S OF UNIX A is for awk, which runs like a snail, and B is for biff, which reads all your mail. C is for cc, as hackers recall, while D is for dd, the command that does all. E is for emacs, which rebinds your keys, and F is for fsck, which rebuilds your trees. G is for grep, a clever detective, while H is for halt, which may seem defective. I is for indent, which rarely amuses, and J is for join, which nobody uses. K is for kill, which makes you the boss, while L is for lex, which is missing from DOS. M is for more, from which less was begot, and N is for nice, which it really is not. O is for od, which prints out things nice, while P is for passwd, which reads in strings twice. Q is for quota, a Berkeley-type fable, and R is for ranlib, for sorting ar table. S is for spell, which attempts to belittle, while T is for true, which does very little. U is for uniq, which is used after sort, and V is for vi, which is hard to abort. W is for whoami, which tells you your name, while X is, well, X, of dubious fame. Y is for yes, which makes an impression, and Z is for zcat, which handles compression. Note: If your head feels ready to explode, don't panic! This first lab covers a great deal of material, that you will use over and over again, and as you do so, you will begin to naturally memorise those commands that you use most frequently. You can speed up the process by reviewing each of the steps you took in this exercise. Until such a time as you remember the various commands, make use of the quick reference page you've edited for LINUX commands. Here is a quick reference page that lists some of the commonly used emacs commands. Here is a quick reference page that lists some of the commonly used linux commands. If you want, print a hard copy of this and store it in a convenient place. Remember to clean up your file space, deleting any unnecessary files, before logging out. To learn to use these quick references, I would recommend that you review today's exercise, and find each of the commands we used in the exercise on the appropriate quick reference sheet. Then study the description of that command on the quick reference sheet, so that you associate the two in the future. Phrases you should now understand: Environment, Command-Line, Directory, File, Editor, Hard Copy, Printer. Back to This Lab's Home Page Back to the Labs Table of Contents Back to COMP131 Copyright 1998 by Joel C. Adams. All rights reserved. Modified 2014 to suit COMP131 at UNE.