Programming Assignment 3 (100 Points) : Start Early!
Programming Assignment 3 (100 Points) : Start Early!
START EARLY!
README ( 10 points )
You are required to provide a text file named README, NOT Readme.txt, README.pdf, or
README.docx, etc. with your assignment in your pa3 directory. There should be no file extension after the file
name “README”. Your README should include the following sections:
STYLE ( 20 points )
Please see previous programming assignment for details on Coding Style.
CORRECTNESS ( 70 points, 35 points each program)
All of your code/files for this assignment need to be in a directory named pa3 in your cs11f home directory.
Please see previous programming assignments to setup your pa3 directory, compile, and run your programs.
Remember we run/execute applications differently than applets.
START EARLY!
Program 1 - Triangles:
Write a Java application (Triangles.java) that displays the following triangle patterns side-by-side using nested
while loops. Everything can be in main().
To get a better understanding, if size of the triangle is 5, here’s the spacing (represented in a grid, for a clearer
picture of the individual '*' and ' ' characters).
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
- All asterisks ('*') and spaces (' ') must be printed a single character at a time using
System.out.print( '*' ) or System.out.print( ' ' ) controlled by the nested while loops.
No Strings! You are not allowed to use a String or StringBuilder or StringBuffer or any data structure to
build each line to be output.
- Formatting is important for this program. If your output does not match the required output
character-for-character, no credit will be awarded. Note there is no space at the end of each line.
- Only valid integers will be entered, but you must check for integers less than 2 and report the
exact error message as shown in the example below.
Example 1:
java Triangles
Enter the size of the triangles to display: 12
* ************ ************ *
** *********** *********** **
*** ********** ********** ***
**** ********* ********* ****
***** ******** ******** *****
****** ******* ******* ******
******* ****** ****** *******
******** ***** ***** ********
********* **** **** *********
********** *** *** **********
*********** ** ** ***********
************ * * ************
Example 2:
java Triangles
Enter the size of the triangles to display: -1
Triangle size must be > 1; Try again.
* ***** ***** *
** **** **** **
*** *** *** ***
**** ** ** ****
***** * * *****
Program 2 – Mickey:
This program will build off of your last Mickey program, so you should make sure that program is working
correctly first. Being more Object-Oriented, we will separate the GUI controller and the Mickey object.
In this program you will be adding a feature that allows you to flip the Mickey left if the mouse is pressed on his
left ear, flip right if the mouse is pressed on on his right ear, and flip upside-down if the mouse is pressed on
his face—all mouse presses must last for longer than 500 milliseconds (1/2 second) during a mouse click.
Once the Mickey is flipped upside-down, if the mouse presses the face in the Mickey for longer than 500
milliseconds during a mouse click, the Mickey should flip right-side up again. Even when Mickey is upside-
down, it could still be flipped either left or right by clicking on its ears. For flipping the Mickey either left or right,
the flip should be centered on the ear that was clicked on. A long mouse press during a mouse click not in
Mickey will not flip Mickey. Recall: a mouse click event is fired if there is a mouse press event followed by a
mouse release event with no mouse movement between the press and release. A mouse press followed by
moving the mouse one pixel and then a mouse release does not fire a mouse click event.
For example:
How the Mickey looks How the Mickey looks after Then click on Mickey’s face
when you place it with holding down the mouse more holding the mouse button
initial mouse click: than ½ second on mouse click down more than ½ second
while mouse is on the left ear: to turn it upside-down:
How the Mickey looks after How the Mickey looks after
holding down the mouse button holding the mouse button
more than ½ second on mouse down more than ½ second
click while mouse is on the right on mouse click while mouse
ear: is on Mickey’s face:
Notes: You should be able to drag mickey around despite however the figure was flipped. Also, even if part of
the Mickey is off the canvas, you can still flip Mickey based on its parts that are still left on the canvas.
FlippingMickey.java - this is the main GUI controller class that handles all mouse events and controls what
we see on the canvas. This class needs to extend WindowController. Methods defined in this class include:
begin(), onMouseClick(), onMouseDrag(), onMousePress( ), onMouseRelease(), onMouseExit( ),
onMouseEnter( ). Where appropriate, code in this class will create an instance of a Mickey and send
messages to this Mickey object (tell the Mickey figure to flip, ask the Mickey figure to check if the current
mouse Location is contained in the Mickey figure, tell the Mickey figure to move, tell the Mickey figure to
remove itself from the canvas).
Mickey.java - this class defines what a Mickey figure is (the 3 filled ovals) and what a Mickey figure can do
(what messages it can respond to). This class will define a constructor to initialize a new Mickey object placing
it on the canvas as part of the FlippingMickey along with the methods to determine if the mouse pointer is
contained in the Mickey figure, move the Mickey figure some delta, remove the Mickey figure from the canvas,
and flip the Mickey figure.
Timer.java - this class calculates timing between events. Just copy the code on page 168 of the textbook for
the class Timer.
It is important to note the difference between the FlippingMickey class and the Mickey class. The Mickey class
should have all the relevant variables and methods specific to each Mickey created on the canvas (in this case
we only create one at a time). The GUI controller class, FlippingMickey, handles all the activities that occur on
the canvas – this includes the mouse interactions/events with the canvas and Mickey object on the canvas.
Since both classes interact with each other, the FlippingMickey (GUI) needs to have a reference to a Mickey
object and the Mickey object needs a reference to the GUI canvas in the FlippingMickey. This is done by
passing the canvas as an actual argument to the Mickey constructor when the code in onMouseClick() in
FlippingMickey creates a Mickey object. Examples in Chapter 6 of the textbook show how to do this.
Specifications:
The program should still have same functionality as was specified in PA2 (able to create a Mickey on the
canvas and drag it around).
import objectdraw.*;
// additional variables you might need and begin() and the event handling methods
}
A skeleton example of Mickey.java:
import objectdraw.*;
Use calls to the Timer class methods in FlippingMickey to determine how long the mouse button was pressed:
- Create a Timer object in begin()
- When the mouse is pressed, the timer should reset (start the timer).
- When the mouse is released, get the elapsed time in milliseconds and if the mouse was on the Mickey
(grabbed) and the elapsed time was longer than ½ second (500 milliseconds) then flag that the Mickey figure
should be flipped if this was indeed a mouse click (vs. a drag).
- In mouse click event handler send the flip message to the Mickey object if it should be flipped. Note the
mouse click event will be fired after a mouse press event and mouse release event with no mouse motion
between the press and release.
- The Mickey should not flip if the mouse is dragged or if the mouse press/click is not on the Mickey.
- When the mouse is clicked, if the mouse was pressed for enough time (1/2 second), the Mickey should flip
upside-down or right-side up. Otherwise the Mickey does not change.
- You should use the following constant to compare with the elapsed time Mickey was pressed:
The starting text/instructions of your program should be adjusted to look like the following (and should
disappear when a Mickey is placed, just like in your last assignment):
cd ~/pa3
cp FlippingMickey.java RotatingMickey.java
cp FlippingMickey.html RotatingMickey.html
By using the previous Unix commands, you are creating a copy of FlippingMickey.java and naming that copy
RotatingMickey.java. BOTH FILES ARE NEEDED IF YOU ATTEMPT THE EXTRA CREDIT.
After this, make sure to change the class definition in RotatingMicke.java to the new name. ie:
public class RotatingMickey extends WindowController {
…
}
Just like for the FlippingMickey.java implementation, these presses must last longer than than ½ second (500
milliseconds) during the mouse click.
For example:
How the Mickey looks How the Mickey looks after How the Mickey looks after
When you place it with holding down the mouse more holding down the mouse more
initial mouse click: than ½ second on mouse click than ½ second on mouse click
while mouse is on the left ear: while mouse is on the left ear again:
How the Mickey looks after How the Mickey looks after How the Mickey looks after holding
holding down the mouse button dragging Mickey over to the side down the mouse button more than ½
than ½ second on mouse click of the canvas second on mouse click while mouse
while mouse is on the right ear: is in the face.
Just to be explicit, no matter what orientation the Mickey is currently in, clicking in the face for over a half
second will center the figure upright in the center of the canvas.
Note: You will NOT receive extra credit for repositioning Mickey in the middle if you hardcode Mickey to be
moved to the center of a window with a specific size; we will be checking this particular feature with arbitrary
window sizes.
This time, the starting text/instructions of your program should be adjusted to look like the following (and
should disappear when a Mickey is placed):
Turnin
To turnin your code, navigate to your home directory and run the following command:
You may turn in your programming assignment as many times as you like. The last submission you turn
in before the deadline is the one that we will collect.
Verify
To verify a previously turned in assignment,
> verify pa3
If you are unsure your program has been turned in, use the verify command. We will not take any late
files you forgot to turn in. Verify will help you check which files you have successfully submitted. It is
your responsibility to make sure you properly turned in your assignment.
Files to be collected:
Triangles.java
Mickey.java
FlippingMickey.java
FlippingMickey.html
Timer.java
objectdraw.jar
README
The files that you turn in must be EXACTLY the same name as those above.