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 directly.

exclaim One thing to bear in mind; unlike BlueJ, netbeans 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.
act
  1. Netbeans 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. 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. If you try this at home, you will need to install the BlueJ project plugin to get it to work.
  3. Open up Circle.java by double-clicking on it. It should now look like this. You should recognise the code from last week.
  4. 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 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.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 some reason this does not work out-of-the-box. Try this:

act
  • Tools->Java Platform
  • Select Javadoc->Add ZIP/Folder. The javadoc should be in c:\Program Files\CS\Java\jdk-xxx-docs.zip (the name might be slightly different).
  • 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 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