Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS1027

LAB 0

Computer Science Fundamentals II

This is an optional lab, not worth any marks. It is to help you get started with Eclipse and Java.

Learning Outcomes

•    Import a project into Eclipse from existing source code files

•    Create a new project in Eclipse and add classes

•    Develop and run a "Helloworld" Java program

•    Add simple loops and conditionals to control the flow of execution

Pre-Lab

If you don't already have Eclipse, download it from the official website at the link below. The website also provides detailed instructions to help with the installation.

http://www.eclipse.org/downloads/

You will also need to download and install the Java Development Kit (JDK) if it is not already

installed on your computer. Note that you may be asked to create an account to download these files. The account is free and it is the official Oracle website so they will not send spam mail.

https://www.oracle.com/java/technologies/javase-jdk14-downloads.html(JDK)

Exercise 1 - Import existing code into Eclipse

1.   Download the two provided Java files:Person.javaandSchool.javaand put them where you can easily find them (ideally within a "CS 1027" folder for all your coursework). 2.   Open Eclipse on your computer.

3.   Select File > New > Java Project from the top menu.

4.   Enter the project name: Lab0_A.

5.   Uncheck “Create module-info.java file” .

6.   Uncheck "Use default location", and browse to find the folder where you stored the Java files downloaded above. Click Finish at the bottom of the window.

7.   Notice your project now appears in the Package Explorer on the left side. You can expand the project's levels by clicking the little arrow icon. You should see both Java files appear in there.

Exercise 2 - Create a new project and copy existing code

1.   Select File > New > Java Project from the top menu.

2.   Enter the project name: Lab0_B

3.   This time, leave the "Use default location" box checked (or click to check it now if it's currently unchecked) so that it will create the project in the designated workspace folder.

4.   Uncheck “Create module-info.java file” .

5.   Click Finish at the bottom of the window.

6.  Again, you should see the project appear in the Package Explorer on the left. Notice, however that if you expand the project, there is a "src" (source code) folder which is   currently empty.

7.   Right-click on that "src" folder and select New > Class.

8.   Enter the Name: School

9.   Click Finish at the bottom of the window.

10. Repeat these steps to create another class and name this one: Student

11. Open the completed School.java file from Exercise 1. Select all of it and copy it.

12. Open your new School.java file that you just created. Delete the little bit of code at the  top (class name and brackets). Paste the code that you copied from the completed file.

13. Select File > Save or click the disk icon at the top to save it. You will notice some red underlines but don't worry! We'll fix that momentarily.

14. Likewise, open Person.java from Exercise 1 and copy all the code. Go into your new Student.java file, delete the default code, and paste the code from Person.java.

15. Save this file.

16. You will see that there is just one error at the top. Hover your cursor over the error on    "Person" and you should see a little message pop up: "The public type Person must be defined in its own file" along with some suggested actions to fix it (ignore their suggestions). The reason for this error is that the filename does not match the class name, which is not allowed.

17. In the Package Explorer, right-click on Student.java under Lab0_B and select Refactor >  Rename. In the pop-up window, enter the name Person and click OK. Now that the class is named Person, which matches the filename (Person.java), the errors should all disappear from both files.

Exercise 3 - Add a main function

1.   Right-click on "src" under Lab0_B and select New > Class.

2.   Enter the Name: Greetings

3.   Further down in the window, under "Which method stubs would you like to create?", check the box beside the first option: public static void main(String[] args)

4.   Click Finish at the bottom of the window.

5.   Note that if you don't click this checkbox, you can type it manually to add the method. The main method is the first method called when you run a program so every project must have at least one main method defined with this exact header.

6.   Inside the main method, type the following text: System.out.println("Hello world");

7.   Save the file (and any other files that haven't been saved recently).

8.   Click the Run icon (white triangle within a green circle).

9.   In the Console panel at the bottom, you should see "Helloworld" printed out.

10. Open School.java under Lab0_B and notice that it also has a main method near the bottom.

11. Click the Run icon again while in the School class. Notice that nothing is printed out this time. This is because it's running the main method in School (which doesn't print anything out) rather than the one in Greetings. Note that if you have exactly one main   method in a project, it will know to use that one regardless of which file you are within,  but if there are multiple main methods, it runs the one in the class you are within or the one you most recently ran.

Exercise 4 - Adding loops and conditionals

1.   Open School.java under Lab0_B if it's not already open.

2.   Notice near the end of the constructor, there is a line of code creating an array of Person objects: students = new Person[] {p1, p2, p3, p4, p5, p6, p7}; (don't   worry if this is confusing right now - you will learn more about objects very soon). An array is similar to a Python list in the sense that it holds multiple items.

3.   Click below this line, but still within the School constructor method, and hit Enter a few times to give yourself space to code.

4.   In this area, you will create a for-loop using the following code:

for (int x = 0; x < 7; x++) {

System.out.println(students[x]);

}

5.   Save the file and click the Run icon. You should see that all 7 student's names and email addresses from above are being printed in the Console.

6.   Now add a while-loop to do the same thing using the following code:

int x = 0;

while (x < 7) {

System.out.println(students[x]);

x++;

}

7. Click inside the for-loop and add a conditional so that it only prints out the first 4 students (without changing the loop header). The if-statement will be:

if (x < 4) {

8.  You will also have to close the bracket after the print line statement:

}

9.   Click inside the while loop and add another, similar if-statement so that it only prints out the last 3 student names (again, do not change the loop header itself). NOTE: make sure the line "x++" is NOT within the conditional or you will enter an infinite loop!

10. Save and run the program and you should see all 7 students' info printed out exactly once each. 4 of those lines from the for-loop and the other 3 from the while-loop.

11. This concludes the optional Lab 0. Feel free to play around the code more for practice.