CS100J / Fall
2000
Programming
Assignment 1
Due: Tuesday, September 5
Goals
This
assignment has two parts that will help you become familiar with Java, Matlab,
and the
CodeWarrior (CW) programming environment. You do not need to know how to program to do the
assignment. You must complete both parts to receive full credit.
Resources
The following resources contain answers to questions
you may have in doing this assignment:
Course Materials:
http://www.cs.cornell.edu/Courses/cs100j/2000FA/material.htm
Facilities:
http://www.cs.cornell.edu/Courses/cs100j/2000FA/facilities.htm
Projects:
http://www.cs.cornell.edu/Courses/cs100j/2000FA/projects.htm
Guide to CodeWarrior Java
for CS100:
http://www.cs.cornell.edu/Courses/cs100j/2000FA/CodeWarrior/cwguide/cwguide.html
Online Help
from inside CodeWarrior (if Help is
installed):
Help |
CodeWarrior
Help | IDE | Create new projects |
stationery
Motivation
Take any positive integer and do the following: if
the integer is even, divide it by 2; otherwise multiply it by 3 and add 1.
Repeat the process as long as the value remains greater than 1. For example,
suppose you start with 11. Then the sequence you get is: 11, 34, 17, 52, 26, 13,
40, 20, 10, 5, 16, 8, 4, 2, 1.
It has long been conjectured that regardless of the
starting point, you will always reach 1, but so far, no one has ever been able
to prove this. Like Fermat's Last Theorem, which stumped researchers for many
years, this deceptively simple statement is hard to prove or disprove.
In this assignment, you are given a Java program to
enter and run that computes such sequences.
The program can be used to experiment with these mysterious sequences.
We also show you how to plot the sequences using Matlab.
If
you find an integer for which the attached program runs forever (and you
haven't made a typo in entering the program), you have found a counter-example
to the conjecture and will become
famous. Call the New York Times.
Part 1: Java
Do the following tasks for this part:
1.
Familiarize
yourself with the pages and guides listed above under Resources. Refer to them,
as necessary, in doing the assignment.
2.
Start
CodeWarrior, create a new project, and open the CUCS Java Application stationery file. (If you don't know how to do
this, or don't understand what you are being asked to do, repeat step 1.)
3.
Edit
the contents of the CUCS Java
Application stationery so that it exactly matches the program on the
attached sheet. One way you can do this
is to erase the unnecessary text from the stationery, and type the additional
text given on the attached sheet. Another way you can do it is to erase the
entire text of the stationery, and type the entire text given on the attached
sheet. Reminder: Carefully maintain the EXACT punctuation and capitalization --
Java is case-sensitive! However, the spacing and indentation matter only for
style and do not need to perfectly match. Enter you name, ID, and section. If you have a partner, do the same for your partner. While entering program text, it is a good
idea to save your work on disk frequently in case you inadvertently erase
everything.
4.
Select
Project|Compile. Does a window
pop-up warning you of compiler errors, also known as syntax errors? If so, carefully
review the statements inside your code since, most likely, you typed something
wrong.
5.
When
finished correcting your typing-errors, select Run. Enter the integer 10 and confirm that your program generates
the output:
5
after dividing by 2
8
after dividing by 2
4
after dividing by 2
2
after dividing by 2
1
after dividing by 2
Done!
6.
Modify
the program so that it produces output for the odd cases as well as the even
cases. For instance, for the input 10, the modified program should produce the
output:
5
after dividing by 2
16
after multiplying by 3 and adding 1
8
after dividing by 2
4
after dividing by 2
2
after dividing by 2
1
after dividing by 2
Done!
Hint: Refer to the code that generates output for
the even case. In Java, the next one
statement following the else keyword
is executed when the if-condition is
false. If you wish to execute several statements when the if-condition is false, you must enclose the list of statements in
curly braces {}, just as in the even case.
7.
Test
your code again by compiling and running it. Be sure to fix any bugs you may
have introduced.
8.
Run
the program with each of the following inputs.
For each input, try to understand what happens and why:
0 |
i.e.,
the number zero |
-19 |
i.e.,
a negative number |
four |
i.e.,
text |
10,11,12 |
i.e.,
a list of integers |
4/2 |
i.e.,
an arithmetic expression |
12345678900 |
i.e.,
an integer too big to store in an int variable |
1234567890 |
i.e.,
a large integer that is not too big to store in an int variable |
123456789 |
i.e.,
a large integer |
"Large" integers might run a "really
long time." To stop your program on a PC, click on the close-box (the icon
with the letter X inside) and select End
Task in the dialog box that pops up. To stop the program on a Mac, press Option-Command-Escape, as explained in
the CW guide.
9.
Run
your modified code for integer input of 11. Print the modified program. Print
the output for running your program with the input of 11.
10.
Remember
to save your program to your own floppy disk if you are using a public machine.
Part 2: Matlab
This
part will very briefly introduce some aspects of Matlab by plotting data you
computed in
Part
1 of this assignment. Matlab provides a very convenient and powerful software
tool for data
analysis,
which typically involves plots of data. Plotting your data from Part 1 will
demonstrate
how
the algorithm you programmed converges upon the integer 1. Note: Pressing Enter
means
pressing either the Enter or Return key.
1.
Consult
your tip sheets and run Matlab.
2.
You
should see a command window pop-up. This is where you will enter commands.
Note: Do NOT type the prompt >>. Enter commands after the >>.
3.
Enter
the following command statements to generate a plot of your test case from Part
1, Problem 9.
>>
plot([11 34 17 52 26 13 40 20 10 5 16 8 4 2 1])
This
command creates the initial plot of your data. The data is plotted on the
vertical (Y) axis. Press the Enter key after typing the input. A graphics
window containing your plot should pop-up.
>>
xlabel('count')
This command labels the horizontal (X) axis
with text. Press Enter after typing.
>>
ylabel('k')
This command labels the Y-axis. Press Enter
after typing.
>>
title('HW1: <type your name(s) here>')
This command titles your plot with text. You
should enter something like title(`HW1: Pat Smith'). Press Enter after typing.
Matlab will automatically "update"
(modify) your plot each time you press Enter.
4.
Print
out your plot.
What to Hand In
Staple the following sheets together:
·
Printout
of the final Java program, signed by you (and your partner).
·
Printout
of the output from running the Java program with input of 11.
·
Printout
of the Matlab plot.
Grading
See
http://eweb.cs.cornell.edu/Courses/cs100j/2000FA/projects.htm
for explanations of due-date and grading policies.
// Name: Partner's
Name:
// ID: Partner's
ID:
// Section: Partner's Section:
// Date:
// Project:
CS100J/Fall2000 P1
/* Input any
integer and repeatedly divide it by 2 if even and
* multiply it by 3 and add 1 if odd. Stop on
reaching 1 or less.
*/
import
java.io.*;
public class
CUCSApplication {
public static void main(String args[])
{
int k;
String temp;
// initialize Text object in to read
from standard input.
TokenReader in = new
TokenReader(System.in);
// Read and write some numbers and
strings
System.out.println("Please enter
an integer: ");
// Read integer
k = in.readInt();
// Perform algorithm
while ( k > 1 )
if ( (k % 2 ) == 0) { // test if k is even
k = k / 2;
System.out.println(k + " after dividing by 2");
}
else
k = 3 * k + 1;
System.out.println("Done!");
}
}