Program 0a

Using Eclipse

Patterns in Software Construction
INF-45


Introduction This first "programming" assignment is not a "programming" assignment at all; it is designed to ensure that you know the fundamentals of using the computing infrastructure for this course, both on your own machine (if you have one) and the computers in our labs. Primarily it concerns starting to learn to use the Eclipse Integrated Development Environment (IDE) for Python and Java, which is available on both the Microsoft Windows and Mac Operating System. Knowing how to use these tools is an important first step towards being able to complete the remaining programs in this course.

Subsequent programming assignments will have much less of a cookbook flavor; instead, they will focus on designing, writing, and debugging programs using some of the libraries we will discuss. But, these assignments will assume that you are already an experienced Eclipse user, who knows all the tools and skills covered in this assignment, and can easily apply them when writing his/her own actual programs.

So, treat this assignment very seriously: you will not want to still be learning this material at the same time that you are writing your early programs. Now is the time to begin mastering these important tools and skills. I recommending doing all parts of this assignment twice: first reading the instructions carefully, and again working without the written instructions (to s determine whether you have internalized them).

Finally, read and follow these instructions carefully. It would be an excellent idea to print a copy of this web page, read through it, and highlight any details that you think are important (and might forget as you are working on the assignment) or that you are confused about. If you have any questions about these instructions, for example, if you think they are in error, or just confusing, please contact your instructor or better yet, post on one of the MessageBoard forums.

You may want to first read through the Eclipse section of the Windows Operating System & Eclipse IDE web page (which is also accessible on the Handouts web page). It includes lots of useful information about using the Eclipse IDE using Java (my ICS-21 students learn about Eclipse and Java this way).


Part 2:
Using the Eclipse IDE
Read parts 3a, 3b, 4a, and 4b, following their instructions. If you have any questions, feel free to ask for help from me, any staff member, or other students. Please cross the "question asking threshold" quickly; get into the habit of being able to ask us questions, especially in lab.

To do this assignment, you must have already downloaded and installed Python, Java and the Eclipse IDE on your own computer (or be using one of the UCI Lab machines, where it has already been downloaded/installed).

In the first part of writing programs with each language, you will start a new project and write a trivial program; in the second part you will download prewritten programs and correct them.


