Lab 2
CSC120: Programming I - Spring 2001
Due Date: Tuesday February 6, midnight
Solutions
UNIX Tips of the Week
When it comes to organization, I recommend that everyone keep
their assignments in different directories under the
directory classes.
For example, the files for Lab 2 problems should be stored
in a directory called: ~/classes/lab2
How does one create these directories?
To create a directory called foo use the mkdir
command.
Warning be sure that your current working directory
is where you want the directory to be created.
Let us look at how you can create the lab 2 directory.
% cd
% mkdir classes
% cd classes
% mkdir lab2
% cd lab2
Now, let us look at each line.
One at a time.
First line: Every person has a home directory.
To reach home you use cd (change directory)
with no arguments.
Second line: From this directory, we create (make) the
directory classes.
Third line: Here we change to the directory classes that
we just created.
Fourth line: Now, we create the subdirectory lab2.
Notice that this will be under the directory classes since we cd'd
to this location in the previous line.
Fifth line: Here we change to the new directory lab2.
Verify:
If you type in the command: pwd it should print the
directory: /lee/home/ugrad/XXXX120/classes/lab2
Second tip. Command history
The shell program bash stores your current command history.
So, if you typed in the following commands to compile and run your program:
% javac HelloWorld.java
% java HelloWorld
% <--- shell waiting for next command
Typing in an up arrow at the next shell prompt (after the program is
finished) would go to your previous command (java) and another up arrow
would go to the one before that (javac).
You can then edit this command to make any changes or simply hit enter
and execute the command again.
Just a tip
GNOME Tip of the Week
GNOME is the windows desktop environment you are running.
It is VERY configurable.
But how do you go about configuring the environment.
The easiest one is via the GNOME Control Center.
Select the toolbox icon in your menu bar at the bottom.
From there you can change features like Background, ScreenSaver,
and Theme among many others.
Given the chance, try it out.
It is good to have an environment that you like.
Submission Policy
The submission instructions are on a separate page located
here
The extension for this assignment is 2.
Therefore, you should run the following command to submit
the first program:
% submit 2 WhoAreYou.java
Part One: Class WhoAreYou
You should do this part before the end of lab! To make
sure submitting works and the input class works.
The first program is a practice in using the Class SavitchIn
and in submitting programs.
Your assignment is to ask the user for their name (String) and then
their age (int) and then outputs the information back in the shown
manner.
For example, here is a sample run of the program:
% java WhoAreYou
Hello, who are you? Christian Duncan
How old are you? 58
Wow, Christian Duncan, you are 58 years old.
Part Two: Class BasicGrader
Write a program which calculates your grade for the class.
All values input should be of type double.
Ask the user for their percentage grade in assignments.
Ask the user for their percentage grade in quizzes.
Then their midterm and finally their final.
The output is their grade as a percent.
The grade is calculated as follows:
grade = 0.5 * assignment + 0.1 * quiz + 0.15 * midterm + 0.25 * final
Here is a sample run:
% java BasicGrader
Welcome to the grader program.
What was the grade for assignments? 90
What was the grade for quizzes? 100
What was the grade for the midterm? 90
What was the grade for the final? 92
The final grade was 91.5%.
Part Three: Class ToDigit
Similar to problem 3 from Chapter 2.
Write a program that reads in a four digit number (like 1998)
adds 1 to that number and then outputs the number one digit per line.
The difference between this and the book assignment is
that the number must be read in as an integer and not a string.
You can assume that adding 1 still makes the number four digits,
i.e., number < 9999.
Hint: Use the % and / operators to get the digits
Here is a sample run:
Please enter a four digit number: 2001
Here are the digits for 2002.
2
0
0
2
Part Four: Class SentenceFlip
Note, this is problem 5 from Chapter 2.
Write a program that will read a line of text as input
and then output the line with the first word moved to the end
of the line.
Here is a sample run:
% java SentenceFlip
Enter a line of text. No punctuation please.
I like programming
I have rephrased that line to read:
Like programming I
Assume that there is no space before the first word and
that the end of the first word is indicated by a blank.
You may assume that no punctuation is given as input.
This assignment tests the ability to use and manipulate strings.
Last modified: Fri Feb 16 18:30:14 EST 2001