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 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.
![]() | 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. |
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.
![]() |
public class HelloWorld { }
|
![]() |
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.
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 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.
![]() | 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.
![]() | Do 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.
![]() | Click on the lightbulb. |
Eclipse offers you suggestions on how to fix the problem.
![]() | Select "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.
![]() |
|
![]() |
MultiplyTwoNumbers.java is the second half of the coursework for this
exercise 3
|
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.
![]() |
|
So what can we do that we could not do with 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.
|
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?