HOMEWORK
PIC 20A, Spring 2002
PROGRAMMING PROJECTS:
- 1. Due date: 04/05/2002, by 5:30 pm.
- Required File Names:
ClickMe2.java, ClickMe2.html
.
- The exercise is suggested by
The Java Tutorial: the applet in our
second example draws
a red spot when you click the mouse within applet's bounds.
Use
Java 2 API documentation to modify the applet so that it will
display your name in purple instead of a red spot.
(Hint: Purple is made of equal parts of red and blue.)
- This is how your applet should behave:
ClickMe2.
- Make sure that you submit your header
in the required format, and that you follow all the instructions
listed below on this page.
- 2. Due date: 04/12/2002, by 5:30 pm.
- Required File Names:
Substring.java
.
- The exercise is suggested in [2], Chapter 1.
Write a program
Substring
that takes two numbers and a
string as command-line arguments and prints out the substring of the
string specified by the two numbers. For example:
java Substring hello 1 3
should print out
ell
Try to handle all possible errors that might arise because of bad
input.
- Make sure that you submit your header
in the required format, and that you follow all the instructions
listed below on this page.
- 3. Due date: 04/19/2002, by 5:30 pm.
- Required File Names:
SavingsAccount.java,
BankTest.java
.
- The exercise is suggested by
Prof. Cay Horstmann.
Write a class
SavingsAccount
that is similar to the
BankAccount
class of our example 5,
except that it has an added instance variable interest
.
Supply a constructor that sets both the initial balance and the
interest rate. Supply a method addInterest()
(with no
explicit parameter) that adds interest to the account. Write a
program BankTest
that constructs a savings account
with an initial balance of $1000 and interest rate 10%. Then
apply the addInterest
method five times and print the
resulting balance.
- Make sure that you submit your header
in the required format, and that you follow all the instructions
listed below on this page.
- 4. Due date: 04/26/2002, by 5:30 pm.
- Required File Names:
Sorter.java, SorterTest.java
.
- Look up the definition of the standard
java.lang.Comparable
interface and of
java.util.ArrayList
class in the Java
API documentation. Write a class Sorter
that supports
general-purpose sorting of an ArrayList
of comparable
objects. Your Sorter
class should define a static
sort()
method that takes an ArrayList
as an argument, sorts in increasing order the objects in the
ArrayList
assuming that they are
Comparable
, and returns boolean true
or
false
depending on success or failure respectively.
In case of success the original ArrayList
should
be sorted, in case of failure the original ArrayList
should remain intact. Test your Sorter
class
with a SorterTest
program which would create an
ArrayList
of java.lang.String
objects,
print out the original ArrayList
, then call your
Sorter.sort()
method on it, and finally print out
the sorted ArrayList
. (The String
class implements the Comparable
interface.)
- Make sure that you submit your header
in the required format, and that you follow all the instructions
listed below on this page.
- 5. Due date: 05/10/2002, by 5:30 pm.
- Required File Names:
Nim.java
.
- On a lighter note... (to rest after the midterm): the game of
Nim, the exercise is suggested by
Prof. Cay Horstmann.
This is a well-known game with a number of variants. We will
consider the following variant, which has an interesting winning
strategy. Two players alternately take marbles from a pile. In
each move, a player chooses how many marbles to take. The player
must take at least one but at most half of the marbles. Then the
other player takes a turn. The player who takes the last marble
loses. Write a program in which the computer plays against a
human opponent. Generate a random integer between 10 and 100 to
denote the initial size of the pile. Generate a random integer
between 0 and 1 to decide whether the computer of the human takes
the first turn. Generate a random integer between 0 and 1 to decide
whether the computer plays smart or stupid. In
"stupid" mode, the computer simply takes a random legal value
(between 1 and
n/2
) from the pile whenever it has a
turn. In "smart" mode the computer takes off enough marbles to make
to make the size of the pile a power of two minus 1 - that is, 3, 7,
15, 31, or 63. That is always a legal move, except if the size of
the pile is currently one less than a power of 2. In that case, the
computer makes a random legal move.
Note that the computer cannot be beaten in "smart" mode when it has
the first move, unless the pile size happens to be 15, 31, or 63.
Of course, a human player who has the first turn and knows the
winning strategy can win against the computer.
- Make sure that you submit your header
in the required format, and that you follow all the instructions
listed below on this page.
- 6. Due date: 05/17/2002, by 5:30 pm.
- Required File Names:
Clock.java, Clock.html
.
- The exercise is suggested by
Prof. Cay Horstmann.
Write an applet
Clock
that draws a clock face with a
time that the user enters in a text field. (The user must enter the
time in the format hh:mm
, for example 09:45
.)
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 * 60 minutes.
- Make sure that you submit your header
in the required format, and that you follow all the instructions
listed below on this page.
- 7. Due date: 05/31/2002, by 5:30 pm.
- Required File Names:
NimGame.java, NimGame.html
.
- The exercise is based on homework project #5.
Reimplement your game of
Nim
program to work as an applet
and as a stand-alone graphical application. You should keep the same
program logic as specified in homework #5, but the user interface
should be reimplemented as a GUI (Graphical User Interface).
The NimGame
class, should be able to run not only as
an applet in a browser, but also as a stand-alone application (by
typing "java NimGame
"). As usual your program should
graciously handle and recover from all user-input errors. The details
of the graphical representation of the Game, as well as user controls
is left up to your imagination, skill and amount of time you can spare.
The only general guidline is that the interface should be convenient
and transparent (intuitive) to any user, even the one who sees your
applet for the first time.
- Make sure that you submit your header
in the required format, and that you follow all the instructions
listed below on this page.
- 8. Due date: 06/07/2002, by 5:30 pm.
- Required File Names:
PlayNim.java, PlayNim.html
.
- The exercise is a continuation of homework project
#7. You are asked to add one more mode to your implementation of
NimGame
- a "Demo" mode. You also need to provide
user with a possibility to choose before the game whether he/she wants
"Demo" mode, "Smart" mode, or "Stupid" mode.
Once the mode is chosen, it can not be changed until the game is over.
User should be able to change modes before each new game but not
during the game.
You already know how computer should behave in "Smart"
mode and in "Stupid" mode. The new "Demo" mode
should be a non-interactive demonstration of a play in which computer
plays versus itself (so that a human user would be able to observe an
example of an actual game). Being in "Demo" mode, your
program should start at least two new independent threads representing
two "cyber" players. Each player-thread should consistently follow
any of the two usual game strategies, either "Stupid" or
"Smart". In the beginning of a "Demo" mode game each
player-thread should announce which strategy it will be following.
Certainly player-threads have to take turns, i.e. you will need to
arrange for synchronization between them.
Once again your implementation of the Nim
game should
be GRAPHICAL, and it should be runnable either as an applet or as
a stand-alone (graphical) application. The details of the graphical
interface of the Game, as well as user controls is left up to your
imagination, skill and amount of time you can spare.
The only general guidline is that the interface should be convenient
and transparent (intuitive) to any user, even the one who sees your
applet for the first time.
- Make sure that you submit your header
in the required format, and that you follow all the instructions
listed below on this page.
READING ASSIGNMENTS:
(Please note that an updated version of entire
content of both books is available on-line at no cost.)
- [1]
M.Campione, K.Walrath, A.Huml, "The Java Tutorial, 3rd Ed.
A Short Course on the Basics."
- [2] D.Flanagan
"Java Examples in a Nutshell, 2nd Ed.". Courtesy of
D.Flanagan, a
complete set of code samples from this book is available free of
charge.
- 04/01/2002
- [1], Chapter 1
"Getting Started", sections:
- 04/03/2002
- [1], Chapter 1
"Getting Started", sections:
- 04/08/2002
- [1], Chapter 2
"Object-Oriented Programming Concepts", sections:
- 04/15/2002
- [1], Chapter 2
"Object-Oriented Programming Concepts", sections:
- 04/17/2002
- [1], Chapter 3
"Language Basics", sections:
- 04/19/2002
- [1], Chapter 3
"Language Basics", sections:
- 04/22/2002
- [1], Chapter 5
"Classes and Inheritance", sections:
- 04/24/2002
- [1], Chapter 5
"Classes and Inheritance", sections:
- 04/26/2002
- [1], Chapter 7
"Handling Errors with Exceptions", sections:
- 04/29/2002
- [1], Chapter 4
"Object Basics and Simple Data Objects", sections:
- 05/01/2002
- [1], Chapter 4
"Object Basics and Simple Data Objects", sections:
- 05/03/2002
- [1], Chapter 5
"Classes and Inheritance", sections:
- 05/08/2002
- [1], Appendix B
"Internet-Ready Applets", sections:
- 05/13/2002
- [1], Appendix B
"Internet-Ready Applets", sections:
- 05/17/2002
- [1], Appendix B
"Internet-Ready Applets", sections:
- 05/24/2002
- [1], Chapter 8
"Threads: Doing Two or More Tasks At Once", sections:
- 05/29/2002
- [1], Chapter 8
"Threads: Doing Two or More Tasks At Once", sections:
- 05/31/2002
- [1], Chapter 8
"Threads: Doing Two or More Tasks At Once", sections:
- 06/03/2002
- The End. Please review all the material before the Final Exam.
GENERAL PRACTICES AND PROCEDURES:
- Assignments will be distributed in class and posted on the class web page.
Current homework assignments are listed above on this page. Read the text
and stay a bit ahead of the lecture. Material from the reading part of an
assignment may appear on midterms and finals. Approximately nine
programming projects will be assigned over the course of the quarter.
Each project will require you to write a program in Java to solve a
specific problem.
- All programming projects must be submitted electronically, by placing
your files in the "SUBMIT" folder in your account. Be sure to give your
files the EXACT name required (remember that case counts).
Computer programs must be submitted by the specified time on the due date.
No homework will be accepted by e-mail to the TA or to the instructor.
No late projects will be accepted.
Leave the homework files in your "SUBMIT" folder for the rest
of the course, even after they have been collected and graded.
You may be able to submit files remotely with ftp, but
it is best to make sure your submission works in the Lab before
you submit. If you wish, you may overwrite a previous submission
with a new version at any time before the due time on the due
date. Please do not store any files in your submit directory
other than those that are to be or have been collected. To
confirm that you have submitted correctly, run the checksubmit
program. This check generally requires a trip to the PIC Lab.
Your homework scores will bo posted in your gradebook on
"my.ucla" web site along with
any other feedback from the Reader.
Please, check your gradebook on
"my.ucla" web site each week to verify your grades. This is
very important, it is the only way to know for sure that you actually
received credit for your assignment.
- Each program you submit MUST begin with the following
header:
/*
Your name
Your Student ID
Your e-mail address
I certify that the following represents my own independent work and
conforms with the guidelines of academic honesty described in the
course syllabus.
*/
Any homework submitted without this header will not be graded,
and you will receive zero credit for it.
- Your code should be as easy to read as possible. Use indentation
carefully and consistently to delineate levels of nesting.
Disjoint blocks should be well separated by concise and
informative comments. This will be worth at least 1 point of
every assignment. Although you are allowed a fair amount of
choice in style, please try to conform to the conventions used
in lecture and in the text.
- You may use the PIC Computer Laboratory in Boelter Hall 2817 to work on
your programming assignments. Use the Java 2 SDK (version 1.3)
and/or Forte4Java IDE for your projects in this class.
These Java Platform development tools are installed
on all PC's in the PIC Lab in the following locations:
C:\java\jdk131\bin
C:\java\forte4j\bin\runide_multiuser.exe
If you have your own computer and would like to work at home, you can
use your favorite java development environment. However, you must still
submit your program to your UCLA class account. To do this, bring a
floppy disk containing your program to the lab and copy your files to
your class account. Always compile and test your programs on the lab
machines before submission.
- Start working on programming assignments early. Do not wait until the
last minute to submit your work, as the lab may be full near critical
time. Deadlines for homework are strictly enforced.
- You are encouraged to discuss aspects of the course with other students,
and you may discuss the homework assignments in general terms with others.
You may also get help from the TA or the instructor in writing your
programs. However, the general rules of academic honesty apply to all
homework: each homework assignment must represent your own, independent
work. In particular, you may not copy any part of a program written by
someone else without citing the reference carefully.
- If you have a question on the way a homework was graded, you must contact
the instructor within one week of its return. No e-mail request for
regrades will be accepted, you must contact the instructor in person.
- Your programming will improve with practice. After finishing an
assignment, do not be afraid to experiment with your own programs.
Last modified on
by fedandr@math.ucla.edu.