Lecture 4 (Sherriff + Tychonievich) - Intro to Eclipse

Lecture Date: Wednesday, January 21

We might not cover all of this today - but we definitely will over the next week. I’m keeping all the info together to make it easier to find.

What is Eclipse?

  • Eclipse is to coding as Word is to writing a paper
  • Eclipse is an integrated development environment (IDE) - that means it has lots of tools in it to make coding easier
  • Eclipse is open-source software - this means that no one “owns” Eclipse and that many people around the world all help to write it’s code together (nominally, it’s main supporter is IBM)
  • Eclipse is written in Java and can actually do some prediction as to what you’re about to type

Starting up Eclipse

  • Starting up Eclipse brings up the Workspace - this is where all your projects will be stored
  • Think of a project as each new assignment - all code that “goes together” should go in one project - much like if you put all your papers on one particular topic in the same folder
  • You can have multiple workspaces - I tend to have a separate workspace for each class I teach, and then in each project I have each of the different assignments or labs
  • Thus, you all should probably only have one workspace - maybe two if you want to have one for class and one for lab, but one is easier to manage
  • Put your workspace somewhere you can find it - normally it goes under your documents folder and is simply called “workspace”
  • Let’s look in the workspace right now and see what’s there - metadata contains the settings for your workspace
  • When you’ve chosen your workspace in Eclipse, the welcome screen or the main window should open

Inside Eclipse

A few main windows to note here:

  • Package Explorer - this shows you all your projects and files
  • Editor - this is the blank thing in the middle
  • Problems - this window at the bottom will let you know about coding mistakes you’ve made
  • Outline - just like in a paper, code can be split up in such a way that having an outline (or table of contents) makes it easier to find your way around

This layout is called the Java Perspective and contains everything you need to code in Java. Eclipse has quite a few perspectives, but we can look at them later. One thing to note - if you close any of these windows, you can find them under Window -> Show View (each of these windows is called a View)

Your First Program

  • When you start a new program, start by making a new project: File -> New… -> Java Project
  • Give the project a name (like Hello World), then click Finish
  • Notice that Hello World has appeared in your Package Explorer. Click on the arrow beside Hello World.
  • You should have two things here - a folder called src and the JRE System Library.
  • src is short for “source.” Code that you write is called “source code,” because it’s the origin of your program. We put all our code in the src folder so that Eclipse knows that it’s part of our program
  • The JRE System Library contains a whole bunch of pre-written code that we can take advantage of. Think of it like Legos or other building blocks - you want to build your own thing, but sometimes, it’s nice to have some pre-made pieces that do exactly what you want. That’s what the JRE Library is for. We’ll be using it a lot.

Your First Class

  • In Java, we put all of our code into “classes.” In the most basic terms (and actually in nearly every program I write) there is exactly one class in each Java file we’ll make and the name of the class is the same as the name of the file. Here, we’re writing a Hello World program, so we should make a class called HelloWorld.
  • Notice what I did there? I combined the words into one, funky looking word. Java doesn’t handle spaces well (actually, at all) so we need to concatenate words together when we want them to stay together. We use what’s called “camel case” to differentiate the words (notes the W is the “hump” in the word :-) ).
  • Right click on the src folder and choose New -> Class.
  • In the name field, call your class HelloWorld, then check the box that says “public static void main (String[] args)” (I realize that looks scary, but trust me, we’ll show you what it does!). Then click Finish.
  • You now have a Java program! It doesn’t currently do anything though…

Hello World

http://en.wikipedia.org/wiki/Hello_world_program

“While small test programs existed since the development of programmable computers, the tradition of using the phrase "Hello world!” as a test message was influenced by an example program in the seminal book The C Programming Language. The example program from that book prints “hello, world” (without capital letters or exclamation mark), and was inherited from a 1974 Bell Laboratories internal memorandum by Brian Kernighan, Programming in C: A Tutorial, which contains the first known version.“ Hello World is often the first program written for any language or any new platform.

The Parts of a Program

(this can mainly be found in your text)

HelloWorld.java
1
2
3
4
5
6
7
8
9
10
11
public class HelloWorld {

  /**
  * @param args
  */
  public static void main(String[] args) {
      // TODO Auto-generated method stub

  }

}

Can you find the class statement? The comments? The main method? A block of code?

Adding to Hello World

In place of the TODO statement, put the following:

HelloWorld.java
1
System.out.println("Hello World!");

Notice that there’s an asterisk now beside the file name in the Editor view. That means you need to save your file. Click the disk icon or Ctrl-S (or Command-S for Mac)

Now let’s edit the comment block. You need to let everyone know you’re the one that wrote this code! In between the /* and the / put whatever you want to identify yourself.

Save the file again.

Now it’s time to run your program! You should see what looks like a green play button in the main toolbar in the program. It’s called Run. It will run whatever program file is currently highlighted. Click on it now to run your first program!

We’re not doing any fancy graphics yet, so the program runs in what is called the “console window.” Think of this just like a command prompt or a DOS prompt (if that means anything to you). Before Windows was created (or any other graphical user interface (GUI)) programs ran as plain text. And that’s how we’re starting out in this class. It’s simpler for now, trust me. I know it doesn’t look as cool, but we’ll do some graphics stuff later in the semester.

Dragging and Dropping Files

Next, we’ll learn how to bring in files that others have written. For next week, we’ll be drawing pictures with a turtle (trust me, it’s more fun than it sounds).

Download these two files to your machine (Right-click -> Save As…):

You can actually just drag and drop files from your window explorer into the src directory in Eclipse!

If we have time…

  • Looking at a project
  • Importing and Exporting projects
  • Taking this Hello World to the next level
  • More things to do in Eclipse!

Sherriff:

Tychonievich: