CIS 1073Computer Programming & Higher Level Languages - Kyvernitis

CIS 1073 - LAB 01

Your first java program

using the pico editor, telnet and ftp

to a remote unix host

Contents

USE TELNET TO LOG IN TO lucas (THE REMOTE HOST)

EXECUTE UNIX COMMANDS

RUN THE PICO EDITOR

EXIT PICO (SAVE OR ABANDON CHANGES)

COMPILE YOUR PROGRAM

RUN YOUR PROGRAM

TRY OTHER UNIX COMMANDS

MAKE YOUR OWN PROGRAM, COMPILE, & RUN

FTP YOUR JAVA PROGRAM FROM REMOTE HOST TO YOUR PC

SUBMITTING YOUR HOMEWORK

USE TELNET TO LOG IN TO lucas (THE REMOTE HOST)

Telnet is a protocol that allows a user to log into a remote computer and type in commands as if they were sitting in front of that computer. We will use “SSH Secure Shell Client” to telnet into lucas which is our host computer. Click on Start (lower left of screen) – All Programs – SSH Secure Shell – Secure Shell Client

When you see this window, press the space bar on your keyboard (as indicated at the bottom of the window)

When you see the “Connect to Remote Host” dialog box.

  • Enter “lucas.cis.temple.edu” for Host Name.
  • Enter your class supplied User name (not your TuPortal username)
  • Leave the other boxes as they are
  • Click on the “Connect” button

Enter your (class supplied) password (not your Tuportal password) and click on OK.

When it asks “Are you sure you want to continue connecting (yes/no)?”, type yes and press the enter key. There are various hosts to which you might be connected. In this session I happened to be connected to “ironman”.

EXECUTE UNIX COMMANDS

The operating system on these host computers is called “unix”. Unix does the same kinds of things that Windows does (allows you to run programs and organize your files), but the interface that we use is rather primitive and text based – it does not have a Graphical User Interface like Windows.

Let’s use a couple of unix commands to manage our files. You should create one folder for each lab so that you are able to keep your program files organized.

To see what files and/or folders you already have (probably none so far), type this— <enter> means press the enter key

  • ls<enter>

To make a folder (or directory) for this program (the folder will be called “demo”), type

  • mkdirlab01 <enter>

Now when you type the ls command , you should see the folder you just created (folders show in blue).

  • ls <enter>

To move down into that new folder, type

  • cdlab01 <enter>

Note the unix prompt that tells you the name of the or directory where you currently are positioned (lab01).

RUN THE PICO EDITOR

Now you are ready to create a file that contains our first demo program. You do this by invoking the “pico” editor (an edit program that is available on our unix host computers). You can run pico on an existing file, or you can create a new file – which is what we want to do now. Type pico and then the name of the file you want to create. The name of the file we want to create is CalculateExpenses.java so type this:

  • picoCalculateExpenses.java<enter>

Note that all java programs must have a “.java” extension in their filename.

The pico editor screen looks like this:

Copy the text of your first program (below) into the clipboard (copy from this document, from windows) – do this by selecting the text with the mouse, right clicking, and selecting Copy.

It is NO COINCIDENCE that you named your source code file CalculateExpenses.java and the class inside that file is named CalculateExpenses. In java, your program’s class name must match the file name (plus “.java” at the end).

// CalculateExpenses.java: calculate the average of this month’s and last month’s food expenditures

// Programmer: Sally Kyvernitis

// Date: 8/31/2009

// Bring in code that can get input from the user

import java.util.Scanner;

public class CalculateExpenses

{

public static void main(String[] args)

{

// Declare variables.

doublefoodLastMonth; // how much spent on food last month

double foodThisMonth; // how much spent on food this month

double avgFoodSpent; // average of how much spent on food over past 2 months

// prepare to read user input

Scanner keyboard = new Scanner(System.in);

// prompt the user to enter how much they spent on food last month

System.out.print("How much did you spend of food last month? ");

foodLastMonth = keyboard.nextDouble( );

// prompt the user to enter how much they spent on food this month

System.out.print("How much did you spend on food this month? ");

foodThisMonth = keyboard.nextDouble( );

// calculate total monthly expenses

avgFoodSpent = (foodLastMonth + foodThisMonth) / 2.0;

System.out.println("Your average monthly food expenditure is ");

System.out.println (avgFoodSpent);

}

}

