You are on page 1of 7

Monash University FIT2099 Object-oriented Design and Implementation 2020 S1

Week 1 Bootcamp
Welcome to FIT2099

Objectives
In this week’s lab, you will:
• learn how to use an IDE to edit and build a Java project
• learn how to import code into a Java project

Task 1. Accessing the development environment


Almost all commercial Java development is done in an IDE (Integrated Development Environ-
ment). We recommend Eclipse or IntelliJ IDEA1 .
Eclipse is free, open source, and has been around for a long time, but looks a bit dated these
days. The Pro version of IntelliJ IDEA is commercial software, but there is a free open source
Community Edition with a limited set of features.
We will provide instructions for using Eclipse and IntelliJ, but you may use any IDE you like
as long as it can build Java 11. We won’t provide any support for any environment other than
Eclipse and IntelliJ, though — if it breaks, you get to keep both pieces.
Installing Eclipse
Installing Eclipse on your own computer is quite straightforward. Go to the Eclipse website
(http://www.eclipse.org) and follow the instructions.
Installing IntelliJ IDEA
You can download an installer for IntelliJ IDEA at https://www.jetbrains.com/idea/download.
There’s a lot of really good documentation, including video howtos, at https://www.jetbrains.
com/idea/documentation/.
Ensure that you can access a Java development environment before you proceed with this lab
sheet.

Task 2. Hello World! in Java


Next, you will create your first Java program. By tradition, the first program you create in a new
language should print the string “Hello, World!”. We’ll stick with that tradition.
A word about projects
Java programs almost always contain more than one file of source code, and may contain hund-
reds of them. This can be hard to keep track of, especially if you are responsible for maintaining
several programs. IDEs help you deal with this complexity by organizing your code (and other
program assets such as images or data files) into projects. In fact, you can’t even compile and run
a program in Eclipse unless you create a project for it. That’s because, as well as organizing your
source code for you, the project file also keeps track of information such as which system libraries
to use.
1 If you would like to use a different IDE, such as Visual Studio Code or NetBeans, please make sure that you know

how to use it to do all the things this guide shows you. You’ll also be using git to share files later in the semester, so you
may wish to explore your IDE’s git integration.

1
Monash University FIT2099 Object-oriented Design and Implementation 2020 S1

Your project settings determine which JRE (Java Runtime Environment) will be used to build
and run your program. All JRE distributions come with a JVM (Java Virtual Machine). This is
the interpreter that will actually execute your Java instructions – without one, you can’t run a
Java program. They also have a compiler called javac. This is a program that translates your Java
source code into bytecode – instructions that the JVM knows how to run.
We will be using JDK (Java Development Kit) 11. This can be downloaded from (https://www.
oracle.com/java/technologies/javase-jdk11-downloads.html). All JRE distributions come
with both a JVM (Java Virtual Machine), which executes your compiler (javac, which translates
your )We will be using JDK (Java Development Kit)2 11. This can be downloaded from (https:
//www.oracle.com/java/technologies/javase-jdk11-downloads.html)
The Java language itself doesn’t support the notion of a project. This is managed by your IDE,
and different IDEs have different ways of doing it. We’ll show you how to create projects in
Eclipse and IntelliJ; if you’d like to use a different IDE, you’ll need to figure this out for yourself.
Either way, our first step towards creating our first Java program will be to create a new project
to store our source code.
Creating a project in Eclipse
If you’re working in Eclipse, select File → New → Java Project from the menu. A dialog box
will pop up where you can select a name and some other properties for your project.
Enter the name HelloWorld as the name of the project. Ensure that you are using Java 11; if
necessary, select it using the “Use a project specific JRE” option.

The HelloWorld project is then added to your Eclipse Workspace. This is a folder on your shared
drive where all your working source code is stored.
Creating a project in IntelliJ In IntelliJ, you access the New Project wizard by selecting File →
New Project... from the menu. A dialog will pop up that lets you choose what kind of project
to create. Choose Java, and make sure that the Project SDK (Software Development Kit) is set to
Java 11. This image shows you where to look and what to look for.
2A JDK is a JRE that comes with additional programs that make programming easier.

2
Monash University FIT2099 Object-oriented Design and Implementation 2020 S1

If Java 11 doesn’t appear on the dropdown list of SDKs, you should see an option to “Download
SDK”. Choose that, and select a vendor and Java version – both Oracle and AdoptOpenJDK have
distributions of Java 11 available.
Click Next. The next screen allows you to choose a template for program creation, but this won’t
be necessary for our purposes, so click Next again.
The final screen lets you choose a name for your project. Call it HelloWorld. You can also choose
a location for your project
Adding a class
Next, you will need to create a class - in Java, all program source code is organized as part of
a class. This project is very simple, so we will only need a single class with a single method.
That method must be called main() – when you tell the Java virtual machine to run a program,
it always searches that program for a method named main() to tell it what to do next.
Adding a class in Eclipse
If you’re using Eclipse, right-click on the HelloWorld project and select New → Class. Name
the class HelloWorldMain3 . Select the checkbox that says that you would like to create a met-
hod stub for public static void main(String args[]), as well as the checkbox for “generate
comments”.
You may get a warning about “creating a class in the default package”. We’ll discuss Java packa-
ges, and why it is a good idea to use them, later in semester; however, for the time being, it’s
enough to know that you can safely ignore this particular warning.
3 Note: while it is not strictly necessary to use CamelCase to name Java classes, it is is a very widely-used convention.

See for instance Google’s Java style guide (https://google.github.io/styleguide/javaguide.html).

3
Monash University FIT2099 Object-oriented Design and Implementation 2020 S1

The source code for the new class appears.


You’ll see a source code window something like the following:

Adding a class in IntelliJ IntelliJ users will need to open up the HelloWorld project by clicking
the arrow next to it in the Project window. Right click the src folder and select New → Java
Class – if that option doesn’t appear, check that you’re right-clicking the correct folder. A small

4
Monash University FIT2099 Object-oriented Design and Implementation 2020 S1

dialog will pop up that lets you name your class – call it HelloWorldMain.

Unlike Eclipse, IntelliJ doesn’t give you the option to insert an empty main() method. Between
the curly brackets of the HelloWorld class, type the following:
public static void main(String args[]) {
}
Note that every part of this method declaration is needed, even the empty brackets – if you leave
anything out, your program won’t compile. You’ll see that the IDE provides autocompletion
hints for you as you type.
Building and running your project In Eclipse, you can run this program now. One way to do so
is to right-click the HelloWorld project folder icon, and select Run As → Java application.
In IntelliJ, you’ll need to tell IntelliJ what kind of project you’ve built before you can run it by
creating a run configuration – do this by clicking the “Add Configuration...” button in the toolbar.
Click the + button and select Application.
Give your run configuration a descriptive name, such as HelloWorld. In the Main Class textbox,
type HelloWorldMain. Check that the JRE is correct (it should be a Java 11 JRE or JDK) Click OK.
You’ll very rarely need to do this more than once per project.
You can build the project by clicking the hammer icon in the toolbar. Then you’ll be able to run
your program by clicking the green triangle icon in the toolbar4 . You’ll be able to build and run
without touching the run configuration from now on.
Making the program do something The program will compile and run now. It will, however,
do nothing, as the body of the program is completely empty.
Now, we’ll add some code to do something.
Writing the main() method
In a standard text-based Java program, execution begins with a method called main(). Generally,
you should only have one main() method in each Java project.
Between the curly brackets of the main() method, type the following source code line:
1 System .out. println ("Hello , World !");

(Note: if you followed the directions for creating an Eclipse project, you can delete the comment
that says “// TODO Auto-generated method stub”.)
Tip: Type this by hand rather than copying and pasting! Pasting code from a PDF into an IDE
often fails to work. The PDF may contain smart quotes or other characters that don’t belong in
your source code.
As you type the line of source code, you’ll see error indicators appear – in Eclipse, an X in a red
circle will appear to in the left margin of the line you’re typing on; in IntelliJ, a red underscore will
highlight missing or unfinished statements. This indicates that your code is not (yet) syntactically
correct Java. Modern IDEs are generally very good at identifying syntactic mistakes in Java code
as you type. When you are finished typing the line, the error indicators should disappear. If
they don’t, you have made a mistake somewhere; if you can’t find it, ask a tutor or classmate, or
4 There are at least three other ways too – explore the user interface and see if you can find them

5
Monash University FIT2099 Object-oriented Design and Implementation 2020 S1

post to the FIT2099 Moodle forum. Keep your code snippets and screenshots reasonably small,
though, and don’t forget to read up and down the thread to see whether somebody else has
already found a solution to your issue.
Save your modified class by clicking on the save icon in the toolbar.
Run the modified code. Confirm that it indeed prints “Hello World!” in the console log. This is
what it’ll look like in Eclipse:

And in IntelliJ:

Task 3. Importing and exporting code with Eclipse


Despite what popular culture would have you believe, programmers very rarely work alone on
a project. In order to work as a programmer, you’ll need to learn how to share your source code
with teammates.

6
Monash University FIT2099 Object-oriented Design and Implementation 2020 S1

Later on in this unit, you will be using version control systems which allow programming teams
to create shared file repositories. These not only allow easy sharing, but keep track of all the
thousands of changes that a team of programmers will make in the course of a large project.
Sometimes, however, it can be very useful to make a zip file containing an Eclipse project to send
around, so we’ll try that now.
Download the MovingBall project from the FIT2099 Moodle page. This archive contains a project
for a slightly more complicated Java program. It is available as an Eclipse or IntelliJ project –
choose the one that matches the IDE you’re using.
Select File → Import from the main Eclipse menu. In the dialog box, select “Existing Projects
Into Workspace”, select the file you’ve downloaded, and click “Finish”.
A new project called MovingBall will appear in the Package Explorer on the left of your Eclipse
window.
Try to run MovingBall in the same way you ran HelloWorld. You’ll see a window pop up, and a
circle will gradually move across it.
Now change the window title from Sample Frame to *name* modified this where *name* is
your name. (If you can’t figure out how, have a look at the documentation and try Google. If you
still haven’t figured it out by Week 2, ask your lab staff!)
Run your modified code to ensure that it still works.
Now, export your project to a zip file archive. To do this, right click on the MovingBall project in
the Package Explorer, select export, then select General, then Archive File in the dialog box,
then click the Next button. Ensure that you include the source code files, but not the contents of
the bin directory, in the export. Finally, choose an appropriate file name to save to and click the
Finish button to create the archive.

Congratulations
You’ve completed the Week 1 introductory Bootcamp exercise. Be aware that future Bootcamps
will:
• be much more challenging, and
• be marked in class.
Preparing for these in advance is strongly recommended!
See you next week.

You might also like