CS 1110/1111: Introduction to Programming

Lecture 5

Announcements

More than 15 people still want to switch labs. If you can switch, go to Piazza and make someone's day.

Request Tracker: it will be a few days before we get to these requests.

Talking Points

Today we'll start previewing most of Java. We'll go fast. We won't go into much detail. Next week we'll come back and start walking through each piece in detail.

Getting set up

  1. Open Eclipse. Remember, it MUST NOT be removed from the folder it came in, thoguh the whole folder may be moved.

  2. If it asks you about a workspace, pick the folder where you'd like all the files we write this semester to be located.

  3. In the welcome page there's a button labeled Workbench on the right. Click it.

  4. Select File → New → Java Project (you might need to select just Project and then select Java Project on the next screen

  5. Type the project name as TurtleDemo (or something else if you want; I'll assume TurtleDemo)

  6. Leave everything else in its default state and press the Finish button

  7. If it asks about the Java perspective check remember my decision and click Yes

  8. Click the little plus sign next to the project name:

    You should now see a folder called src; we'll be putting files in that.

  9. Download Turtle.java and World.java

  10. Open your operating system's file viewer to where you downloaded the .java files

  11. Drag Turtle.java and World.java from your file viewer onto the src folder icon in Eclipse. If it asks, chose copy rather than link. You should now see something like the following:

    If you do not, click + icons and/or drag files around until you do.

  12. Select File → New → Class

  13. Type Demo1 in the Name field and check the box next to public static void main(String[] args), then click Finish

The end result of this should be a window where you can type code, opening with public class Demo1.

We'll walk through the following code:

import java.awt.Color;

public class TurtleTest {
    public static void main(String[] args) {
        World cruton = new World(800, 500, Color.WHITE);
        Turtle sasha = new Turtle(cruton, -50, 100);
        sasha.forward(200);
        sasha.right(76);
        sasha.forward(100);
        sasha.right(76);
        sasha.backward(50);
        sasha.goTo(0,0);
    }
}

Things you can tell a world

CommandAction
World w = new World();Makes a new 600-by-600 pixel world
World w = new World(w, h, Color.RED);Makes a new w-by-h pixel world with a red background
w.clearGround();Erases all turtle trails
w.saveAs("someImageName.png");Saves the current image in your project folder (path/to/your/eclipse/workspace/TurtleDemo/someImageName.png)

Things you can tell a turtle

CommandAction
Turtle t = new Turtle(someWorld);Makes a new turtle in the center of the world
Turtle t = new Turtle(wrld, x, y);Makes a new turtle at the specified point of the world
t.forward(100);t moves forward 100 pixels
t.backward(83);t moves backward 83 pixels
t.left(47);t rotates to its left (counter-clockwise) 47°
t.right(306);t rotates to its right (clockwise) 306°
t.setColor(Color.RED);t becomes a red turtle.
t.pickPenUp();t stops leaving trails as it moves.
t.setShellSize(200);t becomes a very large turtle
t.setPenWidth(20);trails t leaves are now 20-pixels wide
t.dropPicture("http://www.cs.virginia.edu/cs1110/logo.png", 30);t draws a picture about 30 pixels wide where it is standing

There are a few other methods too, like t.putPenDown() and get methods for most of the set methods.

Things we'll discuss

Draw a house

Draw a circle

Write a method to draw squares of any size

Write a method to draw polygons of any number of sides

Write a method to draw fractal trees

Copyright © 2014 by Luther Tychonievich. All rights reserved.