Then, in your SSH window, make sure pico is MAXIMIZED (full screen),then right click anywhere in the white area of the pico window, and select Paste. You should see the program in the editor (see bottom of page). If it looks terrible (badly aligned etc), just ignore that and keep going.

EXIT PICO (SAVE OR ABANDON CHANGES)

To get out of pico, type this (hold the control key down while pressing the X)

  • <control> - X

If you want to save your file, press “Y” in answer to the question “do you want to save?” Otherwise (if you made big mistakes and do not want to save), press “N”.

  • Y

Then pico then prompts you for the name of the file you want to save. Pico assumes that you want to save it with the same name that you opened it with. So, to confirm that you want to keep the same filename, just type

  • <enter>

COMPILE YOUR PROGRAM

Now, you are ready to compile your program.

What to type / What that means / Why you are typing that
ls / Unix command to list all the file and/or folder names (in the current directory) / To make sure that you have your source file and that it is named correctly – it should be named CalculateExpenses.java
javac CalculateExpenses.java / Invoke the java compiler (named “javac”) to translate the source code that you have stored in file “CalculateExpenses.java” / To check for errors and to create the byte code that you need so you can run your program. If it says “nothing”, then that’s good, your program compiled without errors. Otherwise, it shows the statements it doesn’t like and provides an error message.
ls / Unix command to list file and folder names / To see that the compiler created a new file (named CalculateExpenses.class) that holds the byte code translation of your source code.

If you got a lot more output than what was shown in the screen capture above, then probably you have compiler errors (you may have forgotten to maximize pico before pasting in the java source code). To fix this, you could simply delete your source program (the unix command would be rm CalculateExpenses.java) and repeat the previous steps (starting with pico CalculateExpenses.java, maximize the pico window, then paste the code in again).

RUN YOUR PROGRAM

Now, to run your program, type

  • java CalculateExpenses <enter>

and enter the data (your food expenses last month and this month) – as prompted by the program.

TRY OTHER UNIX COMMANDS

Try out these other unix commands until you are comfortable with their use. NOTE:

  • “file1” and “file2” mean whatever file name you want to edit, copy, rename, move.
  • “dirName” means the name of whatever directory you want to make, move to, or delete.

Sample Unix command / What it does
ls / List the names of files and/or directories that are in the current directory
ls -l / Detailed listing of the files and/or directories in the current directory
pico file1 / Run the pico editor on (existing file) “file1”. If a file does not exist with that name, then create one.
cp file1 file2 / create a copy of file1, calling it file2
mv file1 file2 / Means move, but is also a way to RENAME file1 to be called file2
rm file1 / Remove file1
mkdir dirName / Make a directory called dirName
cd dirName / Move from the current directory to the subdirectory called dirName
cd .. / Move from the current directory to the parent directory
rmdir dirName / Remove the subdirectory called dirName
rm –r dirName / Remove subdirectory dirName and all its contents

