CS计算机代考程序代写 Java DEPARTMENT OF COMPUTING & ELECTRICAL ENGINEERING

DEPARTMENT OF COMPUTING & ELECTRICAL ENGINEERING

F29AI – Introduction to Eclipse, git & Gitlab
Getting (& submitting) code for Coursework 2, Part 1

Eclipse (an IDE, my favourite) and GitLab are two tools you will be using to do Part 1 of Coursework
2. It is important that you know the basics of using these tools. For those of you who are already
familiar/experienced with git or Eclipse, want to use the command line for git, or want to use a
different IDE than Eclipse, you can skip Part 1.

IMPORTANT: If you are a 2-year Masters student, you will not be using Eclipse or Gitlab as your
coursework will not involve any programming. So you can safely ignore this document.

Part 1: Introduction to Eclipse

Eclipse is an Integrated Development Environment (IDE) that is designed to help programmers
develop software. It is widely used in industry, academia and for personal development projects.
Eclipse is a large and complex piece of software, but fortunately you can ignore most of the more
complex parts when you first start to program. The aim of this introduction is to take you through
the basics so that you are ready to use Eclipse in future lab sessions.

Create a new workspace

The code you write in Eclipse is stored in a particular directory, known as a workspace. Since it is
possible to have more than one workspace, when you first run Eclipse, it will ask you which
workspace you want to use. At this point you need to create a new workspace:

1. In the Workspace field, type:
C:\Users\\SD1
Note the last character is a one, not the letter ‘l’. Also, be sure to replace
with your Windows user name, which will typically be two or three letters followed by a
number.

2. Check the “use this as the default” checkbox and click Launch.

3. Click OK, then dismiss the welcome panel that appears.

Make a new project

In Eclipse, you always have to create a project before you can start writing code. A project is basically
a directory within the workspace that holds related pieces of code, compiled programs, and
anything else the program needs in order to function correctly (e.g. text files or images).

1. From the File menu, select New -> Java project (you can also use the little arrow next to
the first icon in the Eclipse toolbar). This should cause a dialog box to appear.

2. Type “Lab1” in the Project Name field and then click Finish. Lab 1 should now appear
under the Package Explorer panel.

3. In case Eclipse asks you whether you want to create a Module, click “Don’t Create”.

Make a new class

A class is a file that contains Java code. Programs can be made up of multiple class files, but to begin
with you will only be creating programs that have a single class. To create one:

1. Select Lab1 in the Package Explorer, and then select File -> New -> Class.

2. Type “MyProgram” in the Name field,

3. Select the checkbox next to public static void main(String[] args),

4. Click Finish. A new file, MyProgram.java should now appear under Lab1 and its contents
will be displayed in the central panel.

5. To reduce clutter, close the Task List and Outline panels. You don’t need these. Your
screen should now look something like this:

Write some code

Now you have a project and a class, you can write some code:

1. Delete the line: // TODO Auto-generated method stub

2. and then replace it with: System.out.println(“Hello!”);

3. As you type, Eclipse will offer you some suggestions in a dropdown menu. Ignore these for
now.

4. Make sure you type the text exactly as shown. If you get the capitalisation wrong (e.g.
system rather than System) or miss out a character (e.g. the semicolon at the end of the
line), a small red “x” will appear next to the line, telling you there’s something wrong. If this
happens, hover the mouse cursor over the “x” to see what the error is.

5. Save the file by clicking on the disk icon in the Eclipse toolbar, or alternatively by typing
ctrl-S (or command-S on a Mac).

Run the code

To run the code:

1. Select “MyProgram.java” in the Package Explorer.

2. Click on the play button in the Eclipse toolbar.

3. Assuming your program compiled correctly (i.e. if there are no errors in your code – see
above), a Console will appear at the bottom of the Eclipse window and Java will print out
“Hello!”

Modify the code

Try making some changes to the code:

1. Replace “Hello!” with something else, then run the program again.

2. Remove the semicolon at the end of the line. A red ‘x’ should appear in the margin. Hover
the mouse cursor over this and look at the error message that is displayed.

3. Change “System” to “system”. Hover over the red ‘x’ again. There should now be two error
messages displayed.

4. Click on the ‘x’. This will bring up a list of suggestions for how to fix the “system” error
message. Eclipse tries to be helpful and suggests ways of correcting some errors, but note
that its suggestions are not always correct. In this case the correct solution (Change to
‘System’) is quite a long way down the list, and all the other suggestions are incorrect.
Double-click on the correct solution and Eclipse will fix the problem. At this stage in your
programming career, it is better to work out how to fix errors yourself, rather than rely on
Eclipse’s often misleading suggestions.

Part 2: Introduction to GitLab

GitLab is a code repository (i.e. somewhere on the internet where code in stored) that is hosted by
the university. Lecturers use it to share code with students, and students use it to store code that
they have written for assignments. This introduction will take you through the basics.

Sign in to GitLab

Before you can use GitLab, you need to sign in:

1. Open a web browser and go to:
https://gitlab-student.macs.hw.ac.uk/

