Week 02: Lab 1
Objectives
- We will learn how a Java program is compiled and executed (“run”)
- We will learn how a program can be passed data before execution
- We will learn about data types and simple operations which can be performed on data depending on their type
Exercise One
Download the free book How to Think Like a Computer Scientist, Java™ Version by Allen B.
Downey, and do the Exercise 1.7 at the end of Chapter One, p.11-12: you have to
type in (really, type it in, do not paste and copy!) the “Hello World” program
(like it’s presented in the lectures or in the Downey’s book, p. 7). Compile
(javac Hello.java
) and run (java Hello
) it. Then follow the advice of
breaking the initial code (in Ex 1.3, a—i), to make small changes, and try to
compile the program again. Read and think through the meaning of the compile
messages.
Exercise Two
Write a program that takes two integers and prints out their sum, difference, product and average.
Change the values of these integers by editing your program. Then make the program read the values of integers from a user as two command-line arguments (like we did in the lectures). The program execution on the command-line will, therefore, looks like this:
% java MyProgram 3 5
8
-2
15
4
You should remember, that the command-line argument’s type is the String
class, and
before they can be used as integers (which is the intended goal here), these
strings must be converted to integer types (the int
type is one of the primitive
types used to represent integer numbers). By peeking (only so slightly) ahead
of the course material, use the following “transformation” trick (which is the
standard way to convert a value of string type to the integer it is meant to
represent; see more details in Hortsmann’s book Ch. 1.5.4, or the
Converting Between Numbers and Strings in
Java Tutorial):
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
BTW, can you (do research if necessary) modify this program to make it print a well-formatted output, similar to this one?
% java MyProgram 10 2
10 + 2 = 12
10 - 2 = 8
10 * 2 = 20
(10 + 2)/2 = 6
(Hint: find out how the method printf()
can be used; this method is a useful
alternative to the more simpler ones print()
and println()
. See the
Java API documentation for the PrintStream class.)
Exercise Three
-
Write a program that calculates the number of characters in a string which the user types in.
-
Write a program to remove the first character from a string which the user types in.
-
Write a program to remove the last character from a string which the user types in.
Note You will make your life simpler if you reuse a previous program to do the next task which may be similar (like in the Exercise Two). Copy an existing source file (on the Terminal this is done by the copy command:
% cp OldClass.java NewClass.java
and appropriate editing of the NewClass.java
.)
Organising your workspace
It make a lot of sense to organise your home directory for the COMP6700 (or
COMP2140) studies. Given the content of our activities (check the labs and
deliverables in the Schedule), you can create a dedicated
directory comp6700
(or, comp2140
) in your Home Directory, with the following structure:
comp6700
/ | \
/ | \
/ | \
assignments hw labs
/ \ | / \
ass1 ass2 ... lab1 ...
This can be done by merely using the window manager (on Linux, this application is called Files; launch it the same way you’ve launched Terminal or Atom), or alternatively (and more in the spirit of programming) by using the command-line tools like mkdir (again, refer to the Basic Command-Line Commands guide, and/or ask your tutor to demonstrate).