In this lab, we will get familiar with Java and the Eclipse IDE. The concepts we will cover are variables, reading data from the user using the console, writing data to the output window, and using Strings and the Java API. The tutorials in the lab will be presented by the TA step-by-step. However, you are free to work at your own pace on the lab. The lab assignment questions will not be presented by the TA.
This tutorial will demonstrate how to create a workspace, a project, and a Java file in a project. The tutorial will use HelloWorld.java as an example. We will run a program and show the comments necessary in each program (student id and name).
Eclipse stores all the information about your projects in a workspace, so first you need to set one up.
When you open Eclipse, you will be prompted for where it should load a workspace from, or where a new one should be created.
Open Eclipse: Start Menu -> Programs -> Eclipse ->Eclipse
If the text box contains another value, change it to "F:\Workspace" so that all of your files are stored on your network space. Click OK.
Next time you open Eclipse, select the same path.
You will be presented with a welcome screen that gives you some options to learn about Eclipse.
Click on the arrow icon to go to the Workbench and get started.
You are now presented with the editing view. Since we haven't started working yet, everything looks pretty empty.
Create a new Java Project by going to the File Menu -> New -> Java Project
In the Project Name text box, give a name to your project (eg., "Lab5") and click Finish.
There is now an item in the left hand column for this project.
If you expand it you can see that there is a 'src' folder for your code files, and the JRE System Library is included.
Next we want to create a new Java Class, to create our first program.
Right click on the src folder and select New -> Class.
We want to name it HelloWorld, and make sure to check the box next to "public static void main(String[] args)". Then click Finish.
We now have an editor window with a template for our new class.
Make sure to add a multi-line comment to the start of all of your files that has your Name and Student number.
To output the string "Hello World!", we need to call the method System.out.println(), and pass it our string.
You may notice that as you type, Eclipse tries to help you by giving suggestions.
Everytime you type a period after a word Eclipse recognizes, a menu is displayed below the text you are typing with any suggestions.
You can scroll through this list with the up and down arrows on the keyboard, or with your mouse.
Hitting enter will finish typing whatever you have selected for you.
Your file should now look like this:
To run your program, save your file, then go to Run -> Run As -> Java Application.
Or click the green and white 'play' icon and select Java Application if a dialogue appears.
The bottom portion of the window changes to the Console, which contains the text "Hello World!".
Download the HelloWorld.Java example file.
This tutorial will discuss the Java API, the Scanner class for input, and the PrintStream (System.out) for output.
The Java API can be accessed at http://download.oracle.com/javase/6/docs/api/.
This describes all of the built-in classes available to you in your programs.
Task: find the documentation for the Scanner class:
The Scanner class will allow us to read in what the user of our program types and use it within our program.
Task: find the System class in the documentation.
The System class contains a field called 'out', that is of the type PrintStream. We used this in the previous tutorial to display "Hello World!".
Click on the PrintStream link to view information about this class.
It contains many versions of the println method we used earlier to work with different types of data.
Task: Create a program that reads two numbers typed in by the user, and prints out their sum as "A + B = X"
Create a new java class called 'Adder'. Again, remember to check the box next to "public static void main(String[] args)".
First we want to create a new instance of the Scanner class, so we can read what the user types.
Name your variable reader and pass System.in to the Scanner class constructor, so that it reads text typed on the keyboard.
If Scanner is underlined in red, click the icon in the left margin and select "Import 'Scanner' (java.util)."
Next we want to ask the user for two numbers. Call System.out.print() with a string asking for the first number.
Now we can ask the user for a number by calling our reader object's nextInt() method.
The program will wait for the user to type in a number and hit the enter key.
Repeat, asking for a second number.
Now that we have the two numbers, we need to add them together and then display the result with another call to System.out.print().
Your finished program should look like this:
Debugging allows us to look at our program while it is running and see if it is working properly.
A breakpoint is a spot in our code where we would like it to pause during debugging, so that we can examine how it is working.
To add a breakpoint to your program, double click on the left margin on the line you would like the program to pause at. There will now be a blue dot displayed.
We want to add three breakpoints to our code: one before asking the user for the second number, one before adding the two numbers together, and one before printing the sum.
To debug your program, go to Run -> Debug As -> Java Application. There is also a short-cut icon in your toolbar that looks like a green beetle.
After you type in the first number, you will be prompted to open the 'Debug Perspective'. This means we have hit the first breakpoint.
Click on Yes, and your screen should look like this:
There are a few familiar panels, and a few new ones.
In the panel showing our program code, the breakpoint that we are paused at has an arrow next to it. This indicates that the highlighted line has not been run yet, and will execute next.
In the top right, you can view the variables of your program. We can see that number1 is there, with a value of 2, but number2 and sum are not there yet.
In the top left debug panel, click the green arrow (Resume) icon to advance to the next breakpoint.
Because our program asks for another number first, type the second number into the console at the bottom.
Now we can see that the variable number2 has a value, and we are paused before calculating the sum.
Advancing again, we can see that sum has been calculated, but the result has not yet printed.
And advancing one more time prints the sum and leads us to the end of our program. To switch back to the Java Perspective, click on the button labelled Java in the top right of the program window.
Create a program that prompts the user for 5 numbers which we will refer to as A, B, C, D, and E. Your program should output the result of A - B + C - D + E = X. For example, if the values the user entered were A=10, B=5, C=20, D=-5, and E=4 the output should be: 10 - 5 + 20 - -5 + 4 = 34. Make sure to have a comment with your student id and name. Here is some sample code.
For this question, we will use the String class. This will require using the Java API to lookup some of the methods of this class to do the tasks required. You will also have to use the if/else syntax for decisions. The tasks required are:
Type a string (at least 10 characters): Hello There Give me two numbers, that represent the index of the first character (inclusive) and last character (exclusive) to extract 1: 2 2: 4 What do you think the characters between that range are?: ll You're Right! The string you typed was 11 characters long Your string in uppercase is: HELLO THERE
Type a string (at least 10 characters): Hello Your string was too short.
Type a string (at least 10 characters): Hello There Give me two numbers, that represent the index of the first character (inclusive) and last character (exclusive) to extract 1: 2 2: 7 What do you think the characters between that range are?: llo Not Quite... The answer is: llo T The string you typed was 11 characters long Your string in uppercase is: HELLO THERE
Type a string (at least 10 characters): Goodbye World Give me two numbers, that represent the index of the first character (inclusive) and last character (exclusive) to extract 1: 3 2: 1 The numbers you entered were invalid Please try again
For this lab assignment, each group should submit using Connect the two Java files for the questions.