2. This will bring up the sign in screen. Select the “Remember me” checkbox and then click on
Azure OAuth2.

3. Type in your university email address (your user name followed by @hw.ac.uk) and click
Next.

4. This should take you to the university’s sign in page. Enter your email address and
password and click Sign In.

Set up encryption

GitLab uses public key encryption to securely transfer code. Before you can use it with Eclipse, you
need to make an encryption key and share it with GitLab. You only have to do this once for it to
work with all computers in the lab. It’s a bit fiddly, so please follow these instructions carefully:

1. Go to Windows -> Preferences to open Eclipse’s Preferences dialog.

2. Navigate to General -> Network Connection -> SSH2 in this dialog.

3. Click on the Key Management tab. It should now look something like this:

4. Click on the Generate RSA Key… button (the second button!)

5. Copy the text (using ctrl-C) that appears (starting with ssh-rsa) in the text box.

6. Click Save Private Key… and then Okay to the confirmation message.

https://gitlab-student.macs.hw.ac.uk/

7. Now go back to GitLab in your web browser, click on the right-most dropdown menu in
GitLab’s top toolbar and choose Settings, and click on the SSH keys option on the left.

8. Paste the text you copied earlier into the big text box, type “Eclipse Key” in the title box,
and click Add key.

Check out (downloading) a project from GitLab

GitLab allows you to set up a project and then share it with other people (just like GitHub and other
git servers, if you’ve ever used one of these). We use GitLab to provide the materials mainly for
distribution, testing and feedback on F29AI Coursework 2, Part 1. Consequently, before you start
your coursework, you need to know how to check out a project from GitLab. This is how you do it:

1. Assuming you managed to sign in successfully to GitLab (see earlier), a list of projects
should now appear. Click on the one called F29AI-2019-20 / F29AI-CW2-TicTacToe.

2. Click on the word fork towards the top of the screen, then select your name when asked
for a namespace. This will create your own copy of the project, which you can edit. THIS
STEP IS VERY IMPORTANT. DO NOT PROCEED WITHOUT FORKING YOUR OWN COPY.

3. Click on the Clone button. This will reveal a URI. Copy this by pressing the button next to it:

At this point you have the option of (1) checking out a project from within Eclipse using
Eclipse’s own git integration; or (2) checking out the project using git directly from the
command line. If you want to use Eclipse (highly recommended) then definitely do (1).

(1) Checking out a project from within Eclipse (using Eclipse’s own git integration)

1. Go to Eclipse, select Import…, open the Git folder, select Projects from Git (with smart

import) and click Next.
2. Select Clone URI and click Next.

3. Paste (ctrl-V) the URI you copied from GitLab into the URI text field.

4. Keep clicking Next and then Finish when you get to the last screen.

5. A new project called F29AI-CW2-TicTacToe should appear in the Package Explorer
panel:

(2) Checking out a project using git directly from the command line.
1. Open the terminal (terminal in linux/mac and command line in Windows)
2. Using cd go to a folder where you would like to place the project – this is usually

somewhere you would store your other projects, usually under ~/git/
3. Then do: git clone – paste the URI you copied from the previous stage in place

of . This should download the project into a folder with the same name as
above: F29AI-CW2-TicTacToe

Committing changes from Eclipse

One of the benefits of using GitLab is that it provides version control, allowing you to see the changes
you’ve made to your code, and if necessary role it back to an earlier working version. For this to be
beneficial, you need to commit your changes often. This is how you do it:

1. First of all, make a change in the project you imported. For instance, replace the “Hello
World!” string with something else.

2. Right-click on the project name in the Project Explorer. This will bring up a menu. Scroll
down to Team and select Commit…

3. This will bring up the Git Staging panel. Assuming you’ve made a change to your java file, it
should appear in the ‘Staged Changes’ section at the bottom. If not, click the “+” button.

4. On the right, in the ‘Commit Message’ area, write a short description of the change you
made, e.g. “Changed the output message”. Also make sure your name and correct email
address appears in the author and committer text fields.

5. Click on the Commit and Push… button. This will send your changes over to GitLab.

6. Go back to the forked project in your web browser. You should find that the changes you
made now appear in the code stored in GitLab. If you click on the History button, you will
see your commit message.

7. You can also see your commit history inside Eclipse. To do this, right-click on the class
name in the project explorer and choose Team -> Show in History. This view also allows
you to see the changes you made at each commit.

8. GitLab supports automatic unit testing for java so once Arash uploads the unit tests for the
Tic Tac Toe project, you should be able to see whether your code passes the tests. There
will be separate instructions on how to use/run these tests.

For version control to be effective, you should commit changes to your code frequently, not
just when you’ve got it working. This will allow you to easily roll back any mistakes.

Rolling back to an earlier version

To replace your current project with an earlier version, you can use Eclipse’s replace function:

1. Right-click on your project’s name in the Project Explorer and select Replace With. The two
most useful options in this sub-menu are Previous Revision, which will restore your project
to how it was at the previous commit, and Commit…, which will restore it to how it was
after a specified commit.

Part 1: Introduction to Eclipse
Part 2: Introduction to GitLab