Copyright © Cay S. Horstmann 2009 
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
wordwordwordFor 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?
Make a new Alice project car2 in Netbeans. Load
car.a3p and be sure that your project is called
car2 so that you don't overwrite your old lab project.
MyCar class, implement a method
public void drive()
{
move(MoveDirection.FORWARD, ..., ...);
turn(TurnDirection.LEFT, ..., ...);
}
that drives the car by two units, then turns 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. The
move and turn methods have a third parameter
to give the duration of the animation. Set the duration to
SHORT, a constant that you should define in the method and
set to 0.05.
What is the code of your drive method?
MyScene.run method:
for (int i = 1; i <= 100; i++) car1.drive();
We'll discuss the loop in chapter 6—it means “run the next statement 100 times”. Just copy/paste it.
What happens when you run the program?
Toaster t = new Toaster(); t.setLocalPointOfView(getLocalPointOfView()); getScene().addComponent(t);
Add these lines to the drive method. Netbeans should
complain about not knowing Toaster. Ctrl+Space tells NetBeans to
“cheat” and give you a list of possible
completions/actions. Try it out to get a feel for it: Type Toaster,
then Ctrl+Space, or perhaps Toaste, then Ctrl+Space.
Caution: This feature is addictive, and once you are
hooked, you'll have a hard time going back to BlueJ. (If you can't
figure out how to use Ctrl+Space, import the package
org.alice.apis.moveandturn.gallery.kitchen.Toaster, but be
sure to have someone else show you later.) Compile and run. What
happens?
MyScene.run method, drop a toaster before
the car starts moving. Note that you are no longer in a
MyCar method, so you need to call
car1.getScene() instead of getScene etc.
Mercifully, you don't need to use the Pythagorean theorem in Alice
to compute distances. After the 100 steps, compute
car1.getDistanceTo(t), where t was the
initial toaster. Have the car say that distance. (Note that the
say method takes a String parameter, not a
double.)
What is the code of your run method?