Homework 02



Assignment: Four problems -- 10 points each for #1 and #4; 15 points each for #2 and #3 -- due on: Reading: Your CS5 lecture notes and the online readings for week 1 and week 2.




Problem 1   [10 pts]     Hello, World     [topic: Getting started with the HMC labs and Java]

For this problem, you'll compile, perhaps alter, and then execute a small "Hello, World!" program in Java. Although this may seem like a rather silly exercise, as Wikipedia notes:

A "hello world" program can be a useful sanity test to make sure that a language's compiler, development environment, and run-time environment are correctly installed. Configuring a complete programming toolchain from scratch to the point where even trivial programs can be compiled and run may involve substantial amounts of work. For this reason, a simple program is used first when testing a new tool chain.
Provided the "workspace files" associated with the Windows and Mac IDEs (Integrated Development Environments) were properly set up (that was my job!), and the account and machine you are logging into are compatible with this workspace, your "hello world" should work with very little difficulty. But anytime you return to program in a new language (machine, or environment) its always a good idea to start with this simple exercise.

There are slightly different programming environments for the lab PCs and lab Macs -- be sure to go through the appropriate instructions. Also, if you would like to set up this software on your own machine, it is all freely available. Full instructions are available from the CS5 software install page.

Macintosh

  1. Log in with your odin username and password
  2. Open your charlie home directory:
  3. Download the first problem from the CS 5 website:
  4. Open the Hw2Pr1 java code in XCode (this is the Mac's IDE):
  5. That's it! Before submitting, be sure to include your name, the date, the time you spent on the problem (not much!), and any other comments for the graders in the top portion of the code. A guide to online submission of your java programs is below, after the PC instructions.

PCs

  1. Log in to a lab PC with your charlie username and password
  2. Open your charlie home directory:
  3. Download the first problem from the CS5 website:

That's it...!

For this first problem, there's not much more to it. To get you in the habit of documenting your code, be sure to add your name, the date, and any comments to the top of the file before submitting it.

Submitting your code

The best way to get to the site is to click the Submission link from the upper right of the CS 5 webpage. From there, you should

  1. Log in with your odin username. Initially, your CS 5 submission password is the same as your odin username.
  2. The first time you submit, you should change your CS 5 submission password if you wish.
  3. Click Submit an Assignment. Enter the assignment number and problem number (assignment #2, problem #1 in this case) and then click Browse or Find File.
  4. You will be given a dialog box to browse for your file. On the Macs, be sure to click on the HMC_2009 label on the left in the middle (and then navigate to your charlie directory), so that you are getting files from the Charlie server. On the PCs, go to the H: drive below My Computer.
  5. Go to the folder with your problem (in this case Hw2Pr1). Enter it and then enter the subfolder named source_code.
  6. Double-click on the file named CS5App.java. As long as you view folders by default with Details you'll see the .java extension. On the Mac, you'll always see the full file name.
  7. It should return you to the webpage, where you can click Submit (Mac) or Submit Query (Windows)
  8. If successful, a page indicating the time should appear -- you may want to follow the source link to be sure you submitted the code you wanted to! You may submit as many times as you like -- all of the submissions are accessible from the CS 5 submission website (only the last one will be graded). You can also use the website to download your source code to work on it elsewhere, as described on the Take CS 5 anywhere page.

Important:

Congratulations! You've finished this first problem. We will try to grade all of the submissions within a week of the due date. Now you're set to download the next problem, Hw2Pr2, and try it... .








Problem 2   [15 pts, individual problem]     abstract art ?     [topic: introduction to Objects and graphics]

This is a problem to be worked on individually -- you may discuss the problem with other students, the tutors, or the instructor -- please do! However, all of the coding and all of the code must be your own, starting from the provided files.

In this problem, you'll be writing a program to display rectangles on a drawing canvas. The goal is to create a particular picture, shown in the image below. Although there are row and column numbers in the picture, they are just for reference here -- they're not in the actual program. Also, since you may be reading this on a black-and-white printout, the second image has letters to indicate what each color should be.

    

Although this is only a minimal image, this problem will introduce you (or remind you) to some of the programming conventions with images and graphical objects. Also, being a minimalist s not necessarily a bad thing!

Even before reading further, you may want to try out an example of graphical java code. To do so, download the Hw2Pr2.zip file from the Files for download link on the CS 5 home page. Then, unzip it and start it just as with problem 1. When you run this program, an extra window opens (in addition to the console). To close this window, click on it and enter q.

In CS5App.java for Hw2Pr2 there are more things going on than in the "hello world" program (in fact, this program is the graphical-plus-console equivalent of console-only "hello world" program). For instance, here we're mixing graphics and console output (you can also mix graphics with getting input from the console, but in this problem, we're not yet using console input). In what follows, we explain a bit more about what's going on.

First, there is code to print a short message (to the console). Then,

  GrCanvas art = G.createCanvas();
actually does the work of creating a 10x10 graphical "canvas" inside a window. That canvas is called art. Next, there is the line
  art.add(new GrRectangle(0,0,6,10,Color.red));
This line of code adds a new graphical rectangle to the canvas named art. The syntax (i.e. formatting) of this code may seem quite strange, but the syntax is not important at the moment -- what is important are the numbers and color that specify this rectangle.

The first two input parameters indicate the horizontal and vertical offsets of the upper-left corner of the rectangle you want to draw. The third parameter indicates the width you want for the rectangle, and the fourth parameter indicates the height you want. The width and height should be at least 1. The fifth parameter is the color you would like the rectangle to be, e.g., Color.red.

Important: note that the coordinate system here is different than what is typically used, e.g., in mathematics. The y-axis increases downwards, and the origin is in the upper-left corner.

Java offers lots of support for graphics. In this problem, we're explicitly using the built-in colors that Java offers -- they are listed at the bottom of the CS5App.java file. You won't need colors other than Color.green, Color.red, Color.yellow, Color.blue, and Color.white for this problem. (It's possible to mix your own colors by specifying different amounts of red, green, and blue -- click on this page if you're interested and have a bit of Java background already.)

Experiment! You can change the "stacking order" of the rectangles by changing the order in which they are drawn. Also, rectangles are filled-in by default, but you can create "frames" with just the outer edge by including an additional false parameter after the color:


  art.add(new GrRectangle(3,4,7,5,Color.yellow,false));
The above line, as you'll see when you run the program, creates an empty rectangular frame, rather than a filled-in rectangle. (You won't need this unless you're working on the extra credit.)

You can also add a pause anywhere in the program with the code H.pause(200), which waits for 200 milliseconds before proceeding. This is useful for seeing how a drawing is being created. (And you can change the 200, as well.)

Submitting your code Once your program is drawing the desired figure, you can submit your code the same way that you submitted it for Hw2Pr1. Be sure to label it as homework #2 and problem #2 in this case.

Extra Credit For extra credit, try to create the same drawing in as few drawing commands as possible. (See the bottom of this assignment for more details... .)






Problem 3   [15 pts, individual problem]     a printing puzzle     [topic: output formatting and variables]

For this problem, you'll write a program that will print a more involved set of statements than Problem 1. In addition, you will use variables to print out quantities. It is easier to print quantities as Strings, but when they're numerical variables we can actually do interesting things with them, and this problem will take advantage of that... .

Getting started Download and unzip the Hw2Pr3 folder from the usual spot... .

What to do For this problem (Problem 3) your program should print out exactly the following text:

/-------------------------------------\
| Welcome to the frugal arithmetician |
\-------------------------------------/

The goal is to print several numbers using two variables:

  an integer, x, which equals 5
  a double, d, which equals 42.0

along with the operators +, -, *, /, %, (int), and (double) .

Five is 5, and ten is 10.

"one"           is 1
"three"         is 3
you're "young" til 36
"one-third"     is 0.33
"ten thousand"  is 10000
"pi"            is 3.14159

/-------\
| Bye ! |
\-------/

What makes this problem more than just printing is that you're not allowed to print any literal numbers! That is, you may print any strings and characters you like except that you may not explicitly print any strings or characters with numbers or digits in them. That is, the following line would not be allowed in this problem:

  H.pl("Five is 5, and ten is 10.");

How, then, to print the numbers you'll need? You could use two variables and arithmetic operations on those variables. For example, to print the 7th and 8th lines needed, you could write

  int x = 5;
  double d = 42.0;

  H.pl("  an integer, x, which equals " + x);
  H.pl("  a double, d, which equals " + d);
where you have defined the variable x with value 5 and the variable d with the value 42.0 .

You could, of course, define a variable for each value you want to print... . So, to make the problem a bit more interesting, there is one more limitation -- inside your main method you must use only two variable definitions:

  int x = 5;
  double d = 42.0;
No other variables or numeric constants are allowed! This means that you'll need to combine x and d with themselves and each other in order to create the numeric values you will need. Also, you should not create numbers a few digits at a time. Thus, to print the number 55, you could not use
  H.p("" + x + "" + x);
because you're really just printing two fives next to each other. In the spirit of this problem, you could add eleven x's together:
  H.p(x + x + x + x + x + x + x + x + x + x + x);
All in all, the permitted operations are these:
  ( )             parentheses (for grouping operations)
  +               addition (for ints and doubles)
  -               subtraction (for ints and doubles)
  *               mutliplication (for ints and doubles)
  /               division (for ints and doubles -- see the hints)
  %               the modulus "remainder" operator (for ints -- see the hints)
  (int)           casting to an integer (it truncates the value!)
  (double)        casting to a double (see the hints for reminders about casts)

A legal way to print the 12th line needed would be to write

  H.pl("Five is " + x + ", and ten is " + (x+x) + ".");
Feel free to use this in your program.

P.S. If this problem seems too easy... see the extra credit!

Assumptions and Hints








Problem 4   [10 pts, individual problem]     artificial intelligence?     [topic: input and output in Java]

For this problem, you'll be writing a program that chats with a user in the console window via the keyboard. There are a number of such chat bots available to try out through instant messaging and online, e.g., Smarter Child.

There have been many attempts at developing this type of "interactive" intelligence -- in fact, the most widely accepted computational definition of intelligence is to do exactly what this problem is asking: to build a program that can interact so naturally with a person that the person wouldn't be able to tell the conversation was with a machine. So far, no program has ever come close.

One possibility may be that people are trying too hard to write interactively intelligent programs. After all, how many conversations with supposed flesh-and-blood people have you had wherein what you said seemed to have absolutely no effect on the conversation itself? This seems to be particularly common with strangers who call your house around dinner time with important news of new long-distance offers. (In any case, their approach is the one we will take in this problem.)

Your task is to write a program that introduces itself and strikes up a conversation. It should pause at least three times to get input from the user -- for example, with a question of some sort, but it does not actually have to use the answer in any way.

Hints

For full credit, your program should ask at least three questions, but additional credit and admiration is available for the convincingness and creativity of the dialog, and I encourage you to give the graders a charge by not ignoring the input (hey, everyone needs a good belly laugh).

Just for the record, there is a $100,000 prize for the first program that actually fools a panel of judges into thinking it's human. (See this link for details.)

Here's a sample run from my program:

Good evening! This is Pat from Verizacom Wireless Solutions.
I notice from our records that you're not yet enrolled
in our monthly cash-back program?

  Your records? I'm not even a customer...


Do you realize that our new personalized tracking
system will allow you to defray calling charges by passing them on
to the people and businesses you call, provided they are part
of our ever-growing Verizacom family? And all without the
hassle and annoyance of old-fashioned "collect" calling...

  That doesn't even sound legal.


In fact, to demonstrate just how easy it is to participate
in our "cash-back" plan, this call is being cost-defrayed to our
callee as we speak! Now, if I can just confirm your billing address,
we will have you up and running for us as fast as possible.

  Who's the "callee" in this case? What?


Excellent. I want to thank you for your value as a
Verizacom customer. A service representative will be by your
house shortly to collect on this evening's transaction as "painlessly"
as possible. We look forward to your continued service in the future.







Extra Credit

Each week, there will be one or more extra-credit problems worth up to 20% on top of the maximum HW score. This week's extra credit extends problems 2 and 3: the fewest number of drawing commands required to produce the "artwork" will earn 10% additional credit; if you manage to use close to the fewest, but not quite the minimal number, partial extra credit will certainly be given. (6 is the minimum possible.) Similarly, the fewest number of arithmetic operations required for the printing of 36, 0.33, 10000, and 3.14159 in problem 3 will earn 10% additional credit, with partial credit possible for close-to-minimal solutions.

How will we count the operations in problem 3? Basically, the arithmetic operations count as 1 operation each; the casts between int and double and uses of parentheses count as no operations at all. Thus, the following way of printing "37" uses one operation, the subtraction:

  H.pl( (int)(d - x) );

Aside: a fundamental question (perhaps the fundamental question) in computer science is determining the smallest number of steps that will perform a particular task. The larger that "smallest number", the inherently more complex the task. Thus, this extra credit is really determining the complexity of the "art" and calculations of problems 2 and 3, respectively.