We will do this lab in class on Monday Jan 28. Please read the lab before class.
.java
file like we did on the first day. Go to the file menu, choose new, and create
the file in your CS140 folder on the P: drive.
In the definitions pane
(that's the largest window in the DrJava editor) start a
skeleton class with the following:
public class Lab2 { public static void main(String[] args) { // put your code here } }
Notice the //put your code here
line.
Any line in Java that starts with a //
is
a comment and is ignored by the Java compiler. Comments are
useful for documenting parts of your code that could be confusing for
somebody else to read, or even for yourself.
For the following questions you can write your Java code in the main method of Lab2 class. This way you can save the code you wrote in class without having to re-type it in the interactions pane.
System.out.println(1/2);
and
System.out.println(1.0/2.0);
print different values?
What are the values displayed?
Lab2
class above, or make a new
class if you like) to calculate how much money, in millions, you
will have after the jackpot is divided and taxes are taken out.
Display your result so it looks something like the following:
After the jackpot is divided and taxes are taken out, you are left with 0.2 million dollars.
Your version should display the correct answer, which is not 0.2 million.
This part of the lab assumes that you have correctly set up the classpath to the book's Java classes as completed in class on Monday. If you haven't, go back and complete Lab 1 to correctly update the classpath (otherwise nothing will work).
Question 3: Briefly describe the purpose of a Java classpath:
This section will introduce you to the concept of a Java object. Objects are programming abstractions that encapsulate the behavior of certain logical chunks of a program. They often relate to real world objects (bicycles, cars, cups, etc.), but don't necessarily have to. The two object types you will be working with in this lab are
World
Turtle
The World
and Turtle
classes are not part of the regular Java
distribution but instead have been created by the authors of the book
we are using. Java classes are defined in files (filled with Java code)
that end in the extension ".java".
Question 4:
Where is the file which holds the Turtle
class (Turtle.java
)
located on your P: drive? (Hint: look at your extra classpath settings.)
Usually you will not program with a class directly (unless it
has class methods, like the Math
class) but instead you'll create objects of that class. To create an object,
you'll use the Java new
operator.
If you want to refer back to the object later, you'll need to
store it in a variable. To create a new object and store it
in a variable all at once, you will use a construct that looks like this:
ClassName variableName = new ClassName(parameterList);
Where ClassName
refers to the name of the class, variableName
refers to the name of the variable (which you can choose) and parameterList
refers to a comma separated list of variables that you may or
may not pass to the
class (called the constructor) when you create a new instance.
What you pass in for the parameter list depends on the class. For
example when creating a new world, you don't need to pass anything in
on the parameter list: World w = new World();
This creates a new world that
is
640 by 480 pixels in size. If you wanted to create a World
of a different
size
you'd have to pass in that size in the parameter list. For example, to create an
800x1000
World
you'd pass 800 and 1000 into the World constructor when you
create the
new object: World w = new World(800,1000);
Question 5:
Write a line of Java to create a new World
class of size 600x600 and
store it in a variable named myWorld
:
Question 6: Execute the line you wrote above in the DrJava interactions pane. What does it do?
Now that you have a world, you'll want to populate it with
turtles. In order to do this you'll need to create an object (and a variable to hold it) of
a Turtle
class,
but you'll also need to pass the current world (myWorld) to
the Turtle
constructor as you instantiate it (otherwise the turtle won't
know where to
live). This is the purpose of the parameter list described
above in step 5.
Question 7:
Write a single line of Java code to create a new Turtle
in a
variable named myTurtle
,
and add it to the World
object referenced by the myWorld
variable created above (if you need help, check out the example on page 45 of your book):
Question 8: Execute the line you wrote above. What does it do?
Now that you have a reference to a Turtle
object,
you can send it messages to do things. You do this by calling
object methods from the Turtle
class. The following are the list of turtle methods you can
use. (In the examples below t
is a
reference to a Turtle
object, just like the myTurtle
variable you declared above):
t.forward(distance); //move 't' forward 'distance' pixels
t.backward
(distance); //move 't'
backward 'distance' pixels
t.turnRight(); //turns 't' 90 degrees to the right
t.turnLeft(); //turns 't' 90 degrees to the right
t.turn(deg); //turns 't' 'deg' degrees (if negative to right, if positive to
left)
t.turnToFace(x,y); //turns 't' so it faces the coordinates (x,y)
t.penUp(); //raises the pen so that when the turtle
moves it doesn't leave a line
t.penDown(); // lowers the pen so that when the turtle
moves it leaves a line behind
itself
t.hide(); // hides the turtle on the world screen
t.show(); // shows the turtle on the world screen
t.setName(aName); // sets the name of a turtle to the string aName
Additionally, you can always print information about your turtle with the command:
System.out.println(t);
Where t
is the reference to your Turtle
object.
Question 9:
What is displayed when you print out the value of your myTurtle
reference?
Question 10:
Create a second turtle (you'll need to name it
something other than myTurtle
) and
insert it into the world at coordinates 50,50. Do this by
passing to the Turtle
constructor a comma separated list containing the x coordinate, y
coordinate, and the reference to the World
you wish
it to go to. If you need help, use the example on page 49 of your book.
Write the command you used below:
Question 11: Write a series of Java commands to cause the two turtles in your world to crash into each other (so they exist at the same x/y coordinate). Try to use as few commands as possible. Write the commands you used here:
Question 12: How can you be sure that both of your turtles now lie at exactly the same x/y coordinate? Write the two lines of Java code below you could use to convince yourself.
P:\cs140\intro-prog-java\bookClasses\Turtle.java
if you're using one of the lab machines.
}
", write a method that will cause a
turtle to draw a star with sides of length d
. (This method
should go below your drawRectangle
method you wrote for homework.
If you didn't write the drawRectangle
method for
homework you should do that first).
The skeleton for this method will look like:
public void drawStar(int d) { //write code here }
Once it's complete you should be able to make a turtle
(if its name is t
) draw a 5 pointed star
with lengths 100 with the command: t.drawStar(100)
;
You should get a result like this: