CS W1001 Introduction to Computer Science

Homework 4 (12 points)

Implementing Algorithms and Meta-Algorithms with Java



Due Date:

Thurday, April 6.

For this assignment, hardcopies (only) of the first Part are required, and both hardcopies and electronic submissions of the second and third Parts are required. The course web page provides directions for electronic submission. Hardcopies are also due in class.

Reading:

"Java: Software Solutions" by Lewis and Loftus. Mostly just the first two chapters on programming, specifically Chapter 2: Sections 2.1, 2.2 and 2.3; Chapter 3: Sections 3.1, 3.2, 3.4 (but ignore operator precedence), 3.5, 3.6, 3.7 and 3.8; Chapter 6: pages 208 - 215 (top of 215 only).

These are introductory chapters from a text on programming with Java. Your familiarity with pseudo-code will make this reading easier for you than it is for the complete beginner. Also, keep in mind that you are not responsible for memorizing all the technical details behind programming in Java -- our goal this semester is to use Java simply to explore the basic concepts of implementing algorithms on a computer. See Section II below for the list of programming concepts are you responsible to learn.

This is a good text for Java programming and has been used for W1007 (the intro course for computer science majors). You may opt to purchase it if you are interested in continuing Java programming after this semester (Papyrus may have copies for $62 -- please send me an email to let me know if they run out). Or, you can purchase copies of just those sections of the book (perhaps later you'll decide to purchase the whole thing) from COPYQUICK at 1211 Amsterdam near 120th street. It is open 9-7 Mon-Thurs, until 5 Fri and 4 Sat, and closed Sunday. When you go, mention 1001 so they know what you need. It should be about $7.00, they told me. Share a copy with a friend to save the environment...

Note: 3/24/00 COPYQUICK lost the copy, they just told me, so at this point they won't have anything until Monday March 27.

While you are at COPYQUICK you can pick up some of the reading for homework number 5, unless you plan on buying or borrowing "Godel, Escher, Bach: An Eternal Golden Braid" by Hofstadter (I hear Papyrus can order it in a few days). From this book, you will read the "Little Harmonic Labyrinth" story, and the beginning of chapter five on recursion (just the first 4 pages). This would cost about $2.50 at COPYQUICK. But I think you'd enjoy the whole book - it is really cool: math, computer science, art and music.

Photos of each of these readings contain the entire table of contents of the books to show you the context of your portion, and to show off how great the entire book is in case you are considering purchasing it. You'll also see a couple things that I crossed off, indicating that you are not responsible for the material.

Further reading responsibilities includes two little on-line glossaries that you can get to from the course web page -- one is on computer system concepts, and the other on computer programming concepts. Make sure you know and understand all the terms in these glossaries.

In addition to the programming reading above, there is some abstract material from our text, "An Invitation to Computer Science". Read Sections 3.1, 3.2, 3.4, 3.5.3. Finally, put on your math hat and read Section 3.6 on intractable problems that absolutely explode exponentially; we can design algorithms to solve these problems, but they just take too darn long! As you'll see, this applies to a sales person doing a circuit of USA cities, as well as a moving person trying to shove boxes into trucks. In fact, the only way to solve these problems is to pray for divine intervention, and even this method has not been proven to work every time. So read up and understand the limits of computers and their underlying algorithms, both computers of today, and future computers of the year 10,000.

Finally, read Section 3.7 for a summary of Chapers 1, 2 and 3.

Part I: Theoretical Questions on Complexity (4 points)

"Complexity" is another way to say "efficiency" (of algorithms). Show your work or explain your answer to each of the following. Note that these questions cover the material from the "Invitation" reading, above. The material for Part II below has been covered in class - the material for this Part is coming; you may want to do Part II first.

Part II: Implementing Algorithms with Java (6 points)

The following are the programming concepts you'll want to learn from the reading above:

Here are the algorithms you should implement. Put each one into its own source code file, and give the file a meaningful name. Also, be sure to make the class name in the code match the file name.

  1. Find the second-to-largest value in a list of numbers. Hint: start with the FindLargest.java code and modify it. Further hint: the following is the pseudo-code for second-to-largest:
    get as input n, the number of user inputs
    get as input user_input number 0 up to user_input number n
    set largest to -9999;
    set secondlargest to -9999;
    set i to 0;
    while i is less than num_entries do the following: {
        if user_input number i is greater than largest {
            set secondlargest to largest;
    	set largest to user_input number i;
        }
        else if user_input number i is greater than secondlargest {
            set secondlargest to user_input number i;
        }
        increase the value of i by 1;    
    }
    output the value of secondlargest;
  2. Read one integer value and print the sum of all even integers between 2 and the input value, inclusively. Print an error message if the input value is less than 2. You can start with the program to sum all integers (both even and odd) called ~es66/W1001/src/Sum.java
  3. Output an inputed list of numbers in reverse order. Get the input like it does in FindLargest.java.
  4. Output the even numbers (only) of an inputed list of numbers. Get the input like it does in FindLargest.java.
  5. Read one integer value, n, and output an error message if n is not positive, otherwise output n factorial ("n!"), which is n * (n-1) * (n-2) * ... * 1. This is similar to Sum.java, except you are keeping a running product during the while-loop, not a running sum.
  6. Print out "I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that I know that you know that algorithms are very powerful, but very limited." Don't bother counting how many times it happens -- just loop it about 40 times. I didn't count it, and neither will your TA!

    CONSTRAINTS: For this assignment your program must be shorter than what it outputs, and can only use System.out.println once, at the end of the program, not inside a loop. How can you do that? Well, you must use string concatonation with the + operator. This is like Sum.java, but instead of keeping a running arithmetic sum, you have a running String variable that keeps getting increased in size with the +. See the end of YouLookGood.java for the example of string concatonation shown in class.

    Warning: do not name this or any java source code file "String.java".

Part III: Meta-programming Stinky (2 points)

For this assignment you will write Java code that creates Stinky code! Stinky code is just a string, so it will be similar to the last assignment you did above that builds up a big string. The code itself is about the same difficulty and twice the magnitude as that last assignment above.

Specifically, you will make a loop that creates a Stinky program that draws a zigzag across the window.

First, copy Stinky.java and Situation.java from ~es66/W1001/src/stinky/ by typing

cp ~es66/W1001/src/stinky/*.java .
(This copies all files that end with "java" -- the asterisk is a wildcard.)

Next, try it out as it is by typing

javac *.java
to compile both source code files -- ignore:
Note: Stinky.java uses a deprecated API.  Recompile with "-deprecation" for details.

Next, type

java Stinky
to execute it.

This will bring up a new window with a little Stinky doodle in it.... NOT -- First you have to get your computer set up correctly so it can open new windows. At this point in the assignment I must digress for a moment to tell you how to do so.

For those having a problem with opening a stinky window on a Sun computer, do it from an xterm window, not a telnet window -- that should work. For the Mac, it should work.

Also, type the following at the dollar sign: " export LD_LIBRARY_PATH=/usr/openwin/lib"

Please contact a TA immediately if it doesn't work.

If you are on an AcIS MacIntosh, start by typing "fingeron" to allow people to finger you, and then type "finger [userid]" (e.g., I would type "finger es66"), which is called "fingering yourself", believe it or not. This will show you the name of the Mac you are on. Next, type "export DISPLAY=[Mac Name]:0.0" (e.g., I would type "export DISPLAY=ham702:0.0") to tell the Unix computer where it should open a window.

Next, you must tell the Mac you are on that it will have to open a window. Do this by starting "MacX", which you will find under the apple menu (click on the apple at the top-left).

Now, you can type "java Stinky" and expect it to be able to open a window with the doodle in it. Well, you may have to switch back to the MacX program so the window actually comes up -- do this by selecting it from the menu at the top-right corner of the screen.

If you are on an HP in the lab, see the instructions below.

You will see that the doodle is really lame - it just does a couple right angles.

Your assignment: Your assignment is to edit Stinky.java so that it creates two squiggles or zigzags. You only have to edit the very end of Stinky.java -- look there and you will see a comment that says "only change this part". Specifically, just change the value of the String variable named 'program'. First, try making a slight change to the doodle, recompile it, and make sure it works -- this is for your own benefit.

Then, to get some zigzag going, change the end of Stinky to be a loop that builds up a string that commands Stinky to repeatedly go right, down, right, up, etc. -- or something like that -- the nature of the zigzaging is up to you. Hint: start it out with a few "reduce"s before the loop.

After you get it to zag across most of the window, add another loop that makes the String even longer so that it zigzags another direction. For example, if your first zigzagging went from top-left to top-right, now have it zag down-left. Make sure this adds on to the variable named 'program'.

You can optionally put colorfulness in your zigzaging.

Note: if you see a doodle you like, you can put it on a web page by making another doodle page like you did for a previous homework, and copy and paste the Stinky program into the HTML file.

Opening a Stinky Window on an HP:

HOW TO EXPORT THE DISPLAY OR 
HOW TO GET THE STINKY WINDOW TO POP UP
--------------------------------------
               ON AN HP
--------------------------------------

NOTE: All "" are there to show you what you should type in, but you should 
      NOT type the "" in! :)

-A- Opening windows
   1) Log in to the HP
   2) Open an xterm session 
      This can be done by clicking on the "Xterm" icon at the bottom of
      the screen.
   3) Open a cunix session
      This can be done by clickin on the "Telnet" icon at the bottom of
      the screen. Then, type "cunix" in the window/box that pops up.
      Following, you will see the usual cunix-login screen.

-B- Exporting the display
   1) In the xterm session window, type:
      "xhost +"
   2) Find your machine name: (there are two ways to do this)
      a) Look for a sticker on your computer that says something like:
         "muddhp2.cc.columbia.edu"
	 "muddhp23.cc.columbia.edu" 
	 etc.
      -OR-
      b) From the xterm window type:
         "hostname"
         This will give you your machine's name. eg.
	 "muddhp2"
	 "muddhp23"
	 etc.
   3) Now that you have your machine (host) name, you need to export
      the display to that machine. In the cunix window, type:
      "export DISPLAY=[machine name].cc.columbia.edu:0.0"
      eg.
      "export DISPLAY=muddhp2.cc.columbia.edu:0.0"
      It is critical that DISPLAY be capitalized. If when you typed
      "hostname" (see step 2), it returned muddhp2, you need to tap on
      the ".cc.columbia.edu" part.

That should export your display properly. This will work from any machine
in HP. Good luck!!!

email: evs at cs dot columbia dot edu