In this lab, you will play with existing Java programs with the goal of learning a few things about Java: (1) how programs are structured, (2) how to draw simple pictures, and (3) how do do arithmetic with integers. The instructor and the prefector will be around if you have questions (and they'll be thrilled to answer them, because it makes them feel like they're earning the oxygen that they're breathing). Ask if you have any questions, and have fun!
Please work in your assigned group for this lab. Remember that all work is to be done with both teammates working together at the computer. If you find that you absolutely can't schedule time to work together, split up for this assignment and turn in your own work. As always, feel free to ask lots of questions!
One of the nice things about programming in Java is that a lot of the hard work has already been done for you. There are a lot of predefined classes that can be used to accomplish a lot of interesting tasks, including a large number of classes that can be used to program graphics. The details of doing so can be be somewhat tedious in Java—especially for simple things like drawing pictures in a window. Luckily, some kind soul recognized that problem and wrote a class to handle simple drawing tasks much more easily. We'll be using this class in today's lab.
Canvas
classCreate a directory called lab3
to hold your work for
this lab. Save the file Canvas.java in that
directory. You can open up the file and look at it if you'd
like. However, we won't be paying much attention to what's in this
class. Instead, we're just going to use this class in our own
programs. (You can think of Canvas.java as a black box that draws
certain things on the screen when you pull the levers in different
ways.)
Now copy MyArtWork.java into the same directory. Read through the code and see if you can figure out what it will do.
To compile your program, you need to compile both Java
programs. Type javac *.java
in the terminal window. The
*
is a "wildcard." In other words, "*.java
"
is shorthand for "all files that end in .java
". Finally,
assuming that you don't get any errors (call for help if you do), run
your program. Type java MyArtwork
and something should
happen. What do you see? Try modifying the program to accomplish the
following:
The documentation for the Canvas class is at this web page. Notice that the page has distinct sections: a general description of the class, a summary of fields and constructors (we'll talk about those later), and a more detailed description of all the methods available to you. Take a few minutes to skim the methods and to familiarize yourself with the documentation format. Then try picking out a few methods that look like they might be interesting, and try them out. Modify your program to use other methods from this documentation page.
We're going to shift away from graphics for a short while, but don't worry, we'll be back! Download Arithmetic.java, look through the program, to get a sense for what it might do, and run it. Answer the following questions in a file called lab3.txt.
Question #1: What does the operation % do? How about /? (If you're having trouble guessing what % does, try these examples: 50 % 5, 51 % 5, 52 % 5, 53 % 5, 54 % 5, 55 % 5, 56 % 5. Try other cases with a similar pattern and see if that helps.)
Now change the types of a and b from Integer to Double. (You'll also have to change input.nextInt() to input.nextDouble().) Recompile and rerun.
Question #2: How does the behavior of / change?
Question #3: The type Double is not very helpfully named. What does it mean in this context? (Your reading should be helpful in answering this question.)
Write a Java program named MyDrawing.java
. Your
program should create a Canvas
object and, using the
methods in the Canvas
class, draw a picture. Spend a
little bit of time experimenting with the different methods, and then
figure out how to combine these methods to draw more complex shapes or
pictures. Use the arithmetic operations above as part of your
program.
Some suggestions:
pause()
and
erase()
methods to erase part of the picture. (If you're
really feeling ambitious, you might want to figure out how you could
use these methods to make it appear as if something is moving across
your canvas.)Be creative, and have fun!
Turn in your entire directory with hsp, including your answers to the questions as well as your graphics demos. Creativity is highly valued! Again, only one member of a team needs to submit the work, but make sure that both team members names are in comments in the program and in lab3.txt.