Semester 1 Labs
- Lab 1 - Due 10/2/03
- You are to draw a full UML diagram to describe a video
game, in this case the game "Super Mario Brothers" (TM). The game
could be described as a multitude of objects, including the
following: an enemy class
that describes in the most generic terms
what an enemy is, a goomba class
which extends the enemy class and
implements the way the goomba moves around and can hurt the heroes, a hammerturtle class
that extends the enemy class similiarly, a bowser class that extends enemy in
the same way; a hero class
which defines in
general terms what a hero is, how they can die, how they keep score, a Mario class that
extends the hero class and describes how to draw
Mario, a Luigi class which
extends the hero class and describes how to
draw Luigi. You also have a music
class which plays music.
You also have a level class
which will use objects of type goomba,
hammerturtle, bowser, music, and Luigi. Add in any methods that make sense to you
that they belong in the class. This lab is worth 2
points and is to be handed in at the end of class.
- Lab 2 - Due 10/7/03 - possible
solution
- Do exercises 2.44 & 2.45 in the text. This is a
partnered lab, meaning you may work with 1 other student in your class.
This lab is worth 4 points and is to be handed in by the end of the
class (you can be graded earlier though) on 10/7/03.
Tips!
- For 2.44, you might want to break the problem down into
two
pieces. #1 find out which is the first slot that has a cd in
it. #2
take that cd and put it on the stack. Thanks to the precondition
you
know that there must be a cd in the first four slots, so don't bother
checking beyond the first four.
- For 2.45, you need to get rid of the precondition.
The
precondition says that you should assume that 2 of the first 3 slots
have a cd in it. If you get rid of that precondition that means
you
need to ensure that at least 2 of the first 3 slots have a cd in
it.
That means you need to write code to check that indeed, 2 of the first
3 slots have a cd. Ask yourself how many possible ways there is
to
have at least 2 of 3 slots filled, and check for them.
(hint: there
are 3 ways). If any of those 3 ways is true then run the code
that is
already in listing 2.11.
- Lab 3 - Due 10/10/03
- Create a method that will reverse the order of 4 cds in a
Vic sequence. Your precondition is there is a sequence with at
least the first four slots filled with cds. You are to take the
cd in slot 4, move it to slot 1, the cd from slot 3 and put it in slot
2, the cd in slot 3 and put it in slot 2, the cd in slot 1 and put it
in slot 4. Hint:
You need not do the problem in this order, and you might want to use
the stack of cds to your advantage by placing more than 1 of the cds on
it at a time.... (though you can solve this however you
choose) In theory this can be solved with about 13 lines of code
in a method. This is a partnered lab and worth 3 points.
- Do problems 3.5 & 3.11 from the book. This is a
partnered lab and worth 4 points.
- Do problems 3.19 & 3.21 from the book. This is
a partnered lab and worth 4 points.
- Lab 6 - Due 10/30/03
- Write a void method that prompts a user for a number, and
then calculates the factorial of that number and then displays that to
the user using another jOptionPane dialog. Use showInputDialog
(section 4.2) to read in the number as a string. To convert that
string into an integer, do the following: int nameyourintegersomethingelse =
Integer.parseInt(insertnameofyourstringhere); (this
is from chapter 6) To convert the integer (with the result) to a
string you can type the following: String myresult = "" +
insertthenameofyourinteger; Note: A factorial is
defined as
factorial(x) = x * x-1 * x-2 ... * 2 * 1, where f(4) = 4 * 3 * 2 *
1. This is an partnered lab
and worth 4 points
- Lab 7 - Due 11/04/03 (at the end of class)
- Do problems 4.16 & 4.20 from the book. This is
a partnered lab and worth 4 points.
- Lab 8 - Due 11/06/03 (at the start of class)
- Do problem 4.28 from the book. This is a partnered
lab and worth 3 points.
- Lab 9 - Due 11/13/03 (at the end of class)
- Do problems 5.8 & 5.15 from the book. This is a
partnered lab and worth 4 points.
- Lab 10 - Due 11/19/03 (at the start of class)
- Your mission, whether or not you choose to accept it (you
actually don't have a choice in the matter) is to write a method to
double every character in a string. You will write a method that
will fit the following signature: public
static String doubles(String str) {
You must write it so that it
returns a string that is twice as long with every character from the
original string doubled. For instance, if the original str
is "Mr. C, I find your lack of faith disturbing" the resulting String
would be "MMrr.. CC,, II ffiinndd
yyoouurr llaacckk ooff ffaaiitthh
ddiissttuurrbbiinngg" You may not use any looping structures in your
solution at all. (it must all be recursive) Helper methods
are legal, as long as they also do not include any looping
structures.
Once you are done with that you must also complete a method to
calculate (approximately) a square root. This method will need to
fit the following signature:
public
static int squarert(int number) {
The method will work roughly as follows, you need to count
up until you find a number that when it is multiplied by itself, is
larger than or equal to the original number. (the first number
that either is the perfect square root of the number, or just larger
than the perfect square root). Note: You may not use
looping for this problem, but you may use a helper tail recursive
method (look at the notes in the study material for review of this) to
help solve this problem. Do not worry about negative numbers
being input.
This lab is partnered and worth 4 points
Tips (in the form of questions) for this lab:
- How do you find out how long a String is?
- What would you do if you had a String that was exactly 1 in
length?
- How do you add Strings together to create a new String?
- How would you get a substring of a String which consists
entirely of just the 1st character?
- How would you get a substring of a String which consists of
everything from the 2nd character until the end of the String?
These questions can be answered by the book, and if you have them, this
problem should be much easier.
- Lab 11 - Due 11/21/03 (at the end of class)
Today you will write a method two
different ways. Your mission is to write methods that reverse the
order of a String. In other words if a string "garply" is given
as a parameter your methods would return "ylprag". "h" would
return "h", "hi" would return "ih", and "" would return "". You
need to write this once using embedded recursion (you can look at the
first solution to lab 10 for help) and once using a traditional
non-recursive mechanism (using a for or while loop). Either
method should start off like this: public
static String reverse(String str)
You cannot define any variables outside of your method,
nor can you add parameters. Use the examples above to help guide
you towards what your base case might be.
If you complete one of the solutions completely (according
to the grading standards defined in the rubric), you can recieve 2
points. If both are completed completely (according to the
grading standards) you can recieve 4 points. This assignment is
out of 3 points, and is a partnered lab.
- Lab 12 - Due 12/2/03 (at the end of class)
Do problems 6.6 & 6.7 from
the book. Note for 6.7: 32 degrees Farenheit is 273.15 degrees
Kelvin, and for every 1 degree you go up of Kelvin, you go up about 1.8
degrees of Farenheit. This is a partnered lab and worth 4 points.
- Lab 13 - Due 12/5/03 (at the start of class)
Do problems 6.15, 6.37 &
6.38. This is a partnered lab and worth 3 points.
- Lab 14 - Due 12/5/03 (at the end of class)
Do the problems
here
on paper. This is
an individual lab, and worth 3 points.
Write a program that prompts the
user to enter in the number of items to be read in by the
program. Define an array of doubles with that number of items,
then prompt the user to enter in that many doubles. Then
calculate the average of that array, and then display all of the
entries in the array that are larger than that average. This is a
partnered lab, and worth 3 points.
Write a method called doublearray
that takes in an array of integers as a parameter, creates a new array
twice the length of that array, and then copies each element into the
new array twice such that if the original array is {1,2,3,4} the new
one is {1,1,2,2,3,3,4,4}. When you have doubled the array return
the new one via a return statement. The method declaration
(signature) should look like this:
public static int[]
doublearray(int [] foo)
This is a partnered lab, and worth 3 points.
Do the problems
here
on paper. This is an individual lab,
and worth 4 points.