The purpose of this exercise is:
|
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.
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. |
|
So, what advantages do you get from netbeans rather than BlueJ?
Hexagon h = new Hexagon();
// 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( );
badlyIndented.
|
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.
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. |
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. |
You should end up with HelloWorld.java file. This will be half of the
assessment for this exercise 2.
|
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.
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.
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:
|
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.
|
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?