Lab 2
For this assignment, we will write a simple Java program in the DrJava
environment. You should look at the example program
JavaExample94.java that was written in lecture on 9/4.
This program shows the basics of input, calculations and output
which you will need to do for this program.
Input in Java
There a many different ways in Java to get input (in fact I seem
to keep finding new ways that this can be done). One of the simplest
way is to use the
Scanner class. The Scanner class resides in the "java.util" library
and requires a program to use the following import statement to use it:
import java.util.*;
To use the Scanner class to read from the keyboard, we need to create
a variable with an instance of the Scanner class that connects to
"System.in" (the keyboard input stream). This can be done with the
following statement:
// set up a Scanner variable to read from the keyboard
Scanner sc = new Scanner (System.in);
To read an integer value from the keyboard, we now would use the
following statement:
number = sc.nextInt();
Output in Java
To output to the screen, we use the method System.out.println().
Between the parenthesis, we place the information that we wish to
display. To display a string of values (i.e. words), we place the
words in double quotes. Such as:
System.out.println ("Hello. Hope you have a good day.");
To print out a number or the value of a variable, we place that
information between the parenethesis. For example:
System.out.println (57);
System.out.println (number);
We can print both some text and a value by placing both of these in
between the parenthesis and separating them with a + sign, such as:
System.out.println ("The result of the operation is " + number);
Note, there also exists a method System.out.print(). This method
will have the next printed item begin on the same line as this one ends.
The System.out.println() always has the next printed item begin
at the beginning of the next line.
Also note that to print a blank
line in the output (i.e. to skip a line), use System.out.println()
with nothing between the parathesis.
Lab Assignment 2
Due: Thursday 9/11/2008 by 11:59 pm (i.e. midnight)
The Secret of 42
You are to write a program that will always produce the result of 42.
This is not really a useful thing, except you can maybe cause someone
to think that you can read their mind. This type of trick is sometimes
called a calculator problem. The idea was that the person was to be
given a calculator, told to do pick some number they wished to use,
and told to perform various operations using that number. At some
point in time the person was told what number would be on the
calculator. Thus trying to make the person think that their mind
had just been read.
- The first thing the program is to do is to print out a message
something like: "I can read you mind and make you preform a set of
operations that will always result in the number of 42 no matter
what number you start with."
- Now, ask the user to type in any 4 digit number except 0000, and
read this number from the keyboard.
- Now tell the user that we want to make an 8 digit number that contains
the 4 digit number listed twice (i.e. if the user enter 1234, we want to
create the number 12341234). Tell you use you will help them create this
8 digit number by multiplying the original number by 10001 and then adding
the original number.
- Create the 8 digit number and show the user the 8 digit number so we know that it was modified correctly.
- Now tell the user that this number is divisible by 137 and they
are to perform that division. Again tell the user that you will help them
by performing the operation. Show them the result of the division.
- Now tell the user that this new number is the divisible by the original
4 digit number. Again tell the user that you will help them
by performing the operation. Show them the result of the division.
- Now tell the user you have one more operation to perform. You
will need to subtract 31 from this new number and you willarrive at the
predicted result of 42. Once more, tell the user that you will help them
by performing the operation. Show them the result of the subtraction.
- Ask the user if they are impressed with "my mind-reading abilities"
and then quit the program.
Open the key to this is that result from step 6 is always 73 no matter
what 4 digit number they start with. This can be proven by a fairly
simple mathimatical proof that you coul figure out if you wish to.
Note the program only has one value it needs to read in from the user
(this is done in step 2). The other steps are with just print statements
(steps 1 and 8) or print statments with some calculations (steps
3 through 7). Be sure to display the various results as we work
our way through the problem.
Submission of the Lab
The lab must be submitted electronically to the Digital Drop Box inside
of Blackboard. Obviously, this needs to be to the Digital Drop Box
for the CS 101 course. You will only need to submit the java source code
file.
Comments on the ACCC Labs
On the computers in the ACCC Labs there should an H: drive.
This drive is actually a networked connection to your own file space
maintained by the ACCC. No matter what machine you use or what lab
you are in, the H: drive will access the same file space. This means
that you can save a file on the H: drive on a computer in one lab and
access that same file on a computer in another lab. This can be very
helpful. It is suggested that you store your Java program files
on your H: drive.