Copyright © Cay S. Horstmann 2009, 2012
This work is licensed under a Creative Commons
Attribution-Noncommercial-Share Alike 3.0 United States License.
See lab 1 for reporting instructions.
Problems with this icon are optional
Write Java code that computes a
variable
double distance
from variables x1
and x2
. Use variables dx
and dy
,
and do not use Math.pow
.
Distance
with your expression and run
it with test cases (x1, y1) = (0, 0) and (x2, y2) = (30, 40). What
output do you get?Use this formula to write a program Distance2
that
computes the distance between San Jose and San Francisco, keeping in
mind the following:
Math.acos
.What is your program?
if (str.length()
<= 3) return ...; else return ...;
Paste your answers into the lab report.
The Exploratorium has a nifty exhibit showing that, according to research at Cambrigde Uinversity, our brain tolerates scrmabled letters in a word when reading text.
The following exercises explore this scrambling.
StringScrambler
class so that
the scramble
method returns a word that is made up of
word
word
word
For example, if the input is received
, the output
should be
deceiver
Hint: Call substring
three times and concatenate (+)
the results. What is your program?
StringScrambler
in the BlueJ workbench. Then call the
scramble
method with an input of resigned
.
What output do you get? scrambleSentence
with a sentence of your choice
such as "Java programming is easy and enjoyable"
. Just run
it—it calls your scramble
method for each word in
the sentence.
What result do you get?
scramble
method so that it scrambles the second and third
letter of each word. For example, "word"
should get
changed to "wrod"
.
What is your scramble
method now?
scrambleSentence
again with the same sentence as in
step 4. What is the result?i
and i + 1
where i =
randomInt(1, ...)
.
The randomInt
method has been provided for your
convenience.
What is your scramble
method now?
scrambleSentence
again with the same sentence as in
step 4. What is the result?What is your scramble
method now?
scrambleSentence
again with the
same sentence as in step 4. What is the result?Unzip the file and move the car
directory in a folder
sjsu/cs46a/lab5
inside your home directory. (Don't
overwrite your old lab project.) Open the project in Netbeans. Rename
it to car5
so you don't confuse it with the one from Lab
3.
Car
with a
drive
method. But this time, your method should have no
parameters.
public class Car extends RedRover { public void drive() { move(MoveDirection.FORWARD, ...); turn(TurnDirection.LEFT, ...); } }
Move the car by two units, then turn it by a random angle (which in
Alice is measured between 0 and 1, not 0/360 or 0/2π). Hint:
Math.random()
.
Be sure to use a named constant for the distance.
What is the code of your drive
method?
myFirstMethod
in the
Scene
class.
Car myRover = new Car(50); myRover.setVehicle(this); // Needed to make the car appear in the scene for (int i = 1; i <= 20; i++) { car1.drive(); }
We'll discuss the loop in chapter 6—it means “run the next statement 20 times”. Just copy/paste it.
What happens when you run the program?
Prop bottle = new Prop(ColaBottleResource.COLA_BOTTLE); bottle.setPositionRelativeToVehicle(getPositionRelativeToVehicle()); bottle.setVehicle(getVehicle());
Add these lines to the drive
method. Netbeans should
complain about not knowing ColaBottleResource
. Use
Ctrl+Space after that word, or click on the warning icon, to get a list
of possible completions/actions. Choose import
org.lgna.story.resources.prop.ColaBottleResource
. Compile and
run. What happens?
myFirstM
ethod
, drop a cola bottle
before the car starts moving. Note that you are no longer in a
Car
method, so you need to call
myRover.getPositionRelativeToVehicle()
instead of
getPositionRelativeToVehicle()
etc.
Mercifully, you don't need to use the Pythagorean theorem in Alice
to compute distances. After the 100 steps, compute
myRover.getDistanceTo(bottle)
, where bottle
was the initial cola bottle. Have the initial bottle say that distance.
(Why not the car? It might be off-screen by now.)
Note that the say
method takes a String
parameter, not a double
.
What is the code of your run
method?