Download Lab 6 BlueJ, a java IDE

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Library (computing) wikipedia , lookup

Object-oriented programming wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Java performance wikipedia , lookup

Transcript
CPSC 150 Laboratory
Lab 6
BlueJ, a java IDE
Overview
In the first week of the lab, a java editor was presented as a tool for creating java source
files. Hopefully you understand how jEdit works and are comfortable with the purpose it
has in the process of creating programs. Fortunately there are lots of other tools for
development, some of which are specialized and others the are complicated and featurerich for more advanced programmers.
Now we turn to an editor that supports the development of java programs with a special
emphasis on creating objects. You should be at a point in your reading where you are
being introduced to objects, or you will be in the near future, and we will be using BlueJ
to develop programs for most of our activities during the rest of the semester.
You will also find that this IDE (integrated development environment) will allow you to
execute and test programs without leaving the IDE. I.e., you can run the programs from
within BlueJ without using the command-line.
Recall the idea of abstraction. BlueJ allows us to more effectively create these
abstractions critical to good programming. Nonetheless, some of this may seem a little
odd at first, so be patient as you learn about BlueJ and objects. Both are needed to get the
full impact of object oriented programming (OOP).
In this lab we will cover the basics of creating and running programs with BlueJ, then
investigate how to use the debugger to test programs and look for errors. Initially little
attention will be placed on objects, with more attention placed on OOP in the labs that
follow.
BlueJ has already been installed on the lab machines, but videos on installing and
configuring BlueJ on your own machine are located on the course website.
CPSC 150 Laboratory
Running BlueJ
A. The first BlueJ program.
Watch the video
Running BlueJ
for an introduction to the use of the BlueJ IDE.
When you finish watching the video, enter the program HelloWorld, compile and run it.
Question 1
Show your instructor your work when you are finished.
B. The PersonName object
On the web site you will find a file, PersonName.java. This file represents two aspects of
OOP for you to explore. In this lab we will explore a number of areas of importance. First
we will explore our first object library in some depth, Strings. Second we will examine in
some detail, how to write a class library and to create an object using the library, in this
case the PersonName class. Third we will use the debugging capabilities of BlueJ to fix
some errors in the PersonName object.
1. The video and Question 1 illustrated how to create a project. In some cases, as
with PersonName.java, you have the source file but no project. Let’s create a
project from a simple java file. First create a directory named PersonName. Then
download the PersonName.java file and store it in the PersonName directory.
Now in BlueJ, select File/OpenNonBlueJ, navigate to the directory containing the
file and open. It will add the necessary files into the directory to make it a valid
BlueJ project. Be sure to name the directory correctly and store the file in that
directory or BlueJ will not open it, even if you indicate it is nonBlueJ.
Compile and run the program. Your output will look like:
First:obb
Middle:an
Initial:
Last:Smith
First:enn
Middle:
Initial:
Last:Lane
First:Brian
Middle:
Initial:
Last:
The program obviously has some problems. If you look at the main routine, you
can see that the output should be parsing names as
First:Bobby
Middle:Lane
Initial:L
Last:Smith
….
Your lab requirement is to look at the “java
Strings, look at the methods being used by
trim()), determine what they do and try to fix
changes to make should all be made to the
wholename).
6 docs”, find the specifications for
the program (substring(), length(),
the program to work correctly. The
method PersonName (String
Before you get too excited, let’s use the debugger in BlueJ to investigate what is
wrong. An examination of the program will reveal that the PersonName method is
trying to separate (parse is the correct term) the name into First, Middle and Last
names. Expected output is shown below.
Name
First
Middle
Last
Bobby Lane Smith
Bobby
Lane
Smith
Penny Lane
Penny
Brian
Brian
Lane
The problem is that the method is calculating the position of the error incorrectly.
So the problem is to decide where exactly the error is occurring. Here’s where the
debugger comes in. Simply use the source editor window (double click on the
PersonName icon/class). Go to the line just following “if (posBlank>0)”, and click
once in the narrow column on the left, on the same line as this statement. A STOP
sign should pop up. If it doesn’t, get some help before moving on.
CPSC 150 Laboratory
Now compile and run the program (main), just like you ran the HelloWorld. When
execution reaches the line with the STOP, the program will halt, pop up a window
with the values of all of your variables and wait for you to tell it what to do.
Question 2
Get your instructor to verify you are at the right place before proceeding.
2. Once approved, execute the rest of the program by “STEP”ing. I.e. click STEP
repeatedly. On each STEP, check the values of the variables and try to determine
which ones are wrong. Then discuss the problem with your peers and come up
with a plan to fix the problem. Make your changes, be sure you still have the
debugger set to the same place and run it again. As soon as you see a problem
that you want to fix, click STOP, make your change(s) and repeat.
Question 3
Question 4
When you get the correct answers, show your instructor the changes you made and
your correct answers.
3. Include a method named FML that represents the three initials. “Bobby Lane
Smith” -> “BLS”, “Penny Lane” ->”P L”, “Brian”->”B “, then test it by
incorporating the appropriate calls in your main routine.
Student’s Name _____________________________________
ID _______________
Lab 6: Completion Table
Question
Completed Comments
yes
no
1


2


3


4


Instructor’s signature _______________________________

Grade ____________