Part 3a:
Using the Eclipse IDE with Java: A New Project
Before starting, put Eclipse in the Java perspective (choose the Java icon on the top right of the Eclipse window -but below the menu line).
  1. Start a New Project.
    • Select File, hover over New, and click Java Project.
    • In the Project Layout pane, click the radio button labelled Use project folder as root for sources and class files. To avoid having to make this selection in the future on you home machine, in the Source and output folder pane, click the radio button labelled Project; then click Apply; then click OK.
    • In the Project name text box, type Program0aJavaNew.
    • Click the Finish button.

    Note that the Package Explorer tab will now show a folder icon labelled Program0aJavaonNew. Disclose this icon and note the JRE System Library icon, which you do not have to disclose.

  2. Create a class to contain the program.
    • Click the Program0aJavaNew. icon in the Package Explorer tab.
    • Select File, hover over New, and click Class.
    • In the New Java Class pane, type the name Test. Click the Use project folder as root for sources and class files radio button.
    • Check the box labeled public static void main (String[] args).
    • Click the Finish button.

    Note that the Package Explorer tab will now show a folder icon labelled New.java. The editor pane will show a tab labelled New.java with some text in it. Right-click the grey vertical line in the left of the New.java window contents and click Show Line Numbers in the pull-down menu.

  3. Clean up the text and write a tiny program.
    • Select line 1 and delete it.
    • Select lines 2-5 and delete them.
    • copy (or cut/paste) the text
      System.out.println("Hello world!");
      into the editor, replacing line 3.

  4. Run the program
    • Right-click the New.java window contents, hover over the Run As entry, and click 1 Java Application.
    • Click the checkbox labelled Always save resources before launching in the Save and Launch pop-up window.
    • Click OK.

    A tab labelled Console should appear beneath the New.java pane and show the text Hello world!.

  5. Change and rerun the program
    • Replace the word Hello by the word Goodbye.
    • Click the "white triangle in a green circle" icon (aka as "rerun"; it is beneath the Navigate menu item.

    The tab labelled Console should now show the text Goodbye world!.

  6. Delete the project (from Eclipse) and remove it from the workspace
    • Right-click the icon labelled Program0aJavaNew.
    • Click the "red X" Delete icon
    • Click OK in the Delete Resources pop-up window.
      NEVER click/check the Delete project contents on disk (cannot be undone) icon.
    • Remove the Program0aJavaNew folder from the workspace folder. You can delete it, or save it in a folder with all the work you do for this course.

    Again, if you have any problems, please ask for help during the lab. After you are finished, repeat these steps a few times, until you can do them from memory. This programming assignment is very cookbook, but by the end you should have mastered the steps needed in creating and manipulating projects with Eclipse -something you will do over and over again during the quarter. Repeat all these steps until you can perform them from memory.


Part 3b:
Using the Eclipse IDE with Java: An Existing Project
Before starting, put Eclipse in the Java perspective (choose the Java icon on the top right of the Eclipse window -but below the menu line). Download Collatz Program (Java) and unzip it; put the collataz-java folder on the desktop or in a folder that will contain all the work you do for this course. You will see that while some steps are different, others are the same (or similar) to the steps for new projects.
  1. Start an Existing Project.
    • Select File, hover over New, and click Java Project.
    • In the Project Layout pane, click the radio button labelled Use project folder as root for sources and class files. To avoid having to make this selection in the future on you home machine, in the Source and output folder pane, click the radio button labelled Project; then click Apply; then click OK.
    • Click to remove the check in the Use default location checkbox.
    • Click the Browse button.
    • Navigate to the location storing the collatz-java folder that you downloaded and unzipped.
    • Click the collatz-java folder.
    • Click OK; note that the Project name: text box will automatically be filled in with collatz-java.
    • Click the Finish button.

    Note that the Package Explorer tab will now show a folder icon labelled collatz-java. Disclose this icon and note the (default package) and JRE System Library icons. Disclose the (default package) icon and double click the icon it discloses. Finally, note the editor pane will show a tab labelled Collatz.java with some text in it. If its lines are not numbered, right-click the grey vertical line in the left of the Collatz.java window contents and click Show Line Numbers in the pull-down menu.

    This program contains four mistakes that you must fix before running the program: you don't have to understand the mistakes, but you should fix them. Each mistake is marked in the left margin by a "white X in a red circle"; as each mistake is fixed, its icon should disappear.

  2. Fix the program.
    • On line 62, put a + operator after the last String in quotes, and before the identifier testNumber.
    • On line 70, put a ; (semicolon) after the ++ operator.
    • On line 71, replace the = operator by the == operator (no spaces between the two equal signs).
    • On line 72, replace the identifier testnumber by the identifier testNumber (notice the capital N).

  3. Run the program
    • Right-click the Collatz.java window contents, hover over the Run As entry, and click 1 Java Application.
    • Click the checkbox labelled Always save resources before launching in the Save and Launch pop-up window.
    • Click OK.

    A tab labelled Console should appear beneath the Collatz.java pane and show the prompt Enter a positive integer:. Enter the value 17 and press Enter. Observe the program's output (read the comment at the top to understand it). Return the program entering abc to the prompt; the program realizes this is not an integer and reprompts the user. Return the program entering abc to the prompt; the program realizes this is not an integer and reprompts the user. Enter (or copy/paste) the value 123456789123456789 and press Enter; the program terminates because that value is too big for Java's int type.

  4. Delete the project (from Eclipse)
    • Right-click the icon labelled collatz-java.
    • Click the "red X" Delete icon
    • Click OK in the Delete Resources pop-up window.
      NEVER click/check the Delete project contents on disk (cannot be undone) icon.

    Again, if you have any problems, please ask for help during the lab. After you are finished, repeat these steps a few times, until you can do them from memory. This programming assignment is very cookbook, but by the end you should have mastered the steps needed in creating and manipulating projects with Eclipse -something you will do over and over again during the quarter.

    After you have finished, submit the correct Collatz.java file you edited, using the Homework Dropff link.


Part 4a:
Using the Eclipse IDE with Python: A New Project
Before starting, put Eclipse in the Python perspective (choose the PyDev icon on the top right of the Eclipse window -but below the menu line).
  1. Start a New Project.
    • Select File, hover over New, and click PyDev Project.
    • In the Grammar Version pane, select 3.0 from the pull-down menu.
    • Ensure the radio button Add project directory to the PYTHONPATH? is selected.
    • In the Project name text box, type Program0aPythonNew.
    • Click the Finish button.

    Note that the Package Explorer tab will now show a folder icon labelled Program0aPythonNew. Disclose this icon and note the Python32 icon, which you do not have to disclose.

  2. Create a module to contain the program.
    • Select File, hover over New, and click PyDev Module.
    • In the Name text box, type the name Test; the Source Folder text box should already contain /Program0aJavaNew.
    • Click the Finish button.

    Note that the Package Explorer tab will now show a folder icon labelled New. The editor pane will show a tab labelled New with some text in it. Right-click the grey vertical line in the left of the New.java window contents and click Show Line Numbers in the pull-down menu.

  3. Clean up the text and write a tiny program.
    • On line 6, replace the name after @author: by your name.
    • copy (or cut/paste) the text
      print("Hello world!")
      into the editor on line 6.

  4. Run the program
    • Right-click the New window contents, hover over the Run As entry, and click 1 Python Run.
    • Click the checkbox labelled Always save resources before launching in the Save and Launch pop-up window.
    • Click OK.

    A tab labelled Console should appear beneath the New pane and show the text Hello world!.

  5. Change and rerun the program
    • Replace the word Hello by the word Goodbye.
    • Click the "white triangle in a green circle" icon (aka as "rerun"; it is beneath the Navigate menu item.

    The tab labelled Console should now show the text Goodbye world!.

  6. Delete the project (from Eclipse) and remove it from the workspace
    • Right-click the icon labelled Program0aPythonNew.
    • Click the "red X" Delete icon
    • Click OK in the Delete Resources pop-up window.
      NEVER click/check the Delete projcet contents on disk (cannot be undone) icon.
    • Remove the Program0aPythonNew folder from the workspace folder. You can delete it, or save it in a folder with all the work you do for this course.

    Again, if you have any problems, please ask for help during the lab. After you are finished, repeat these steps a few times, until you can do them from memory. This programming assignment is very cookbook, but by the end you should have mastered the steps needed in creating and manipulating projects with Eclipse -something you will do over and over again during the quarter. Repeat all these steps until you can perform them from memory.


Part 4b:
Using the Eclipse IDE with Python: An Existing Project
Before starting, put Eclipse in the Python perspective (choose the Python icon on the top right of the Eclipse window -but below the menu line). Download Collatz Program (Python) and unzip it; put the collataz-python folder on the desktop or in a folder that will contain all the work you do for this course. You will see that while some steps are different, others are the same (or similar) to the steps for new projects.
  1. Start an Existing Project.
    • Select File, hover over New, and click PyDev Project.
    • In the Grammar Version pane, select 3.0 from the pull-down menu.
    • Click to remove the check in the Use default location checkbox.
    • Click the Browse button.
    • Navigate to the location storing the collatz-python folder that you downloaded and unzipped.
    • Click the collatz-python folder.
    • In the Project name text box, type collatz-python; in Python, unlike Java, this box is not automatically filled in.
    • Click the Finish button.

    Note that the Package Explorer tab will now show a folder icon labelled collatz-path. Disclose this icon and note the collatz.py and Python32 icons. Double-click the icon. Finally, note the editor pane will show a tab labelled collatz with some text in it. If its lines are not numbered, right-click the grey vertical line in the left of the collatz window contents and click Show Line Numbers in the pull-down menu.

    This program contains four mistakes that you must fix before the program runs correctly: you don't have to understand the mistakes, but you should fix them (actually you fix three, then run the program, and then fix the fourth).

  2. Fix the program (the first three mistakes).
    • On line 44, put a , after the last String in quotes, and before the identifier testNumber; right-click the editor window and click
    • On line 51, replace the ++ by += 1; right-click the editor window and click
    • On line 52, replace the = operator by the == operator (no spaces between the two equal signs); right-click the editor window and click

    The first mistake is marked in the left margin by a "white X in a red circle"; as each mistake is fixed, and the file is saved, its icon should disappear and an error on the next error appears.

  3. Run the program
    • Right-click the Collatz.java window contents, hover over the Run As entry, and click 1 Python Application.
    • Click the checkbox labelled Always save resources before launching in the Save and Launch pop-up window.
    • Click OK.

    A tab labelled Console should appear beneath the collatz pane and show the prompt Enter a positive integer:. Enter the value 17 and press Enter. Observe the program's output (read the comment at the top to understand it). This program contains an infinite loop.

  4. Fix the program (completely) and rerun the program
    • Click the first red square to the right of the Console tab; doing so terminates the program.
    • On line 53, replace the identifier testnumber by the identifier testNumber (notice the capital N).
    • Click the "white triangle in a green circle" icon (aka as "rerun"; it is beneath the Navigate menu item.

      Enter the value 17 and press Enter. Observe the program's output; this time it terminates normally with an answer. Return the program entering abc to the prompt; the program because that value is not an integer. Rerun the program entering (or cutting/pasting) 123456789123456789; Observe the program's output (which is correct).

      So, the Java a Python program behave differently for thse boundary cases.

    • Delete the project (from Eclipse)
      • Right-click the icon labelled collatz-java.
      • Click the "red X" Delete icon
      • Click OK in the Delete Resources pop-up window.
        NEVER click/check the Delete projcet contents on disk (cannot be undone) icon.
      • Remove the .project and .pydevproject files from the collatz-python folder (otherwise there will be an error message from Eclipse if you try to put this project back into it).

      Again, if you have any problems, please ask for help during the lab. After you are finished, repeat these steps a few times, until you can do them from memory. This programming assignment is very cookbook, but by the end you should have mastered the steps needed in creating and manipulating projects with Eclipse -something you will do over and over again during the quarter.

      After you have finished, submit the correct collatz.py file you edited, using the Homework Dropff link.


Bottom Line You will be writing many programming assignments during the quarter, requiring you to create new projects or modify existing ones. You should be able to manipulate both kinds of projects easily in Eclipse, either with Java or Python.

Also, whenever you want to write a small program to test out your understanding of Java or Python, you should have a very low threshold to quickly creating a project and writing the experimental code that you want to test.