• No results found

Introduction to Java and Eclipse

N/A
N/A
Protected

Academic year: 2021

Share "Introduction to Java and Eclipse"

Copied!
30
0
0

Loading.... (view fulltext now)

Full text

(1)

Introduction to Java and Eclipse

Algorithms and Data-Structures Exercise

(2)

Outline

1 Introduction

Motivation The Example 2 Setting things up

Setting command line parameters in Eclipse 3 Debugging

Understanding stack traces Breakpoints

(3)

Motivation

This is supposed to be a reiteration - of course, you already know Java

These are useful techniques - you should know them, as they apply to software development in general

These are just suggestions - you are free to do it your own way, the result counts

(4)

The Example

The Traveling-Salesman-Problem

Given a set of cities and the distances between them, find the shortest route that visits each city exactly once.

Solution

This problem is NP-complete - but since it is just an example we use brute force anyway

We need to read in a file that gives us the cities and their distances Also, we want to fix a starting city

Both, the file and the starting city should be command-line arguments

(5)

Setting command line parameters in Eclipse

Suppose we have written a class CostMatrixthat has a static method parsefor parsing the input file

(6)

Setting command line parameters in Eclipse

If we run this (Run ->Run), we get an error:

(7)

Setting command line parameters in Eclipse

(8)

Setting command line parameters in Eclipse

The appropriate tab is fittingly called Arguments:

Note that you have to run the program once for Eclipse to generate a default run configuration (it will have the same name as the project)

(9)

Understanding stack traces

Sometimes there are errors in our programs - if we are lucky, the program crashes

Java will present us with astack trace in this case:

Each level of the stack trace represents an active function call at the time of the crash

We do not only know in which function the program crashed, but also where this function was called from

The larger a program gets, the more valuable this information becomes

(10)

Understanding stack traces

Since it is a stacktrace, the most recent call is at the top (you will learn about stacks in detail later this semester)

In Eclipse, you can click on the source-links to get to the point of the error or call

(11)

Understanding stack traces

The error happened in the functionCostMatrix.getCostthat returns the distance between two cities:

(12)

Understanding stack traces

This method was called from the overloaded method CostMatrix.getCostthat calculates the cost of a route:

(13)

Understanding stack traces

(14)

Understanding stack traces

(15)

Understanding stack traces

(16)

Breakpoints

Unfortunately, this is not enough information to solve the problem We need information about the runtime-state

We can get this using the debugger along with a breakpoint A breakpoint marks a point in our program, at which we want to pause the execution in order to inspect the state of the program

(17)

Breakpoints

To set a breakpoint at a specific line in the code, go to that line (by clicking on the link in the stack trace) and toggle a breakpoint:

(18)

Breakpoints

If we execute our program as usual, nothing special will happen, because breakpoints only have an effect in debug-mode:

(19)

Breakpoints

Eclipse will ask you if you want to switch into the Debug-perspective - you should do that

The program will then run until it hits the breakpoint (which will be immediately in our simple example)

(20)

Breakpoints

In the top right, the Variables-view shows us all variables that are currently in scope,and their current values:

This tells us thatgetCostwas just called with the parameters "Berlin"and"Dresden"

(21)

Breakpoints

If we move one step up in the call-stack (you can click on each call in the current call-stack), we see this:

The surrounding call to getCostwas called to calculate the total cost for the route[Berlin, Dresden, Hamburg]

(22)

Breakpoints

First, lets try to reproduce the crash by stepping through the program:

Step Over executes the program until the next line of code in the current function (or, if the function returns, it goes on to the point it was called from)

(23)

Breakpoints

As we can see, the error did indeed occur:

So, what happened in this line?

(24)

Breakpoints

If we inspectcitiesin the Variables-view, we see the problem:

Neither"Dresden"nor"Berlin"are incities- this should not happen

(25)

Breakpoints

So, how to fix this problem?

First, check if input data is parsed correctly - this can be done by setting a breakpoint directly after the code that loads the data:

Everything looks fine - so apparently, some entries get removed from citiesat some point in the program

We can go through the program step-by-step to find the line that deletes the cities

(26)

Breakpoints

(27)

Breakpoints

(28)

Breakpoints

In general, we can of course do this without a debugger, by inserting print-statements throughout our code

Debuggers take some getting used to, but once you get the hang of it, you will find it much easier and cleaner

There are much more features (for instance Watchpoints to break when a variable is accessed or modified)

(29)

Extra Hint - Use Assertions!

Use assertions to check if certain assumptions or pre-/post-condition are hold true:

This assertion will throw anAssertionErrorif localBestRoute does not contain the same amount of cities ascities

Assertions are intended to protect you from programmer mistakes (speakyour own mistakes)

(30)

Resources

These slides will be uploaded to the website of this exercise http://icsa.uwgb.edu/~songh/sc/EclipseDebuggerTutorial/ There will be a screencast demonstrating the example on the website

References

Related documents

c+c%+c'ccc#c c Œou shouldn¶t go to India without visiting the ajMahal.c Oo deberías ir a la India sin visitar el TajGahal.c I¶minterested in studyingpsychology.c!c@stoy interesado

UPnP Control Point (DLNA) Device Discovery HTTP Server (DLNA, Chormecast, AirPlay Photo/Video) RTSP Server (AirPlay Audio) Streaming Server.. Figure 11: Simplified

Results of the survey are categorized into the following four areas: primary method used to conduct student evaluations, Internet collection of student evaluation data,

Being set out from the ethical decision making model mentioned above, in addition to investigate the effectiveness of contemporary literature in accounting ethics education,

For the poorest farmers in eastern India, then, the benefits of groundwater irrigation have come through three routes: in large part, through purchased pump irrigation and, in a

The rest of the thesis is structured as follows: in Section 2 we provide some theoretical background on regular types and their representation; in Section 3 we introduce CiaoPP’s

cDNA pools generated from circulating EM28 ⫹ and EM28 ⫺ NY-ESO-1- specific T cells at different time points before and after vaccination as well as cDNA pools from NY-ESO-1-specific

Nazario Moreno, the enigmatic spiritual leader of the cartel, was allegedly killed in a shootout with government forces in December 2010 and soon after various factions