Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
26/05/2015
1
CSCI222 Objective 2
Use appropriate tools to develop 
and implement a software 
application of moderate 
complexity 
1
Appropriate tools?
• Well – not Unix command line, makefiles, and a text 
editor!
– So very 1980s!
• Appropriate tools means an integrated development environment
– Language aware editor
– Auto-indexing
– Possibly an integrated class browser
– Semi-automated build 
– Good visual interface to debug tools
– Unit testing
– Code coverage
– Integrated web-based version management.
2
Ubuntu only; no Windows 
versions in laboratory
• Back in 2007 when the lab component of CSCI222 was 
first fully implemented, CSCI222 laboratories supported 
both Linux (Ubuntu) and Windows environments
– Being Java based, most Integrated Development Environments run on 
both platforms
• Also allowed students to work on their own machines.
3
No Windows; no own PCS
• It was a disaster
– Students decided they would install software on own Windows 
PCs and never come to the lab.
• Installation on a Windows PC was complex as had to install cygwin
or another C++ environment;
cppUnit libraries in the standard downloads were out of date and had 
to be replaced by newly compiled ones.
• Installation instructions were lengthy – too lengthy for most students 
who decided to install the software without reading the instructions.
• Most Windows installations didn't work.
– When nothing works, try reading the instructions.
– Students wasting time at home, not in laboratories using pre-
configured systems.
4
Use the labs!
• The exercises and assignments are set on 
the assumption that students will work in 
the supervised laboratories using the 
installed software
– Actually
• In assignment 1, a lot of the work is group discussion that can be done 
outside of the laboratories; individual development work on "components" can 
be done in any C++ environment; laboratory only needed for assembly of 
overall system from built components.
• In assignment 2, majority of work involves group discussions and the rest 
involves use of UML design tools that can run in any environment
5
Appropriate tools!
• No cost!
– Several such development environments are now available free
• Initially, IBM sponsorship
– IBM gave away software until the US government (~1969) required 
“unbundling” of software
– IBM still sponsors lots of “open source” software
– In 1990s, IBM acquired a specialist company that produced Smalltalk 
and later Java development environments
– Lead to “Eclipse” environment for Java
– ~2000, Eclipse code donated to public domain (but gets IBM financial 
support)
• Others added C++, Perl, Python etc “plugins” for Eclipse
• Sun copied with free edition of NetBeans, 
• Microsoft now offers free “Express” editions of Visual C#, Visual C++ 
- somewhat cut down but quite sufficient for the average developer (go to 
www.microsoft.com)
6
26/05/2015
2
Appropriate for 
CSCI222?
• CSCI222 : required to be C++ based
• So a C++ IDE
– Supplemented with modern web-based 
version management tools
7
Choices: NetBeans
• NetBeans?
– I have used it for the last 8 or 9 years in the Java sequence 
CSCI213, CSCI399, CSCI398 and tried to get it approved as 
standard IDE for all our CS subjects
• A different lecturer is now taking CSCI213 and he uses Eclipse – so much for standards
– Has support modules for Ruby, PHP, etc
• Used from 2010 for PHP in CSCI110
– Has a universal database client
– Has built in clients for common collaborative version management 
systems
– Has a C++ component.
• Complete with integrated support for unit testing
8
Version management
• Again, choice should be a current technology –
not something like SCCS that has been around 
forever on Unix systems (“originally written in 1972 by 
Marc J. Rochkind at Bell Labs”)
• Picked “subversion”
– HTTP/WebDav interface – checkouts and checkins via 
web
– Subversion clients for NetBeans 
– Other clients exist for more general use
– Has to a significant extent replaced CVS (Code 
Versioning System) as the preferred version 
management system for open source projects
9
Version management
• GIT
– Only a very brief introduction later in CSCI222
– It’s a bit more recent, being developed to support 
open-source, collaborative Linux efforts
• It has better provision for supporting multiple concurrent 
updates by different authors (suitable for a dispersed, chaotic, 
open-source movement)
– Explore it yourself one day – but not during 
Spring session 2015
Exercise 1 includes some very limited use of subversion.
Assignment 1 requires evidence of some use of a version management system
By default, this means using the university supplied subversion server
But you can use alternatives – Google groups, a “GIT” server somewhere, svn somewhere
other than university.  Simply must provide evidence of some experience with version management.
10
UML
• CSCI222’s 2nd assignment is in part a 
UML design exercise
• Various UML editors permissible
– LucidChart UML editor (preferred)
• Collaborative, web-based modern editor
– RationalRose as installed in some CSCI laboratories
– Only requirement on UML editing is that all 
members in a group use the same editor!
11
Working with tools
Fairly limited use – intended 
just to give a taste of modern 
practices.
12
26/05/2015
3
Exercises, assignments 
and tools
• Exercises
– E1 – explore the IDE, learn “testing” technologies (unit 
testing, code-coverage testing, performance analysis)
– E2 – GUIs for C++
– E3 – SQL and NoSQL for C++
– E4 – client-server exploiting libraries
– E5 – modern web-based UML tool
13
Assignments and tools
• Assignment-1 (finish design and implement)
– Each group has associated subversion repository under 
password control
– Each group member has own password
– Individuals collaborate on updating version-managed code-base
– Regular integration builds and tests
– Project report must demonstrate appropriate tool usage
• Assignment-2 (analyse and design)
– Sensible, but not mandatory, that all documents be version-
managed
• Meeting minutes, designs, reviews, reports, …
14
Ubuntu
• Ubuntu in labs 
– major development tools – gnu suite (g++ etc)
– cppUnit – standard library on Ubuntu
• Also Qt, gdbm, mysql, MongoDB, and sqlite libraries added along with some of 
the “Boost” libraries
– Java 1.8.x (current Java; used to run NetBeans and some other utilities)
– NetBeans – 8.02 with C++ modules
• (subversion client as standard)
– OpenOffice
15
Creating 
documentation …
• When preparing reports …  
– Can transfer materials to and from Windows environment and use Word 
etc; need a PDF printer driver on Windows machine
– Simpler to work entirely in Ubuntu using OpenOffice word 
processor 
• Formatted code listings from NetBeans? 
– Use the "Print as HTML" option; 
– Open the HTML file with OpenOffice and cut and past into report.
• Screenshots –
– Gimp installed, has screen capture and editing.
– OpenOffice has an "export as PDF" option to generate PDF final 
versions
16
HELP!
• www.netbeans.org/kb
– Lots of tutorials including some flash based 
recordings illustrating "how to"
17
Starting NetBeans 
C++ development
First steps and a "Hello 
World" example
18
26/05/2015
4
Used NetBeans
before?
• Most of you should have encountered 
NetBeans in CSCI110
– Probably have a few NetBeans created files 
and directories in your home-directory
• Advice:
Get rid of all the old NetBeans stuff before starting 
again in CSCI222
– Remove directories like .netbeans, .netbeans-derby, 
.netbeans-registration, MyDomain, personaldomain, …
– Clear out NetBeansProjects
19
Ubuntu - NetBeans
• NetBeans accessible via “Dash” and can be added to 
your access bar
• The first time you start it, it will take a couple of minutes 
to set up a number of hidden directories and a 
NetBeansProjects directory.
• These will be in your home folder for CS projects (not 
your home folder on main banshee system)
Screen shots in this lecture will
be from a variety of different NetBeans
versions – including some on Windows.
In a few cases, the dialogs displayed by
current NetBeans may differ in minor ways,
but usage is generally the same for all versions. 20
New project 
• Once NetBeans has started, request a 
"New Project"
21
Generated project
• Project view
• File view
Folders like “Header files”
shown in project view are
fictions;
but they are actually useful
and can help keep the project
organized;
After creating a new “header” 
file or cpp file, move it to the
appropriate folder in project
view.
22
Main.cpp
• A generic main
23
IDE window: basic view
Pane used for "Project"
or "Files" view of work.
Project (logical view)
Files (physical view)
Pane for output from compilation, linking, version management
and similar operations
Pane for code editor; syntax
aware highlighting; "code completion"
features etc.
Additional panes
can be opened; look
in "Window" menu
24
26/05/2015
5
Makefiles!
• Let the NetBeans IDE create and manage the 
build process
– It creates its own makefiles for you
– The only time you must intervene is when you use a 
special library (Qt, sqlite, …)
• You have to use a dialog to add the library's header files to those 
used by default in the compilation stage
• You have to use a dialog to add the library itself to the files used 
when linking the executable
25
Editing
• Edit code files in editing window
– NetBeans has code-completion smarts along 
with things like lists of header files
26
Hello world
• What else? It’s always hello world
Current NetBeans uses the “Output” pane for C++
(just as it does for Java); older versions would
open a separate xterm style window:
No output? Truncated output?
NetBeans launches C/C++ program as a sub-
process, getting the output via a pipe.
Often, when sub-process  terminates, 
NetBeans cuts the pipe before all printing has 
completed. Necessary to have “sleep(1);” as 
the final statement before return from main() 
or call to exit().
27
More hello world
• Syntax aware editor flags errors in your 
code (here it’s “end1” instead of “endl”
– Fix things when you notice them being 
flagged!
28
Input
• If your program reads from stdin, you should get a prompt 
and be able to enter data in that “Output” pane:
29
Easy!
OK, how about something more 
realistic – “Hello” programs are so …
Exercise 1 …
30
26/05/2015
6
Exercise 1: testing a 
function
• Sort function and the beginnings of Unit testing
– Little example supplied
• Fill an array with some random values, display
• Invoke “sort” function
• Display “sorted” data
– Initially, sort function has no implementation
• Weird
• No implementation
31
Exercise 1: testing a 
function
• Weird
• No implementation
• It’s part of the “Test Driven Design” ethos
– Set up framework to run code
– Decide on how to might test correct operation of code
– Implement the tests
– Only when have complete test harness set up do you 
actually write the code
32
Sort driver
33
Add cppUnit stuff to 
project
• As covered in more detail later, cppUnit test 
framework involves
1. A driver program
• Entirely standardized!
– Set up things like mechanism to record success/failure of tests
– Set up display
– Create instance of a cppUnit test class
» It’s methods will define the tests
– Run the test class
2. A templated test class
• It has stub methods with tests – replace with your real tests
34
NetBeans adds 
cppUnit
• NetBeans will add the cppUnit driver and a 
testing class to the project
– You then edit the test methods
• In this case, will just want to call the selectionsort() function in 
code that can check whether it’s working correctly
35
Testing a sort 
function
• Generated template class comes with two prototypical 
test functions (testMethod, testFailedMethod)
36
26/05/2015
7
Testing a sort 
function
• Sort function should
– Return an array with exactly the same elements as it was given, 
just maybe in a different order.
• How to test that?
– Well sum of elements should be the same.
– Could make a copy of array before sort, and then one-by-one check that 
each element in “sorted” array was an element in original unsorted array
37
Testing a sort 
function
• What else?
– Well, the “sorted” version of array should have 
data elements in ascending order
38
Try
• Haven’t implemented sort function yet, but 
can try out the test mechanism
39
Implement the code
• When you know that you have a means of 
checking whether your code works, then is 
the time to write the implementation!
40
Happiness is a green 
test bar
41
Usually testing a 
class
• Not common to be testing a function as in sort 
example (but, as shown, you can use the test 
framework to do this)
• More typically, you are incrementally building a 
class
• Build both your class and a cppUnit testing class 
at same time
– Start your class with aconstructor and a couple of 
simple methods (accessor and mutator); have unit test 
class with method(s) to check these
– Add more, test more, re-test previous tests 42
26/05/2015
8
Exercise 1: 
MyRecord
• When you do exercise 1 you will build a 
class MyRecord
– Its data members include vector<> and 
map<,> STL collections
• You will also build and run a matching unit 
test driver
43
Other tests
Code coverage
Performance
44
Exercise 1 also 
covers …
• gcov
– gcov is a standard part of the gcc/g++ development 
tool suite
• No special NetBeans support
– You specify compilation and linking options if you want 
to use gcov
• Compiler then generates extra code that will maintain counts 
of the number of times each basic block in your program gets 
executed
• Run-time code linked in will save these counts to file for later 
analysis
– You run your program a few times collecting data
– You run the gcov analysis tools to get a report 45
gcov
46
Exercise 1 also 
covers …
• gprof
– gprof is a standard part of the gcc/g++ development 
tool suite
• No special NetBeans support
– You specify compilation and linking options if you want 
to use gprof
• Compiler then generates extra code that will measure time 
spent in each function
• Run-time code linked in will save these data to file for later 
analysis
– You run the gprof analysis tools to get a report
47
gprof
48
26/05/2015
9
But how does it run?
Looking inside with the 
debugger … 
49
Debugger …
• The usual:
– Application will be launched in a sub-process 
controlled by the debugger
• Application can be run, control returned to debugger either at 
pre-selected points or when disaster strikes
– Breakpoints
• Set “breakpoints” in your code
• Execution will suspend
• Debugger will allow you to poke around in code inspecting 
values of variables
50
Debugging?
• Advice
– Write correct code
– Write simple, easy to read code
• Debuggers are there to help those that write 
complex, convoluted, buggy code full of little 
features like dangling pointers, and loops that 
run off the ends of arrays etc
• But, of course, it wasn’t your code. It was written 
by a colleague in your group.  It doesn’t behave.
– OK get in there with the debugger
Remember to compile with debug options that save symbol tables etc! 
Actually, that’s NetBeans default setting!  Obviously they trust programmers greatly.
51
Set a breakpoint in your 
code
Just click in this side bar
52
Run, in debug mode
53
Inspect variables
• Can look at simple variables
• Can view call stack, and open up any stack frame
54
26/05/2015
10
Stepping
• You can single step execution if you really 
want to see how something is working
55
Using a version 
management system
Subversion
(svn)
56
Subversion client is built 
into your NetBeans
• Our usage is simple
– Start by
Create a project
or
Checkout a version of an existing project
– Commit changes
All the hard work is done by the administrator of the subversion repository
57
Svn repository
• Machine “virindi” runs a subversion system
– Some directories with public read access
– “Group directories”
• Intended for your use in Assignments 1 and 2
– Practice use of shared repository
– Create project
» Add files
» Colleagues check out copies, maybe update your files and 
check the updated versions back in
– Keeps track of group work
– Allows you to get back to a working version if some recent 
change breaks things!
58
Exercise 1 – just 
check out a project
• “MyRecord” project
– MyRecord
• A class representing details about an employee of an 
imaginary software firm
• Versions of this get used in Exercises 1..5
• Subversion – web-interface
– You can view publically accessible subversion 
repositories with normal web browser, and download 
individual files:
59
Download into 
Netbeans
• If repository contains NetBeans projects, 
these can be downloaded into NetBeans
60
26/05/2015
11
Download into 
Netbeans
• Only one version exists of that project;
if multiple versions, you can select the one that you want
61
UML
LucidChart
62
Exercise 5
• You don’t get to use UML until exercise 5
• There make use of LucidChart – a modern web-
based UML editor
In CSCI205 and other SE subjects you will learn to use Rational Rose – an older more complex UML editor
63
Objective-2 : use 
appropriate tools
NetBeans, cppUnit, subversion, 
and UML editors!
64
Act professionally (or at 
least fake it)
• Professionals use modern development 
tools
• CSCI222 is meant to give you a chance to 
play with such tools.
• Play enthusiastically.
65
C++ project problems
66
26/05/2015
12
Problems with 
NetBeans C++ projects
• I’ve frequently been caught by two problems with NetBeans C++ 
projects
– Both relate to the configuration file that NetBeans uses to keep track of 
the files in a project
• First problem 
– When moving projects between computers
• Second problem
– When trying to create a copy of a project or copies of subsets of files in projects
These problems are specific to C++ projects – there aren’t similar problems with Java, PHP etc 67
Problems with 
NetBeans C++ projects
• If you have a project with main.cpp, MyRecord.h, and MyRecord.cpp
NetBeans may record this 
– using “relative” file paths (i.e. essentially ./main.cpp, ./MyRecord.h, and 
./MyRecord.cpp)
– Using “absolute” file paths (e.g. 
/home/staff/n/nabg/Teaching/222/Examples/E1/Part1/main.cpp, 
/home/staff/n/nabg/Teaching/222/Examples/E1/Part1/MyRecord.h, 
/home/staff/n/nabg/Teaching/222/Examples/E1/Part1/MyRecord.
• If it used absolute file paths, there is trouble if you try to move the 
project to another computer with a different directory structure (e.g. 
one where the project is /home/neil/Teaching/2014/222/E1a)
– It will keep complaining about “non-existent” files
68
Problems with 
NetBeans C++ projects
• Quite often, you want projects with the same or slightly modified 
versions of some code – e.g. MyRecord.h and MyRecord.cpp are 
used in a set of projects
• It’s tempting to use the “Copy” & “Paste” options in the project view 
of NetBeans
– You think that you are adding a copy of the files to the 2nd project, so that you can 
add features that weren’t needed in the first project
– You aren’t actually making a copy!
– NetBeans simply added absolute references to the files in the other project, and if 
you try changing the files you are changing the original (and only) versions.
• This typically breaks your original project.
• Don’t copy and paste files that way – you will only cause confusion.
69
Problems with 
NetBeans C++ projects
• Both problems are really an issue of the mode used for recording file 
names in Netbeans’ nbproject/configurations.xml file
• Generally, you want it to use relative file paths except when you are 
adding a library to a project, e.g. something like 
/usr/local/mongod/libmongo.a, when it is more convenient if it uses an 
absolute path.
• NetBeans seems to remember whether you used relative or absolute 
last time you added a file, and continues in same mode
– So sometimes you start a project and all file names are recorded relative style, 
sometimes some or all of them are absolute style
70
Problems with 
NetBeans C++ projects
• Moral of story:
If NetBeans seems to be building with the wrong versions of files, or 
tells you there is no file when you can see the file right there
check the configurations.xml file
You can open this in the NetBeans editor and fix it.
71