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.
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:
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... .
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,
actually does the work of creating a 10x10 graphical "canvas" inside a window. That canvas is called
art. Next, there is the line
GrCanvas art = G.createCanvas();
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.
art.add(new GrRectangle(0,0,6,10,Color.red));
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... .)
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
/ operates
differently than you might expect when both of its operands
are integers. For example, 10/7 will produce a
result of 1 ! When one of the operands is a double, however,
the results are as expected. So, 10/(double)7 and
10/7.0 and 10.0/7 ... all produce
the result 1.42857 . % (modulus) operator? It operates only on
integers and its result is the remainder after division.
For example, 10%7 results in 3, because
3 is the remainder when 10 is divided by 7. Some other examples:
20%6 is 2 and 20%5 is
0. doubles
and ints is pretty common. Casting an integer
to a double does not change the value, for example, (double)7
is just 7.0 . WARNING: casting from a double to an
integer can change the value, and it does not round, it
truncates it! Thus, (int)7.999 is 7 .
H.pl("pi is " + H.fmt(d) + " or so"); prints pi is 3.142 or so
H.pl("pi is " + H.fmt(d,5) + " or so"); prints pi is 3.14159 or so
H.fmt(d) uses 3 places of precision after the decimal point, rounding as appropriate.H.fmt(d,N) uses N places of precision.Math.PI or similar built-in constants isn't allowed! 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
H.nl() to get input
from the user. The nl stands for "next line" (and so it waits
until the user enters Enter)String variable
at the top of main. Then, assign the
output of nl() to it. For example,
this code twice echoes back what the user types:
String s; // a placeholder for any String
H.pl("Hi! What's your name?);
s = H.nl(); // this gets the user's input into s
H.pl("Hello, " + s); // this prints "Hello, " plus what's in s
H.pl("What dorm are you living in?");
s = H.nl(); // gets the next user input into s
H.pl(s + " is OK, but I prefer West"); // prints a message with that input
You can use the String variable named s again
and again -- each time it gets overwritten with the new input from the
user. Also, keep in mind that the name of the variable (s)
is not important -- you can name it response or
talkToTheHandCauseJimmyAintListening, if you prefer. You do,
however, have to use the name consistently if you expect to reuse the
value stored in that variable over and over...
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.
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.