![]() |
CPSC 211 |
|
Before the Lab
ObjectivesThis lab will serve as a refresher for Unix, Java, and Eclipse. You will be (re-)introduced to the Unix command line and Eclipse by compiling and running a small application both from the command line and in Eclipse. In this lab, you will:
For those of you who have never used Eclipse, here is a link to a short Eclipse tutorial. It would be a good idea to look at this tutorial even if you have used Eclipse before, since it also tells you how to set it up to run Java. If you know C++, but have never used Java, we recommend that you take a look at the following page: http://www.ugrad.cs.ubc.ca/~cs219/CourseNotes/Java/intro.htmlThe first few sections (up to and including the section on Inheritance) discuss the topics that are covered in CPSC 111 at UBC. The first section of this lab introduces you to some basic UNIX commands. If you would like to learn a few more really useful Unix commands, we recommend that you consult http://www.ugrad.cs.ubc.ca/~cs219/CourseNotes/Unix/intro.html(with thanks to Karen Brennan, Daniel Ferstay, and Christina Wong for their work on these notes). Introduction to UnixDesktopThe UNIX desktop environment you see on your screen is managed by a window manager program called fvwm.
There are many different window managers and desktop environments available, but
XtermThe Xterm is a window where you can enter UNIX commands. You will use commands to manage files, run programs, and make programs. An Xterm window contains a UNIX shell command prompt that has the name of the computer server you are connected to followed by the command number surrounded by angle brackets.
UNIXhelpThe following site provides a helpful introduction to UNIX: http://unixhelp.ed.ac.uk/index.html You are not expected to read through the entire site but keep it in mind. During this lab you are expected to read through the following sections of the site to learn how to copy, delete and move files and directories: Note that at the end of the lab, your TA will ask you to do one or more of the following tasks:
Please make sure that you are comfortable performing these tasks before moving on. Compiling and Running a Java Program on a UNIX systemUnzipping and compiling .java source files:
Introduction to EclipseNow we'll use the Eclipse Integrated Development Environment (IDE) to compile and run the paddle ball application. To begin, you will need to set up a new project and import the source code that makes up Paddleball into it.
A note on package naming... As much as possible in this course, we will use full package names for code that start with "ca.ubc.cs.cpsc211". This is a good habit to get into because when you develop software professionally you have to make sure that the naming is correct. Java relies on unique hierarchical naming to help ensure a Java system never has two packages of the same name, which could potentially lead to problems in resolving which code should be used for a given class. Saving your Eclipse CodeMost programmers store their code in a source code repository that maintains different versions of files on which they have worked and that helps them share code with other programmers. You will learn more about source code repositories in CPSC 310. For now, we recommend that after each lab or each significant work session on your code, you snapshot the code from Eclipse into a safe place in your directory (say ~/cs211-archive). Creating a snapshot is easy:
It is possible that your Eclipse workspace can be corrupted. In most cases, we will be able to help you recover your workspace. As your directories are backed up only periodically, we strongly recommend the snapshot approach as well. Running JUnit Tests in EclipseJUnit is a framework that makes it easy to create and run tests for a piece of software ( a unit like a class ). Instead of writing a test driver, with JUnit we create a test class (as a subclass of TestCase that is defined in the JUnit library) and write tests as methods of this class. Each test method's name starts with test (e.g., testMyMethod()) and performs one or more tests for a method of the class that is being tested. Instead of using print statements for output, JUnit tests call various assert functions to compare expected and actual results. One more fact you need to know about JUnit tests is that there is a setUp method that can be defined in a JUnit test class that gets called automatically before each test method is called in the class. This section will NOT show you how to define JUnit test. We'll do that in a later lab. In this lab you will just learn how to use Eclipse to run JUnit tests that we have prepared for you. As an example we have defined two simple classes: 1) CreditCard that holds the information for a credit card and 2) a class CreditCardTest that contains tests for the CreditCard class. To download the code for these classes and create a project for them you should follow the next steps:
You should now have two packages in your project: ca.ubc.cs.cpsc211.ccard and ca.ubc.cs.cpsc211.ccard.test. The first package contains the CreditCard class and the other has the CreditCardTest class. (It is common practice to separate the JUnit tests into a separate package that ends in "test".) Running CreditCardTestIn Eclipse, JUnit test classes do not need a main()
method to run because the test classes are run in a special JUnit mode. When
running in JUnit mode the output/results of the test run are reported via
the JUnit view which opens automatically when a JUnit Test is running. Select CreditCardTest.java in Package Explorer and right-click on it. Then select Run As...-> JUnit Test. Remember that because CreditCardTest has no main() it cannot be run as a Java Application. The JUnit view will open, sharing space with Package Explorer. In the JUnit view there are two panes: a top pane and a bottom pane. The top pane displays a list of the tests that were run. The Failure Trace pane (at the bottom of the JUnit window) displays information about a particular test case that is selected in the first pane. If the test that is selected passed, then the Failure Trace pane does not show any information. When you run CreditCardTest.java, you can notice a few things immediately in the JUnit view. The bar at the top of the view is red. A red colour indicates that tests have failed. When you are running JUnit tests, the goal is typically to keep the bar in the JUnit view green, which indicates all tests have passed. The JUnit view you are looking at now should show four passing tests, indicated with green checkmarks, and three failed tests, indicated with blue "x"s. Typically, your goal in using tests is to solve problems with the software. So, we will focus on the the failures. Double click on the first failure (should be testGetCreditLimit()). The bottom pane will show a trace for this failure and the method testGetCreditLimit will be highlighted in the editor. A number of lines in the Failure Trace for testGetCreditLimit refer to the JUnit methods and you should ignore them. Two lines in that list are important: the first line that explains the failure and the 6th line( at ca.ubc.cs.cs211.....) that shows the line in the code that produced the failure. The rest of the lines in the failure trace show method calls to the JUnit and Java libraries that were executed before and after the failed line in the CreditCardTest was executed. You can safely ignore these lines and focus on the 6th line that points to the failed statement in the code of our test. If you double-click on the 6th line, the corresponding line in the code is highlighted. From the message on the first line, we can see that the statement assertEquals(card2.getCreditLimit(), 1000, 0); failed because card2.getCreditLimit() is 0 not 1000. We now have to determine why this assertion is failing. Here are some steps we can take:
Now you'll see only two failures in the Junit pane. Double click on the first failure that now appears in the list and find the line that failed. What could be wrong here? An initial point to start from is to consider that getBalance() works correctly. Since setup() would have run just before this test, the balance must have started at zero, getBalance() would return 0 and the value of b-200 being -200 would be correct. If so, then there must be a fault in the addPayment() method. Take a close look at the code for the addPayment method. (Put your cursor on addPayment and hit F3.) Is the if statement correct? We want to process the payment if the payment is positive and ignore it if it is negative. Correct the condition and run the test again. Now you'll see only one failure. Use the same procedure to find out what the error is. Correct the error and run the test again. You'll discover that there is still another failure in the same method. You also need to figure out this error, correct it and run the test once more. In this case it should produce no failures. Sometime before the end of the lab your TA will come and check your progress. It will help if you keep track of what you did to correct the errors and show the TA the final test. Helpful Tipsxlock - Always use xlock to lock your screen whenever you have to leave your terminal unattended. Your screen will be locked until you enter your password. However after 15 minutes, a public logout button will appear and someone can log you out and take over your terminal. text editors - You are free to use whatever text editor you like when you program, but in the future you may want to take advantage of some of the advanced features of emacs. Emacs can be a little bit tricky, but can be of great help later on. UNIX shell - There are a few shorthand abbreviations that you can use in the shell to make navigation easier:
ampersand - The ampersand (&)indicates that you wish the program to run in the background. Space between the program name and the ampersand is OK too. You will notice that if you run the program without the ampersand, the prompt will not return until the program exits. By running the program in the background, the prompt comes back right away and you can continue to use the command prompt. You will usually run programs that make their own windows in the background. more - A very useful utility for viewing text files one screen at a time. A similar program called less is more powerful (In UNIX less is more than more). View the man page for less if you want more information. man - The online reference manual viewer. To find out about a program, you can usually learn something from its man page by entering the command:
|
||
ŠUniversity of British Columbia | Last updated:
August 28, 2008 |