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.
greeter
. Make a
class Greeter
(with an uppercase G). Double-click on it, erase
its contents and paste in the contents of this
file. Don't look at it—we'll have a close look in a week or so.
Just hit Compile and Close. new Greeter()
from
the menu. Click Ok on the dialog. What
happens?greeter1
. What two methods do you get in the middle of the
menu?sayHello
. What
happens?string1
. You'll get lots of methods. Call the
length
method. What does it
return?toUpperCase
method. What
does it return?replaceAll
method. It wants parameters. Supply two
parameters so that the method will return "Helloo Woorld!"
.
What parameters did you supply? (Be careful
about quotation marks.)And it's nice that you can explore objects without having to
write a whole program with public static void main
and all
that.
Here is another way in which you can explore objects in BlueJ. Select View -> Show Code Pad. In the bottom right corner, you get a window into which you can type instructions. That window is pretty small. Drag the borders to make it bigger.
Type in
string1.length()
What happens?
toUpperCase
on string1
in the code pad.
What did you do? What was the result?string1
. What result do you get? Why isn't it
uppercase?sayGoodbye
method do?Greeter
class (tan rectangle on top), right-click and pick the new
Greeter(String)
constructor call. Supply your name as the
construction parameter. Remember the quotation marks...Rectangle
class of the standard library, but it is a bit
unsatisfactory that one can't actually see the rectangles. In this set of
exercises, you will work with a modification of the Rectangle
class that shows the rectangles in a window. Make a new project
rectangles
. Make a class VisibleRectangle
(with
uppercase V and R). Double-click on it, erase all code, and paste in this code. Don't look at the code—it
contains advanced magic that you won't understand until the end of this
semester. VisibleRectangle
class and construct
a new VisibleRectangle(5, 10, 20, 30)
. What happens?How did you construct the second rectangle?
translate
method on the first rectangle with
parameters 100 and 50. What happens?grow
method on the first rectangle with parameters
50 and 25. What happens?Add this statement below the other import
statement in
Scene.java
:
import org.lgna.story.resources.prop.CakeResource;
Add these statements to the end of myFirstMethod
:
Prop cake = new Prop(CakeResource.BIRTHDAY_CAKE_MATERIALS); cake.setVehicle(this); cake.setPositionRelativeToVehicle(new Position(2, 0, 2));
Run your program. What happpens?
cake.move
. Watch how NetBeans gives you a list of
all move
methods. This feature is called
autocompletion. The first one, move(MoveDirection, Number,
Detail...)
is the one that you want. Keep typing
cake.move(MoveDirection.
and watch some choices for move
directions. FORWARD
sounds good. For the number, try 1. The
details are optional. Don't supply any. What
happens?
In the NetBeans editor, you hit Ctrl+Space to do autocompletion.
resize
method instead.
Take a screen capture of your supersized birthday cake and add it to the lab
report.