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

CSC8303 -- Bioinformatics Programming in Java

Hello and Resources

The purpose of this exercise is:

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

Netbeans 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 Netbeans. 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 Eclipse are probably the most popular for Java 1.

I've chosen Netbeans for two reasons: first, it's free to download and install, so if you want to, you can put it on your own machines; second, it has a BlueJ plugin, so we can look at the Shapes package, then you can do.

exclaim One thing to bear in mind; unlike BlueJ, netbeans is a tool for the professional programmer. It has an enourmous 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.
act
  1. Find a netbeans installer on the web. You want Netbeans 6.1 and for JavaSE (standard edition).
  2. Install it. Next, we need to fiddle with Netbeans to get it working on the University network.
    1. Find the File c:\Program Files\NetBeans 6.1\etc\netbeans.conf.
    2. Right Click, Open With->Wordpad.
    3. Change the line starting with netbeans_default_userdir to netbeans_default_userdir="h:/netbeans/6.1"
  3. Now start it! Don't bother registering. You need to be a little patient as it takes a while the first time it's run. It will offer you updates, which you should accept. Once it restarts it should look like this.
  4. Now, we are going to install the bluej plugin. Tools->Plugins->Available Plugins, and select "BlueJ project support" from the list. Then "Install".
  5. Now we are ready to start. Let's open the Shapes project. File->Open Project and navigate to the directory you created last time. Your IDE should now look like this.
  6. Open up Circle.java by double-clicking on it. It should now look like this. You should recognise the code from last week.
  7. Open up Automate.java. Hit Shift-F6. This will run the program — you should get a picture of a house popping up.

So, what advantages do you get from netbeans rather than BlueJ?

act
  1. In BlueJ, you could see a set of methods for each object by right-clicking. Find them on the netbeans interface. If you click on the method, it should take you to the code.
  2. Go to Automate.java. Put your cursor immediately after a curly-brace (a "{" or "}" character). Netbeans will highlight both this and the matching brace.
  3. Place the cursor after the last line of your code, and hit "Return". Netbeans 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. Netbeans 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 Alt-Shift-F (which does the same thing). The code will be made neat, with the exception of the comment — Netbeans won't change comments.
  2. Type the following and then wait for a second.
     badlyIndented.
  1. Netbeans 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.
  2. Finally, hit [F6] which will run the class with main in it (that's Automate.java in this case). You should get a picture of a house.

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 "HelloWorld" 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

The Java tutorial runs through creating HelloWorld; try the instructions here. You can go straight to "Creating Your First Application" as you've done the rest already. There is a line-by-line description of what is all does which you can read if you wish.

exclaim The Java tutorial version is actually out-of-date and for a previous version of Netbeans. I've used it because you should get used to coping with out-of-date documentation; computers change fast. There is a newer (and slightly more complex) version here that you can use if you need.
log You should end up with HelloWorld.java file. This will be half of the assessment for this exercise 2.

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 netbeans, with a main class called "MultipleTwoNumbers.java".

For this we are going to use the Scanner class. Java has lots of documentation about it's 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.6; make sure that you get the right version, as there are quite a few version of Java around.

This is kind of irritating; looking up Javadoc in this way is not that convenient. What would be really nice is for Netbeans to do it for you. For something reason this does not work out-of-the-box. Try this:

act
  • Tools->Plugins
  • Available Plugins
  • Select Java SE 6 API Doc English, and then Install.
  • In English this means "Java Standard Edition, Version 6, Application Programming Interface Documentation". Java 6 is also known as Java 1.6. Same thing. Don't worry, don't ask, it just is.
  • Help->Javadoc Index Search and type in "Scanner".
  • This is still not easy enough. Move to MultiplyTwoNumbers.java. Type in Scanner. Fix the error that Netbeans recognises for you. Then right-click on Scanner and choose "Javadoc".
  • Read the first part of the documentation, until you find the code that will read a number in.

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

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's can be quite hard to use. It's not as good for Java as Netbeans 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