Top: Index Previous: Classes and Objects Up: Index Next: Loops and Refactoring

CSC8317 -- Introductory Programming for Bioinformatics

Hello and Resources

The purpose of this exercise is:

exclaim
  • For you to gain familiarity with Eclipse.
  • To understand the resources and documentation for Java that are available to you.
  • To write "Hello World" and read some input.

Eclipse and IDEs

BlueJ is a nice environment. You can see your classes, objects and the relationships between them. It's a good place to get started. It has two critical flaws though:

So, in this practical, we are going to switch to Eclipse. This is a fully-fledged Integrated Development Environment (IDE). It's a practical tool for the rest of the module. It's not the only IDE on the market; actually, there are many, although it or Netbeans are probably the most popular for Java. Eclipse is free and open source, so you can download it and install it on your own machine without limitations.

Choice of IDE tends to be a bit of a religious experience 1. If you have programmed before and prefer another IDE, then you are not required to use Eclipse for these practicals. However, the screenshots for these webpages are all from Eclipse and the demonstrators may be less able to help you if you get into problems with the IDE itself. I used to use Netbeans for these practicals, but it seemed to not work as well with the network at Newcastle.

exclaim One thing to bear in mind; unlike BlueJ, Eclipse is a tool for the professional programmer. It has an enormous number of menu items, options and buttons. This can be a bit overwhelming at first. Please don't worry. For most of this module, you will only need to use a few of these and you will get to learn them pretty quickly.

Hello World

Next, we are going to write a HelloWorld program. This is a very traditional program to write when learning a new language. It just prints "Hello World" out to screen.

These vary widely between different languages. A simple one might look like this:

 print "Hello World";

There are many more complex ones. For instance, the Java compiler will turn your HelloWorld program into this. Windows and your computer will change it into something a bit closer to this.

act
  1. Eclipse should already been installed on your machines. If you want to install it on your own machine, you should be able to find it on the web with little effort.
  2. Now we are ready to start; you should be able to find Eclipse in the start menu on Windows.
  3. Eclipse uses workspaces. It will ask you where you want your workspace to be. Put this somewhere safe.
  4. Click on the "Workspace" button, top right to get rid of the splash screen
  5. As well as workspaces, Eclipse uses projects. File->New->Project->Java Project. Name the project "HelloWorld". Click on "Finish"
  6. Now we create a new Java class. File->New->Class. Name the class "HelloWorld".
  7. You should now have a pane called "HelloWorld.java" which contains
public class HelloWorld {

}
  1. We need to add statements into this class to make it print "Hello World". Use the internet to find the right statements for Java, and put them into this class.
  2. To check that it works run the class. Project->Run. Or Ctrl+F11 if you like keyboards. If you get a dialog that you don't understand, you have probably got things wrong. Try again, or ask the demonstrator.
log You should end up with HelloWorld.java file. This will be half of the assessment for this exercise 2.

Do not under any circumstances move on until you are sure that you have HelloWorld working correctly. If it doesn't work then nothing else will either.

Asking the User for some information.

Printing "Hello World" is pretty dull. So, we are going to try and do something where the program does not do the same thing every time. In this case, we are going to multiple two numbers together.

act Start a new project in Eclipse, with a main class called MultipleTwoNumbers.java. You should know how to add a main method to this class now, because you did this with HelloWorld

For this we are going to use the Scanner class. Java has lots of documentation about its classes, which is very useful; it is generated from source code using a tool called javadoc, and this has become the name of this style of documentation.

act Find the Javadoc for the Scanner class on the web. You are using Java 1.7; make sure that you get the right version, as there are quite a few version of Java around.

This is a little bit painful. There are easier ways to access Javadoc. If you have found the documentation then you will probably realise that to start off you need add

Scanner sc = new Scanner( System.in );

to your main method.

actDo this now.

You should find "Scanner" is underlined with a red squiggle, and there is a little lightbulb with a cross on it. These indicate a problem with your code.

actClick on the lightbulb.

Eclipse offers you suggestions on how to fix the problem.

actSelect "import Scanner(java.util)"

If you hover over the word "Scanner" you should get a pop up, showing the same Javadoc you saw earlier. If you click around, you should be able to pop this up in Eclipse's own internal browser.

You should now nearly be in a position to write a piece of code that asks the user for two integers and multiply them together. You know how to print things for the user to read, you know how to read input from the user. You should be able to work out how to do multiplication for yourself.

quest
  • As well as multiplication, what other operators does Java support?
  • The basic operators are a bit limited and you might need more. Read this link if you want more.
log MultiplyTwoNumbers.java is the second half of the coursework for this exercise 3

Importing Shapes

Finally, let's try importing the Shapes package that we wrote with BlueJ. This will allow us to see some of the other advantages to Eclipse.

act
  1. Start another project called "Shapes".
  2. Right click on the "Shapes" icon in "Project Explorer".
  3. General->File System
  4. Use the Browse button to choose your shapes directory.
  5. Select the Java files (Canvas, Circle, Square, Triangle, Colour and your own Automate)
  6. Click "Into Folder" and choose "src". Then "Finish"

So what can we do that we could not do with BlueJ.

act
  1. Click on Circle.java, then Circle which appears underneath. Click this and you should see a set of methods. Clicking these will take you straight to the method definition.
  2. Go to Automate.java. Put your cursor immediately after a curly-brace (a "{" or "}" character). Eclipse will highlight both this and the matching brace.
  3. Place the cursor after the last line of your code, and hit "Return". Eclipse will indent your code to the right place.
  4. Type in this code:
   Hexagon h = new Hexagon();
  1. This code is wrong; there is no Hexagon class in the shapes package. Eclipse will underline it for you. There will be a small icon in the left margin; this will offer suggests as to how you could fix this error.
  2. Add this code, after the opening brace of the main method.
              // this code has been indented my a maniac
Circle badlyIndented = new Circle();
             // who also likes unnecessary spaces.
             badlyIndented.            makeVisible();

// and really pointless bracket placement
    badlyIndented.moveLeft(

);
  1. Do Source->Format, or Shift-Ctrl-F (which does the same thing). The code will be made neat.
  2. Type the following and then wait for a second.
     badlyIndented.
  1. Eclipse will offer you code completion — all the methods that you can use. A second later, you will get documentation. Use up and down arrows to select the method you want. Having lots of windows pop up can be a bit disconcerting. You will get used to it, and it's very useful.
  2. Finally, hit [Ctrl + F11] which will run the class with main in it (that's Automate.java in this case). You should get a picture of a house.

1. I use Emacs. This is a bit more of a supercharged text editor rather than an IDE. It's very good, but I probably wouldn't suggest it as a first place to go; it can be quite hard to use. It's not as good for Java as Eclipse but you can use it for everything (including writing this web page). Choice of IDE or Editor is religious among programmers.

2. Is your code well-commented and documented?

3. Did I mention at any point that you will be assessed on your code style, and the documentation?


Top: Index Previous: Classes and Objects Up: Index Next: Loops and Refactoring