Homework 02



Due: 11:59 PM on Sunday, September 12, 2004 (for the Monday/Tuesday sections)
or 11:59 PM on Monday, September 13, 2004 (for the Wednesday/Thursday sections)

Reading: CS 5 notes, week 1 and week 2

Four problems, 10 points each for #1 and #4; 15 points each for #2 and #3



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. This is the programmer's view of the development cycle of software. (Actually, program design is another important step, but it's omitted here because there's nothing to design!)

There are slightly different programming environments for the lab pcs and lab macs -- be sure to go through the appropriate ones. 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 CS 5 software install page: (http://www.cs.hmc.edu/~dodds/cs5/install.html).

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 programming environment):
  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 CS 5 website (if you haven't already done so):
  4. Open the Hw2Pr1 code in JCreator:
  5. Change the code to print some other welcoming message (if you wish). Complimenting the CS 5 graders, who are all students who've taken CS 5 before, is never a bad idea ;)
  6. Compile the code by hitting the F7 key. Compiling is the process of checking code for errors and preparing it to run. If there are errors, they will suggest what needs to be fixed in the code. Run the code by hitting the F5 key. That's it!
  7. 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.

That's it...!

For this first problem, there's not much more to it. 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

Important! Be sure to submit ONLY the CS5App.java file -- don't submit any of the supporting files for any of CS 5's problems. We have those files already -- we use those and the CS5App.java file that you submit in order to grade each problem. You may not see the .java extension of the CS5App.java file on the PCs. However, the .java source code file that you want is the one with the small "J" icon to the left.

Also Important! Be sure to submit this problems under Homework #2, Problem #1. We won't find it if it's submitted under another homework or problem number. If you missubmit, you can always resubmit again.

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 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 is 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 page, which is linked from the CS 5 home page. Then, unzip it and start it just as with problem 1. You will see some code that gets the graphics started for this program. Here's an explanation of what's going on:

First, there is code to print a short message. You can mix graphics with printing and input from the console. The line

  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.

Warning: 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 (http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Color.html), 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 variables we can actually do 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 may use two variables and arithmetic operations on those variables. For example, to print the 7th and 8th lines needed, you should 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 may include only the two variable definitions:

  int x = 5;
  double d = 42.0;
but 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. A legal way to do it would be to 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. I have had many conversations with flesh-and-blood people (or, at least, I thought they were) in which my input to the conversation seemed to have absolutely no effect on the conversation itself... . This seems to be particularly common with strangers who call my home 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 you're not limited to ignoring the input if you don't want to... .

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 or extensions 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.