All exercises are due by *midnight* Friday. See below for the turn-in procedure.

In this lab, we will familiarize ourselves with Eclipse. Eclipse is an Integrated Development Environment (IDE) for Java.

# Part 0: Our first Java project

We will start out by creating a simple project to make sure we can properly build and run a Java program. When Eclipse launches, it will ask you for a workspace. The workspace is basically a group of related projects. You can create multiple workspaces, for example, for each class you take. By default, Eclipse will propose a default workspace to use. You can use that, or change it, as you prefer.

When first loaded, it will present an intro screen. You can close it by clicking the X close button.

Let’s now create a new project by selecting File > New Project …. Create a new Java project, that we will call Hello World.

Now let’s add a new class HelloWorld. This time, we will right-click on the project. In series of dialogs that Eclipse shows you, check the box to create a `main()`

method. Checking this box will create a skeleton for the main entry point for the program, just as you have seen in C. In Java, it looks like this:

`public static void main(String args[]) { ... }`

Let’s add the following line to the main method:

`System.out.println("Hello, world!");`

Now, you should be able to hit the green “play” button in the toolbar to compile and run this program. If all is well, you’ve got a running Eclipse setup. We can then do something a little more fun.

# Part 1: Prime Numbers

## Exercise 1: Numbers

Write a class `Numbers`

with a main method that prints the numbers from 1 to 100.

## Exercise 2: Prime numbers

A naïve method for determining if a number *n* is prime is checking that all the numbers smaller than the square root of *n* are not divisors of *n*.

Add a

*static*method`static boolean isPrime(long n)`

that implements this algorithm in the class`Numbers`

. You might need to use the method`double Math.sqrt(double n)`

. A*static*method is a method on a class, that does not depend on an instance of that class (such as`Math.sqrt(double)`

).Modify

`Numbers`

so that it prints all the prime numbers smaller than 100.

# Part 2: Turtle graphics

In 1967, W. Feurzeig and S. Papert created a programming language called *Logo* to teach programming. It also inspired the examples we saw in class.

You are going to use a Java packaged called `TurtleGraphics`

to make some pictures using Java. First, download the TurtleGraphics jar. A JAR file contains pre-compiled Java classes and is basically a library. Add it to the project by right-clicking on the project and choosing `Build Path`

> `Add External Archives...`

. Once added to the project, it is possible to use the classes defined within.

The package `TurtleGraphics`

contains two classes:

`class Sheet`

implements a graphical window that we can draw into. It’s constructor looks like this:`Sheet(int width, int height)`

where`width`

and`height`

are the size of the window in pixels.

`class Turtle`

`public Turtle(Sheet sheet)`

, the constructor, creates a new Turtle on the given`Sheet`

.`void turn(double degrees)`

`void advance(double steps)`

`void penDown()`

and`void penUp()`

lower and raise the pen.`void setPenColor(Color color)`

changes the color of the pen.

For example, the following program draws a Triangle:

```
import java.awt.Color;
import TurtleGraphics.Sheet;
import TurtleGraphics.Turtle;
public class AdvancedTurtle extends Turtle {
public AdvancedTurtle(Sheet sheet) {
super(sheet);
}
public void drawTriangle(double side) {
penDown();
advance(side);
turn(120);
advance(side);
turn(120);
advance(side);
penUp();
}
public static void main(String args[]) {
Sheet sheet = new Sheet(300, 300);
AdvancedTurtle turtle = new AdvancedTurtle(sheet);
turtle.drawTriangle();
}
}
```

## Exercise 3: Squares

Add a method `drawSquare`

to `AvancedTurtle`

to draw a square of a given size.

## Exercise 4: Circles

Add a method `drawCircle`

. Hint: one way to draw a circle is to take 360 steps of distance π × radius ÷ 180.

## Exercise 5: Snowflakes

In 1904, Helge von Koch described the fractal curve obtained by the following algorithm:

- Take a segment.
- Divide it into four new segments of equal length, where the two internal segments form two sides of a triangle:
![Koch segment](images/koch.png) - For each segment, repeat the same procedure recursively to a certain depth.

In this exercise, write a method `koch(int n, double size)`

that draws a Koch’s segment with an initial side length of `size`

and repeats up to `n`

iterations.

Next, add a method `snowFlake(int n, double length)`

that draws a Koch’s snowflake by applying Koch’s algorithm to the three sides of an equilateral triangle.

# Part 3: Back to prime numbers

Eratosthenes was a greek mathematician, geographer, and astronomer who used the elevation of the sun to calculate what we believe to be the first measure of the Earth’s circumference. He also produced an efficient method to compute prime numbers.

### Eratosthenes’ algorithm:

- Make a list of numbers from 2 to
*n*and call it*numbers*. - Take an empty list and call it
*primes*. - Take the smallest number,
*k*, in the list of*numbers*. - Add
*k*to the list of*primes*. - If
*k*is bigger than^{2}*n*, stop and append all remaining*numbers*to*primes*. - Remove all multiples of
*k*from*numbers*. - Go back to step 3.

## Exercise 6

Implement this algorithm in Java. Here is one way you might do it:

- Create two arrays:
`numbers`

of*n-1*`boolean`

s.`primes`

of*n-1*`long`

s.

`numbers`

is a boolean array, where we can use`true`

and`false`

to indicate whether a number is still in the array or not.`primes`

stores the primes when we find them.

# Hand-in

- Select your projects in Eclipse.
- Right-click and select
`Export...`

- Choose
`General > File System`

. - Export the project to a folder named
*your-last-name*-lab1 - Create a .zip of this folder.
- Upload the zip to the lab1 submission site.

Assignment by Pablo de Oliveira Castro, licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.