Software and Programming 1 Lab 1: Introduction, HelloWorld Program and use of a Debugger 1SP1-Lab1.ppt Tobi Brodie (tobi@dcs.bbk.ac.uk) 6 January 2015 Module Information Lecturer: Roman Kontchakov Chemistry Lecture Theatre, UCL Christopher Ingold Building Lab Session 4.30pm: Room 407, Birkbeck Main Building Roman Kontchakov Lab Sessions 6pm & 7.30pm: B29, UCL Foster Court, Tobi Brodie, Ping Brennan, Suneel, Dilek Module materials: http://www.dcs.bbk.ac.uk/~roman/sp1/ 2 Module Information Generally, each session is split into two ninety minute sessions and as there is a large attendance, the class is also split, so the lecture and lab session you attend will be one of the following below: • Main lecture 6pm and 7.30pm, • Lab sessions 4.30pm, 6pm and 7.30pm Attendance is compulsory for both Lectures and Labs and a register is maintained. Note: Lab sessions are designed to reinforce the materials covered in the previous weeks lecture so there is no difference in which lecture you attend. 3 Lab Session 1: Objectives (Re)familiarise ourselves! As this is the first week, we are not following up from a lecture, so we can spend this session organising logging into the computers at UCL and reminding ourselves of the syntax of Java and the tools we have used for programming. • Blue J – Application for coding in Java (free, cross platform) • Coding in Java: Basic Class construction, Methods and signatures, output to the terminal window • Loops and ‘loop tables’ • Debugging using Blue J In order to do this we will create some Classes, beginning with HelloWorld. 4 Java Project Name of project: week1 Name of class: 5 HelloWorld Getting Started • Launch BlueJ - begin with the Start icon in the lower left corner of the screen. • Select the options in the order shown: Start -> All Programs -> Programming Tools -> BlueJ • Create a new Project on your disk space. 1. Select Project then followed by New Project. 2. Select a directory in your disk space and a suitable name for your project, e.g. week1. After entering week1 in the BlueJ window, a new BlueJ window will appear for the project week1. 6 Getting Started (2) • Create a new class by clicking on button New Class ... in the new BlueJ window. • Enter the name HelloWorld for the new class and click on OK. 7 Write your first class • Move the mouse on top of the class icon with the name HelloWorld, right-click and select Open Editor. • Delete all the code in the template class and leave it empty for now. 8 Write your first class (2) • Writing your own code: 1. Start by writing two keywords, public class. 2. Write the name of the class, HelloWorld. 3. First line of your code looks like: public class HelloWorld 4. Any code that you might write next for the class HelloWorld must be put after the first line and it must be enclosed with braces (i.e. { } ). The two slashes // denote the beginning of a comment. 9 public class HelloWorld { // all code must lie between the two braces that // define the boundaries of the class } Write your first method • Steps in defining a method: 1. First write public static void. 2. Next write the method’s name main. 3. Followed by the method’s parameters String[] args in brackets. 4. Finally followed by the method’s boundaries ( open/close braces { } ). Your code must look like: Note the indentations of the lines of code which make the code easier to read. 10 public class HelloWorld { public static void main(String[] args) { } // end of method } // end of class Write your first method (2) 5. Use the statement System.out.println(); within the method to make it print something in your terminal. For example, System.out.println("Hello, World!"); 6. Your code must look like this: 11 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } Compiling your first class • Click on the button Compile. The compiler will check your code for syntax errors and error messages (if any) are displayed at the bottom of the window. • The final message should be one of the following. Either Class compiled – no syntax errors Or an error message. • Important: after each modification of the code, always compile the new code. 12 Execute the method • Close the Editor and return to the project’s workspace. • Move the mouse on top of the HelloWorld icon, right- click and invoke the method main by clicking on it. • A window will appear and select OK. • A terminal window will appear with the message Hello, World! 13 Loops & Debugging In order to remind ourselves how to work with loops we will modify the HelloWorld program we just created into PrintHelloWorld as shown in lecture slide 20: Note: You will need to save this as a new Class in Blue J 14 public class PrintHelloWorld { public static void main(String arg[]) { for (int i = 1; i <= 10; i++) System.out.println("Hello World!"); } } Loops & Debugging (2) 15 Before we run the code for (int i = 1; i <= 10; i++) System.out.println("Hello World!"); we should be able to work out how many times “Hello World” is printed to the terminal window by quickly drawing out a table. Value of counter i (at start of loop) is I <= 10 ? Output to terminal Increment at end of loop (i++) i=1 TRUE "Hello World!" i=2 i=2 TRUE "Hello World!" i=3 i=3 TRUE "Hello World!" i=4 i=4 TRUE "Hello World!" i=5 i=5 TRUE "Hello World!" i=6 i=6 TRUE "Hello World!" i=7 i=7 TRUE "Hello World!" i=8 i=8 TRUE "Hello World!" i=9 i=9 TRUE "Hello World!" i=10 i=10 TRUE "Hello World!" i=11 i=11 FALSE Loops & Debugging (3) 16 By utilising the built in Debugger in Blue J we can make sure that the values of the counter are as expected and that our program will print “Hello World” the correct amount of times. Before you can start debugging your code you must compile it. Once compiled the section to the left of your code will turn white (see figure below). Loops & Debugging (4) 17 We can then add breakpoints in our code. Breakpoints will halt the code execution when we run it, allowing us to inspect the values held in variables in the currently executing methods. Loops & Debugging (5) 18 Once are breakpoints are in place we can then execute the method as we did with HelloWorld. The method will execute until the first breakpoint and halt, displaying the debugging window. The debugging window shows the current values in all variables within the method. We then continue execution from one breakpoint to another (until the program ends) by using the step button. Try it yourself! Adapt the code you have written so far and using the for loop code from lecture slide 19: create a class that computes the sum of the first 10 natural numbers. Use the debugger for step-by-step execution and inspect the value held in the variable sum on each step. 19 for (int i = 5; i <= 10; i++) { sum = sum + i; } Additional Exercise* Write a program that computes the factorial of a given natural number as input by a user (passed as a command-line parameter). To do this you will have to import the Scanner java utility package at the beginning of your code. If you cannot remember how to import java utilities, or the algorithm used to compute the factorial of a given natural number, do look these up, but do try and work out the correct way to code this in java on your own as this type of exercise is often used in ‘in class tests’ or exams. *Additional, NOT optional! 20 Use of Debugger Read Section 7 of the official BlueJ tutorial at: http://www.bluej.org/tutorial/tutorial-201.pdf which is also attached to the hand-out. 21