ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

Similar documents
CS520 Setting Up the Programming Environment for Windows Suresh Kalathur. For Windows users, download the Java8 SDK as shown below.

2 Getting Started. Getting Started (v1.8.6) 3/5/2007

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Getting Started (1.8.7) 9/2/2009

Software and Programming 1

Comp Assignment 2: Object-Oriented Scanning for Numbers, Words, and Quoted Strings

Software and Programming 1

SDKs - Eclipse. SENG 403, Tutorial 2

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Testing and Debugging

HOW TO USE CODE::BLOCKS IDE FOR COMPUTER PROGRAMMING LABORATORY SESSIONS

CS 201 Software Development Methods Spring Tutorial #1. Eclipse

S8352: Java From the Very Beginning Part I - Exercises

ICOM 4015 Advanced Programming Laboratory. Chapter 4 Introduction to Object Oriented Programming

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

C02: Overview of Software Development and Java

COMP 110/401 APPENDIX: INSTALLING AND USING ECLIPSE. Instructor: Prasun Dewan (FB 150,

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

7 The Integrated Debugger

CS 106 Introduction to Computer Science I

Laboratory Assignment #4 Debugging in Eclipse CDT 1

13 th Windsor Regional Secondary School Computer Programming Competition

Java Programming Constructs Java Programming 2 Lesson 1

Summer Assignment for AP Computer Science. Room 302

1.00 Lecture 2. What s an IDE?

Javac and Eclipse tutorial

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Lab 1: First Steps in C++ - Eclipse

At the shell prompt, enter idlde

Eclipse Tutorial. For Introduction to Java Programming By Y. Daniel Liang

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

Mobile App:IT. Methods & Classes

Lecture 1 - Introduction (Class Notes)

Java Program Structure and Eclipse. Overview. Eclipse Projects and Project Structure. COMP 210: Object-Oriented Programming Lecture Notes 1

Using Eclipse Europa - A Tutorial

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

CMPSCI 187 / Spring 2015 Hangman

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Code::Blocks Student Manual

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

Mechatronics Laboratory Assignment #1 Programming a Digital Signal Processor and the TI OMAPL138 DSP/ARM

Getting Started with Eclipse/Java

Introduction to Programming Using Java (98-388)

Introduction to Computation and Problem Solving

An Introduction to Komodo

CSCI 161: Introduction to Programming I Lab 1a: Programming Environment: Linux and Eclipse

Lab 0 Introduction to the MSP430F5529 Launchpad-based Lab Board and Code Composer Studio

Just Enough Eclipse What is Eclipse(TM)? Why is it important? What is this tutorial about?

PART 1. Eclipse IDE Tutorial. 1. What is Eclipse? Eclipse Java IDE

Your password is: firstpw

Hello World on the ATLYS Board. Building the Hardware

DOMAIN TECHNOLOGIES. Getting Started Guide Version 1.1. BoxView IDE. Integrated Development Environment

CS50 Supersection (for those less comfortable)

Sun ONE Integrated Development Environment

Eclipse CDT Tutorial. Eclipse CDT Homepage: Tutorial written by: James D Aniello

CS 231 Data Structures and Algorithms, Fall 2016

Workbook A dialog box will appear asking for you to select your "workspace". Leave the default as is, which should be /home/crsid/workspace.

The User Experience Java Programming 3 Lesson 2

Using Eclipse for C, MPI, and Suzaku

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

INF 111 / CSE 121. Homework 3: Code Reading

Math Modeling in Java: An S-I Compartment Model

Lab #1: A Quick Introduction to the Eclipse IDE

CHAPTER 1INTRODUCTION... 3 CHAPTER 2INSTALLING ECLIPSE...

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Introduction to Eclipse

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Implement an ADT while using Subversion

Assignment 1. Application Development

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

CPS109 Lab 1. i. To become familiar with the Ryerson Computer Science laboratory environment.

Debugging in AnyLogic. Nathaniel Osgood CMPT

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Introduction to Java Applications

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

3 Getting Started with Objects

1. Go to the URL Click on JDK download option

Debugging in AVR32 Studio

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Prerequisites for Eclipse

In this lab we will practice creating, throwing and handling exceptions.

CSCI 161: Introduction to Programming I Lab 1b: Hello, World (Eclipse, Java)

Resource 2 Embedded computer and development environment

Programming Basics. Digital Urban Visualization. People as Flows. ia

Lab 9 Loops, Debugging

IBM WebSphere Java Batch Lab

Code::Blocks Student Manual

An overview of Java, Data types and variables

ActiveSpaces Transactions. Quick Start Guide. Software Release Published May 25, 2015

Eclipse. JVM, main method and using Eclipse. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

News in RSA-RTE 10.2 updated for sprint Mattias Mohlin, May 2018

3. NetBeans IDE 6.0. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Programming with Java

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Building and Running a Simple UML RT Model in RSARTE

Department of Computer Science University of Pretoria. Introduction to Computer Science COS 151

Transcription:

ICOM 4015 Advanced Programming Laboratory Chapter 1 Introduction to Eclipse, Java and JUnit University of Puerto Rico Electrical and Computer Engineering Department by Juan E. Surís

1 Introduction This chapter is designed to introduce Eclipse, Junit and the basics of debugging for those of you who have never used an integrated development environment (IDE). It also assumes that this is the first time that you will be programming in Java. After working through the chapter and all the exercises, you will be able to create projects, classes and unit tests as well as perform some basic debugging. 2 Getting to know Eclipse Eclipse is an open source IDE available for the most common operating system platforms. It provides a powerful programming environment rich with features that make software development easier. In this course we will be using only a handful of these features, some of which we will discuss in this chapter. In this section we will get acquainted with the basic functionality needed to start programming in Java using Eclipse. 2.1 Workspace Before we start Eclipse, we must decide where all our work will reside. The code we will write will be saved in various files and we must now choose where these files will be saved. This area is what in Eclipse is known as the workspace. We may have more than one workspace, but typically all the code related to a single activity will reside in the same workspace. The laboratory instructor will instruct you where you may create a directory to serve as your workspace. Figure 1: Create a new Java project 2.2 Project Within your workspace you want to create groups of files that belong to the same activity. As you will see over the course of the semester, each activity will consist of many files. We also wish to keep files from different activities separate. In Eclipse, a group of files that belong to a single activity is known as a project. Figure 1 shows how to create a new project in Eclipse. You may also create a new project by the following menu sequence File New Java Project 1. When we create a new project, we need to enter some information regarding the project. Figure 2 shows the window where we specify the project information. First, we must give 1 From now on, we will use this shorthand to indicate a menu sequence 1

Figure 2: Configure new project the project a name. Always try to give the project a meaningful name that describes the activity that the project implements. In this case, we will name the project HelloWorld. Make sure that the JRE selected is JavaSE-1.6. You should leave all other information as default. You may click Finish to create the project. Figure 3: Create a new Java class 2.3 Main class A computer program is nothing but a sequence of instructions that a computer can execute. Like all sequences, a computer program must have a well defined point to start the execution. For those of you with experience in C/C++, this starting point is the function called main. In Java there also exists a function (called method) main which indicates the starting point 2

for the program. Since all code must be part of a class, the main method must belong to a class. The class that contains the main method is called the main class. Let s go ahead and create the main class, called HelloWorld, as shown in Figure 3. You may also create a new class by File New Class. Figure 4: Configure the main class Just like we did when creating a new project, we must provide some information regarding the class. Figure 4 shows the windows where we specify the class information. Notice that we need to provide the package it belongs to. For now we will let it belong to the default package, although Eclipse warns that that is discouraged. Give the class the name HelloWorld. Since this is the main class, we will ask Eclipse to create the stub (or signature) for the main method. This is done by selecting the checkbox as shown in the figure. The stub will look like this: public static void main(string[] args) Now that the main class has been created, we are ready to add some code and create our first Java program. As is traditional with Hello World programs, we will add code to write a single line to the console that reads Hello world. This is accomplished by adding the following line of code to the (empty) body of the method main: System.out.println( Hello world ); We now have our first executable Java program. Figure 5 shows how we execute a Java program in Eclipse. We can also do Run Run. The output of the program is shown in the console window, as shown in Figure 6. 3

2.4 A closer look at Eclipse Figure 5: Execute HelloWorld Before we continue with the rest of the chapter, let us take a closer look at Eclipse and identify the most important aspects, which we will be using and interacting with throughout the semester. Figure 6 shows the Eclipse window right after execution of the HelloWorld program. We just looked at the console. If we were to execute this program in a command line, whatever would appear on the screen is shown in the console window. Figure 6: Snapshot of current project The leftmost window is called the Package Explorer. In this window, we can see the projects that are in our current workspace. By double-clicking on a project, we can see a tree of all the files that belong to the project. We can see that there exists a file under our project, called HelloWorld.java. This file contains all the code for the HelloWorld class we just created. Java stores each class in its own.java file and in this window we can browse the files for each class in the package. Also, notice that HelloWorld.java resides under the 4

entry src. This means that the file is in the source directory called src. Later on we will create another source directory. Having different source directories helps us separate different portions of our project. Next we have the open file tabs. Here Eclipse displays all the source files we have opened for browsing and/or editing. In this case, when we created the class HelloWorld, a tab was for the source file was opened by Eclipse. We can also open a tab for a given source file by double-clicking the file name in the Package Explorer or by File Open File... and then selecting the file. Try closing the file by clicking on the x on the tab and opening it again. Now let us examine the file tab for HelloWorld. In it we can see the declaration of the class. We can also see the body of the main method. The file tab is the place where we will be interacting the most with Eclipse while we are programming. Finally, at the upper right corner we can see the Perspective Menu. A perspective in Eclipse is an arrangement of windows within Eclipse. This concept is very useful, as we desire different window arrangements depending on the task we are performing. The current perspective is Java. Later on, we will switch to the Debug perspective. 3 Another class In this section we are going to create another class that we will use in the main method of the HelloWorld class. Go ahead as before and create another class named Summation. This time do not select the checkbox to create the stub for a main method. You should now see in the Package Browser a file named Summation.java and a new tab for the file with the class declaration and an empty body. 3.1 Code for Summation class Copy the code in Figure 7 (by hand!) in the Summation. The code for the Summation class consists of a member field, a constructor and a method. The member field is public, of type int and is named sumub. The method is public, has a return type of int and is named dosum(). By inspecting the code, we can gather that the method calculates the sum of the integers from 1 to sumub 2. Constructors will be discussed in a later chapter. Now modify the code of the main method of the HelloWorld class as in Figure 8. Execute the program as before. You should see results in the console as follows: Hello world The sum of 1 to 20 = 210 Now change the value of s.sumub in method main to 240000. What is the result? Can you explain why? Finally, let us add some comments to our Summation code, so that when we look at this code 2 years from now we can remember what we did (or somebody else can use it without having to read the code). Fortunately, Eclipse can get us started by creating a placeholder for our comments by simply placing the cursor on the line containing the element we wish to create the comment on and do Source Generate Element Comment. Fill in the comments with something appropriate. 2 Actually, there is a an error which we will catch later. 5

Figure 7: Code for Summation class Figure 8: Code for main method 4 Unit Testing A crucial part of programming is testing the code we write to verify that the functionality is as we intend. Unit testing is a verification technique where programs are broken done into units. We then write test(s) for each individual unit. Each test case is self-contained and individual from the others. For the purposes of this course, most of the times a unit will be a method. In this section we will try unit testing with JUnit and Eclipse. 4.1 Unit test for Summation class As mentioned above, usually our units are methods. In this case, the unit we wish to test is the method dosum(). They way we will test this method is by creating an instance of a Summation object, set a value for the sumub and verify that the method returns the correct value. 6

To create a unit test, first we will create a new source directory to store the unit test. It is convenient to store the testing code separate from the rest of the code. Do Project Properties Java Build Path and select the Source tab. Click the Add Folder... button, then the Create New Folder... button. Name the new folder Test and click Finish. You should see the new folder in the Package Browser. Figure 9: Create a new JUnit test (a) Window #1 (b) Window #2 Figure 10: Configure the new JUnit test Figure 9 shows how to create a new JUnit test. You may also do File New JUnit Test Case. Just like when we created the project and the class, we need to provide some information about the new test. Figure 9 shows the windows where we provide the information. 7

On Figure 10(a) notice the option to create a JUnit 4 Test was selected. Throughout the course we will be using JUnit 4. We must change the Source folder to HelloWorld/Test and name the test test dosum. Specify the class under test as Summation and click the button Next. This leads to the window shown in Figure 10(b) where we specify the method(s) we are going to test. Select the method dosum and click the button Finish. Notice on the Package Explorer that the test should be under the source directory Test. Eclipse may ask if you want to add JUnit 4 library to the build path. If so, select Yes. Type the test code (by hand) as shown in Figure 11. Most of the code should have been generated by Eclipse. Run the test the same way you have run the previous programs. Figure 11 shows the state after the test has been run. Notice that where the Package Explorer used to be there is a summary of the tests run and a Failure Trace. We can see from the Failure Trace that our test failed. In the next section we will figure out why. Figure 11: Code for new test 5 Debugging While we always strive to write our code perfect the first time, seldom are we so lucky. We have just discussed how to test our code to verify functionality. We actually found that the code does not actually work as it should, since our test failed. Testing helped us find out there was an error, debugging is the technique we use to find out why and where the error is so we can fix it. The essential element of debugging is what is called a breakpoint. A breakpoint is a signal that tells Eclipse to temporarily suspend execution of the program or test at a certain point. Once suspended, we can examine the state of the program, such as variable values. By observing the state of the program, we can verify that it is as we expect it to be. 8

To facilitate debugging, Eclipse provides us with a Debugging Perspective. Change to the debugging perspective by clicking the Debug button in the Perspective Menu, or by doing Window Open Perspective Debug. Figure 12: Debug Perspective Figure 12 shows the window arrangement in Debug Perspective, while a debugging session is active for the execution of HelloWorld. To start the debug session, first we need to set a breakpoint. Go to Summation.java and place a breakpoint on the line shown in the figure (13). You can place a breakpoint by double-clicking on the blue horizontal bar next to the line of code where you wish to place the breakpoint. The mouse point in Figure 12 shows how to set the breakpoint in the desired line of code. Now that we have the breakpoint we desire, we can start the debugging session. Figure 13 shows how to start the debugging session. You may also do Run Debug. Figure 13: Start debugging session Now that the debugging session is started, your program should have executed up tu the line where we placed the breakpoint. You should see exactly as in Figure 12. The first area we want to notice is the Variables tab. We can see the value of the variable in the current scope: i and sum. Since this is the first iteration of the for loop, the values as are expected. 9

Once the execution of a program is suspended, we have several options as to how to proceed. In the debug window, there is a menu with several buttons. Float your mouse over them and Eclipse will pop-up a note with the function that each button performs. The functions that we will be interested in today are: 1. Continue - Commence the execution of the program at the point where it was last suspended. The execution will continue until a breakpoint is reached or the program terminates. 2. Stop - Terminate execution of the program and the debugging session. 3. Step Over - This executes the next line of code. If this line of code calls any methods, the entire code of these methods is executed, unless a breakpoint is encountered. 4. Step Into - This executes the next line of code. However, if this line of code calls any methods, the debugger will enter the code for the first method executed and stop at the first line of code it encounters. This is useful if we want to debug the code for these method calls. Be careful not to try to step into code for method calls of classes you do not have the code to. The best way to get a sense for how these work, continue your debug session trying all of of them. Try to anticipate what will happens and when you are incorrect figure out why. After a couple of debugging session, you should have a good idea of how these work. After you have entered a few debugging session for HelloWorld, select the Test dotest.java tab and start a debug session. This debug session is now executing the unit test we created earlier, which we know failed. Now your job is to figure out why. (Notice that the breakpoints we set earlier are still set). Fix the Summation code so that the method dosum() returns the sum of all integers from 1 to sumub. Show the code to the instructor when you think you have fixed the error. 6 Exercise Create the following: (a) A new project named LetterCountExample (b) A main class for the project named the same of the project. (c) A class called LetterCounter, with the following members: i. A field of type char named letter. ii. A method of return type int, named docount(string) that takes one argument of type String and returns the number of times the character in the variable letter occurs in the given String. (d) Create a Junit test that uses the test string abracadabra and verifies that the method returns the correct value. 10