Problem Set 6 No9.e10b.2017
Problem Set 6 No9.e10b.2017
I. Overview
This problem set is due prior to 5 PM on Monday, April 10. The
assignment is designed to reinforce your understanding of Swing and event-
driven programming techniques, as well as other related concepts. Consult the
Reges & Stepp textbook (chapter 14) as well as Oracles on-line documentation
at http://docs.oracle.com/javase/7/docs/api/index.html as needed.
1The alternative would be to run such applications on a personal computer that has Java installed, e.g.,
using the Terminal program under Macintosh OS X.
My mail program presents a window that looks like this when I want
to compose a new message:
Send
Note that the JFrames title should be set to whatever the user types
inside the Subject: JTextField. If the user does not type anything, then the
title should simply be New Message.
Dr. H. H. Leitner Unit 6 Problem Set Spring, 2017
Computer Science E-10b: Introduction to Computer Science Using Java, II Page ! 3
! !
2You could utilize such methods as drawOval, fillOval, and drawRect; all three take 4 arguments: x, y, width, and
height. Or you could use a JPanel (sized correctly) and set its background color. You decide what works best!
!
The drawing area shown above is 300 pixels wide by 200 pixels high.
Its background color is cyan. The horizontal and vertical lines are drawn
in red and the diagonal line is drawn in black. The diagonal line has up-
per-left corner (50, 50). Successive horizontal and vertical lines are spaced
20 pixels apart.
A user clicks on one of the numbered tiles thats adjacent to the empty
space, and that tile then moves to the empty space. For example, if I click
the 8, the result looks like this:
!
Note that if a user clicks on a numbered tile that is NOT adjacent to the
empty space, then nothing should happen.
IMPORTANT: To avoid the having the tiles shuffled into positions that
make it literally impossible to move them back into the order shown
above, you need to shuffle the tiles carefully. One way to do this is to re-
peatedly exchange 2 tiles positioned adjacent to the open space in a le-
gal way. You should place a Shuffle button on the display, as shown in
the following solution written by one of the teaching assistants:
You should arrange the initial configuration of the board with the tiles
in order, from 1 to 15. Then clicking the Shuffle button will reorder the
tiles in a solvable manner. You can play with this solution by copying the
files Fifteen.class and Fifteen$1.class from the unit6 folder on
the Lecture Examples page of our course website.
! !
Running on Macintosh OS/X Running on Windows
Now I click =:
!
Now I click
You get the idea: the 4 basic arithmetic operators (+, -, *, and
/) are binary in nature, requiring two operands. The square-root opera-
tor is unary, and will perform its computation on whatever value is cur-
rently in the calculators display. If the square-root of a negative number
is attempted, the calculator display should produce an ERROR message.
In addition, consider what would happen if the user clicks 1 then 6
then + then 9 then then = in this case the calculator should
take the square root of 9 (which is 3), and then add it to 16, which gives a
sum of 19. If the user hit = after the 9, and then hit the result 5
would instead be displayed.
http://sites.fas.harvard.edu/~cscie10/CalcBackendTest.java
http://sites.fas.harvard.edu/~cscie10/CalcBackend.java
[3] The calculator performs all of its computations using the data type
double. Consider using the Java class method
Double.parseDouble(s) or Double.valueOf(s).doubleValue() to
convert from a String value to its double representation.
[4] The display should not allow any part of the number to scroll off
either side of the field. And, of course, the user should not be able to
input a number with more than one decimal point!
[6] You can use a GridLayout for the Calculator buttons, but see extra
credit option #7 below.
[2] 3 points: Implement a +/- button that negates the current display.
Clicking this button repeatedly will toggle the values sign.
[3] 3 points: Allow the user to control the calculator by using the keyboard
(in addition to the mouse). In other words, hitting the * key will
have the same effect as clicking the * with the mouse. (Examine
the Microsoft Windows calculator to find out which keys correspond to
, =, MC, and others. One way to do this is to right-click on a but-
ton and then select Whats This?) Youll need to learn how to do
this by referring to: https://docs.oracle.com/javase/tutorial/
uiswing/events/keylistener.html andhttps://docs.oracle.com/
javase/tutorial/uiswing/misc/keybinding.html
[6] up to 5 points: Implement one button of your own design. Your idea
must be approved in advance by your teaching fellow. To get you
thinking along the right line, heres a view of OS/Xs calculator, op-
erating in scientific mode.
[7] 5 points: The layout manager used to arrange the buttons in the
above figures is the most complicated one (but also the most flexible),
a GridBagLayout. It allows the calculator window to be resized
while keeping the relative size and location of the various buttons
intact. Refer to the GridBagDemo.java program in an earlier lecture
handout for guidance. Study the code in that example to understand
how to use the GridBagLayout and GridBagConstraints classes.
That programs output is shown below:
Write a Java application using Swing graphics that draws the face of a
clock, showing the time that the user enters in two JTextField areas (one
for the hours, one for the minutes).
Hint: You need to find out the angles of the hour hand and the minute
hand. The angle of the minute hand is easy; the minute hand travels 360
degrees in 60 minutes. The angle of the hour hand is harder; it travels 360
degrees in 12 x 60 minutes. Dont attempt this problem if you dont re-
member your trigonometry!
Hour 5
Minute 45
[9] 10 points (for extra credit only) use file named Olympics.java
Write a Java application using Swing graphics that draws the Olympic
rings.