Instructions:
Answer the following questions one at a time. After answering each question,
check your answer (by clicking on the check-mark icon if it is available)
before proceeding to the next question.
Getting Ready:
Before going any further, you should:
to an appropriate directory/folder. (In most browsers/OSs, the
easiest way to do this is by right-clicking/control-clicking on
each of the links above.)
1. Conventions:
Throughout this lab, the following typographic conventions are used:
[Enter] refers to the "Enter" key.
[Tab] refers to the "Tab" key.
[Down Arrow], [Up Arrow], etc... refer to the
down arrow key, up arrow key, etc...
[Ctrl] refers to the "control" key and [Ctrl]+k
means that you should press the "control" key and the "k" key at the
same time.
The phrase "Enter the command xyz" means that you should type xyz
in the terminal/command-shell window and then press [Enter].
2. Working with Directories:
This part of the lab will help you learn how to
work with directories/folders.
Start a terminal/command-shell window. (For example,
click on Start, pull up to Run...,
enter cmd.exe in the text field, and click on
OK..)
Enter the command cd
What output was generated?
The current working directory which, in this case, is your home
directory. (From here on in, this directory will be denoted by
home.)
Enter the command mkdir cs
Enter the command cd cs
Enter the command cd
(Note: When executed with a parameter, cd is used to change
the working directory. When executed without a parameter it is used
to display the name of the working directory.)
What output was generated?
home\cs
Enter the command mkdir labs
Enter the command cd labs
How can you determine the current working directory?
It may be included in the prompt, but you can always use the cd
command. (Note: You can change the CMD prompt using the prompt
command. For example, the command
prompt $P$S will change the prompt to the current working
directory in followed by a space.)
3. Compiling and Executing Java Applications:
This part of the lab will help you learn how to compile and
execute Java applications (a.k.a., programs) in a command shell.
Change the working directory to the directory that contains the
files you downloaded.
Enter the command javac Tripler.java to compile
the source code.
What file do you know was created?
Tripler.class
Enter the command java Tripler to execute the
byte code. Enter 10 in response to the prompt.
What was output?
10 tripled is 30
4. Redirecting Input:
It can be tedious to test programs that accept input from a user
because you often have to run them many times, typing the user's
responses each time. One can, instead, tell the program to read from a
file rather than the console. This is called input redirection.
This part of the lab will help you understand input re-direction.
Enter the command type test1.inp to see the contents of the
file named test1.inp. (More on this command later.)
What is in the file?
It has a single line
10
Enter the command java Tripler < test1.inp.
Given the title of this section, what do you think the < does?
It tells Tripler to read from the file
named test1.inp rather than from the console.
What was output?
Enter your number: 10 tripled is 30
Why aren't the two parts of the output on different lines?
Because the first call to print() does not include
a \n character.
Why were the two parts of the output on different lines when the user
typed the response (rather than it bein redirected)?
Because the user pressed the Enter key.
5. Executing a Java Application Multiple Times:
Especially when testing, it is sometimes necessary to run the
same Java application multiple times with different command-line
parameters. This part of the helps you do that.
Start jGRASP.
Click on File-New-Plain Text.
Copy the following lines into the newly created file:
java Tripler < test1.inp
java Tripler < test2.inp
and not that each of this lines is a valid CMD command.
Save the file as tests.bat in the directory
that contains Tripler.class
In the CMD shell, enter the command tests
What happened?
The program Tripler was executed twice.
How could one use this when testing code?
It is a convenient way to run multiple tests without having to type the
input each time.
6. Redirecting Output:
Sometimes the output from a program can be long and confusing. In
such situations, one can tell the program to redirect the output to
a file rather than the console. This is called output redirection.
This part of the lab will help you understand output re-direction.
Enter the command java Tripler > test1.out.
Type 10 and press Enter.
Enter the command type test1.out.
What was displayed?
The contents of the file test1.out, which is:
Enter your number: 10 tripled is 30
Why wasn't the prompt displayed on the console?
Because all output was re-directed to the file.
How could one use this to test your programs?
If one had a file with the expected output then one could use a
file comparison utility (like Meld or Kdiff3) to compare the actual
output and the expected output for differences.
For practice, use a file comparison utility (like Meld or Kdiff3) to compare
test1.exp (the expected correct output)
and test1.out.
For more practice,
Change Tripler.java so that it no longer produces the correct
output, run the program again (again redirecting the output to a file),
and compare this output with the expected output.
Note that, because of the file names, it is fairly easy to overwrite an
existing file when redirecting output. Hence, you may want to use the
chmod command to make the .in and
.exp files read-only. For example,
chmod a-w *.in will remove write access on all files
that end with .in for all users.
7. Reducing the Amount of Typing:
This part of the lab will help you learn how to work more efficiently
and reduce the amount of typing required to accomplish
many tasks.
Enter the command nkdir skills_cmd (Note: This is
nkdir not mkdir!)
What output was generated?
'nkdir' is not recognized as an internal or external command,
operable program or batch file.
Press the [Up Arrow] key.
What happened?
The previous command was re-typed.
Press the [Home] key
What happened?
The cursor moved to the first character on the line.
Press the [End] key.
What happened?
The cursor moved to the end of the line.
Press [Home] to go back to the first character.
Press [CDelete]
What happened?
The first character was deleted.
Type m (which will insert an m as the first character).
Press [Enter]
Change the current working directory to your root directory
by entering the command pushd \.
Enter the command cd
What output was generated?
The name of your root directory.
You now need to go back to the skills_cmd directory.
Save yourself some work by entering the command popd
(which takes you back to the directory you were in when you changed
the directory with pushd).
What is the full path of your current working directory?
home\cs\labs\skills_cmd
Move up one level in the directory hierarchy by entering the command
cd ..
Enter the command cd
What output was generated?
home\cs\labs
You now need to go back to the skills_cmd directory.
Save yourself some work by typing cd [Tab]
(which tries to "complete" the command).
What happened?
The command changed to cd skills_cmd
Press [Enter] to change to the skills_cmd
directory.
Enter the command mkdir temporary
Press [Enter].
Enter the command cd (to see the name of the
current working directory).
Enter the command dir (to list all of the files
in this current working directory).
You now need to remove the temporary directory using
the rmdir command. Press [Up Arrow]
until the mkdir command appears.
Press [Home] to go to the first letter of the command.
Press [Insert] to toggle from insert mode to overstrike mode.
Type rm to replace the letters m and k.
Press [Enter].
8. Working with Files:
This part of the lab will help you learn how to work with files
more efficiently.
Change to your home directory.
Enter the command dir
What happened?
All of the files in the home directory were listed.
Enter the command dir /w
What happened?
All of the files in the home directory were listed in a "wide" format.
Enter the command dir /W > filenames-short.txt (which
will redirect the output of the dir command to a file named
filenames-short.txt)
Enter the command dir > filenames-long.txt
Enter the command dir /S > filenames-all.txt (where
the /S option instructs the dir command to
look in all subdirectories)
Enter the command dir filenames* (where the * is called the
wildcard character).
What happened?
All of the files that begin with "filenames" were listed.
Move filenames-long.txt to the directory for this lab
by entering the command
ren filenames-long.txt cs\labs\skills_cmd
How could you have used the [Tab] key to
avoid having to type the file names?
You could have pressed [Tab] twice.
Do the same thing with filenames-all.txt (Hint: Use
[Up Arrow].
To make sure you have moved the right files, enter the command
dir filenames*
To delete/remove the remaining file, enter the command
del filenames-short.txt
Quickly/effciently move to the skills_cmd directory
by typing cd [Tab] followed by a backslash and
another [Tab]
9. Getting Help:
This part of the lab will help you learn how to get help
in the CMD shell.
Get the "help pages" for the dir command by
entering the command
help dir.
What does the cls command do? (Hint: Use the "help
pages".)
It clears the screen.
What does the type command do?
It displays the contents of a text file.
What does the find command do?
Searches files for strings.
What does the copy command do?
It copies files.
What does the more command do?
It displays the contents of a text file one page at a time.
10. Looking At and Inside Of Files:
This part of the lab will help you look at the contents of files
and search for strings within files.
Show the contents of the filenames-all.txt
file by entering the command
type filenames-all.txt. (Note: Make sure you are in the
correct directory.)
Search for all occurrences of the string "2013" in all .txt
files in the current working directory by entering the command
find "2013" *.txt
11. Using the GUI Too:
This part of the lab will give you a convenient skill.
Insert a thumb drive (if you have not already done so).
Type cd in the command shell window.
Click and drag the icon for the thumb drive into the command shell window.
What happened?
The OS completed the cd command with the name of the
thumb drive.
Note: This part of the lab allows you to "go further" on this material. It is neither required nor for extra credit. It will, however, help you gain a deeper understanding of the material. 12. Other Shells:
Learn more about CMD by entering the command help
to get a list of all commands.
Learn about the BASH shell used in Unix/Linux/OS X
and how it differs from CMD
at the course help pages.
Are you using MS Windows but would like to have a BASH shell?
Install MinGW
and use MSYS. Follow the instructions in the section on
the "Graphical User Interface Installer".
(You might as well install some other
programming languages while you are at it, like, Ada, C++, Fortran, abd
Objective-C. You probably won't use them now but you may use them
later.)