MAKE YOUR OWN PROGRAM, COMPILE, & RUN

  1. In lecture, you should have written (on paper) your own program. One that asks for your own inputs, performs your own calculations, and displays your own results.
  2. You were asked to think of your own calculation – something other than averaging this last month’s and this month’s food expenses. Use these operations: + to add, – to subtract, * to multiply, / to divide, and ( ) to control order of operations.
  3. Change the class name of the program to be something representative of your calculation (so, not CalculateExpenses as done in the sample program).
  4. Rename the variables so that they are meaningful in the context of your calculation. (So, your variable names must be something other than “foodLastMonth”, “foodLastMonth”, and “avgFoodSpent”.) Remember that your variable names should start with a lower case letter and then use “camel case”. Although this is not absolutely required by the compiler, it is good programming style, so I require it. Once you decide on the name of a variable, you must spell it exactly that way everywhere it is used (capitalization matters).
  5. Make sure to declare all your variables of type double (since we do not run into any issues, for example, with integer division not giving us the results we expect).
  6. Change the prompts for the user input and change the output statement that displays the results.
  7. Don’t forget to modify the comments to indicate:
  • Who the programmer is (you)
  • When you completed the program
  • What calculation your program will be performing
  • What each variable will hold
  • Other comments, as necessary, in the code.
  1. If you want to save yourself some typing, you can start by making a copy of the sample program CalculateExpenses.java and copy it to a name that is appropriate for your own program (the class name that you specified on paper above). Remember that cp is the unix command to copy a file. So, you would type this cp CalculateExpenses.java yourProgramName.java (but substitute your program’s class name)
  2. Edit your program (by typing pico yourProgramName.java, but use your program’s name). Once in pico, remember that the dark blue cursor is what you move around in order to type in or delete characters (not the lighter blue cursor) – and you can only move this by the arrow keys, not the mouse. It takes a little getting used to, but it’s not so bad after that. Make sure that the class name (inside the file) and the file name match exactly (even capitalization matters and must be the same). In the program’s comments, change the author to your name and the date to today’s date. Exit pico, recompile, and rerun to make sure you have not made any mistakes so far. Remember that to compile you type javac yourProgramName.java and to run you type java yourProgramName.
  3. Run your program two times, with two different sets of input, and make sure that it displays the results you expect.

FTP YOUR JAVA PROGRAM FROM REMOTE HOST TO YOUR PC

FTP stands for File Transfer protocol. Using the FTP protocol, two computers are able to send files between each other. We will use the “SSH Secure File Transfer Client” program to ftp your java program from the remote host to your local PC (e.g., save to your desktop or flash drive).

Since you have completed the programmingand testing work of your first lab, now you must prepare your homework submission. To copy your program from the remote host to your local computer, run SSH Secure File Transfer Client as shown below (from Start – Programs).

When the Secure File Transfer Client window comes up, click on File – Connect.

Enter lucas.cis.temple.edu as host name, enter your CIS 1073 assigned username and click on the connect button.

Then enter your class assigned password.

Now you will see the files on your local computer (your C drive, perhaps your flash drive) on the left pane and the files on the remote host on the right pane, like this:

Notice that you open a folder (like windows explorer) by double clicking on it. The folder icon with up arrow lets you go up to the parent folder from where you are.

Double click on the folder where your source code is (on the right side where your remote files are). Find the file that has “.java” as its extension (it should be named yourProgramName.java -- but the name you gave your program), and drag that file either to your computer’s desktop or your flash drive (on the left side).

SUBMITTING YOUR HOMEWORK

  1. Go to the Assignments section of blackboard and clickon the first lab assignment. If you have trouble with blackboard using IE, switch to firefox (or the other way around) – there can be some “browser specific bugs” in blackboard.
  1. You should still have Secure Shell Client Client open (if not, just run it again—log into lucas as before, but don’t worry if you get connected to a different computer – all computers map to the same disk drive where you’ll find your latest files. Open up a blank word document (on your local computer). Run the program you just wrote (on the remote host using SSH Secure Shell Client – as demonstrated below). Use whatever data you like for input. Run your program a second time using different data. Size the SSH window down a little, so that it is easier to read, click on Alt-PrtSc to copy the active window into the clipboard, then paste that into the word document. Save your word document somewhere (maybe your desktop or your flashdrive).
  1. From Blackboard, click on the browse button and select the word document you just created (that holds a screen capture of running your program twice).
  1. Then click on “add another file” and attach your java source code to the blackboard assignment – it’s the file with the “.java” extension that you just dragged to your local computer (perhaps to the desktop or perhaps to your flash drive).

Please note that after you submit your assignment, you will not be able to resubmit again unless you ask the TA or me to clear your submission.

HOWEVER, you can always still see the assignment notes (that I posted originally, that describes the assignment). After you have submitted an assignment, it may look like you can no longer see the assignment handouts, but it just gives you an extra message “that you have already submitted this assignment, click anyway…”.

1

Sally Kyvernitis, Temple University / September 1, 2009