CS 140- Lab 2 Instructions

We will do this lab in class on Monday Jan 28. Please read the lab before class.

Objectives

  1. Learn how to declare and use simple objects.
  2. Learn how to write simple methods.

Set Up

  1. One or two students should work at one workstation.
  2. In one student's account follow the instructions in the next section. To help choose whose account to use, pick the student who didn't log in last time. The student who is logged in is the "driver" while the non-driver should actively help by observing, making suggestions, and dictating code. You should trade roles each new open lab session.

Method

For the purposes of this lab, write all of your program in a .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.

Part I - Experimenting with types

  1. Why do the statements System.out.println(1/2); and System.out.println(1.0/2.0); print different values? What are the values displayed?
  2. Imagine you just won the lottery. The jackpot was $5 million, but there were a total of 3 winners (including yourself), so you only get 1/3 of the total jackpot. On top of that, the U.S. Government plans to take 1/2 of your winnings as tax. Write a program (you can add it to the 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.

Part II - Experimenting with Turtles in the Interpreter

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

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):

Additionally, you can always print information about your turtle with the command:

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.

Part III - Writing Simple Methods

  1. In DrJava, open the Turtle.java class located in the book's class directory that we set on Monday. This file should be at P:\cs140\intro-prog-java\bookClasses\Turtle.java if you're using one of the lab machines.
  2. Question 13:At the bottom of the file before the last closing brace "}", 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:

    (Hint: a star has 5 sides who are each turned 144 degrees from each other.)
  3. Extra credit question: Write a method to draw a 7 pointed star, like this:

© Rich Sharp (modified by Ed Harcourt)