eclipse total beginner companion document

Upload: muzhochan

Post on 09-Feb-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/22/2019 Eclipse Total Beginner Companion Document

    1/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    Eclipse And Java For Total Beginners

    Companion Tutorial DocumentBy Mark Dexter

    Table of Contents

    Introduction.........................................................................................................................................2Tutorial Target Audience.....................................................................................................................2Tutorial Obectives..............................................................................................................................2!"# learn Java $it" Eclipse%.............................................................................................................&Topics Covered...................................................................................................................................&Tutorial Approac"................................................................................................................................&'etting T"e (ost From T"is Tutorial..................................................................................................&)ample Java Application * +ersonal ,ending ,ibrar#........................................................................-Do$nloading and Installing Eclipse ...................................................................................................-+la#ing t"e ,essons............................................................................................................................

    /e0)i1ing t"e ideo........................................................................................................................

    Closed Captions and ,esson Table of Contents............................................................................,esson Outlines..................................................................................................................................3

    ,esson 4 * Create 5our First Java Class.......................................................................................3,esson 2 * Add (et"ods To Class.................................................................................................3,esson & * 6se Eclipse )crapboo7................................................................................................3,esson - * J6nit Testing in Eclipse8 +art 4....................................................................................3,esson * J6nit Testing Continued...............................................................................................9,esson 3 * 6sing Test0First Development in Eclipse.....................................................................9,esson 9 * Create Boo7 Class.......................................................................................................9,esson : * Add +erson to Boo7 Class...........................................................................................9,esson ; * (#,ibrar# Class and Arra#,ist.....................................................................................9

    ,esson 4< * )tart on (#,ibrar# Class...........................................................................................9,esson 44 * Create first met"ods in (#,ibrar# class.....................................................................:,esson 42 * Create c"ec7Out8 c"ec7In (et"ods..........................................................................:,esson 4& * Continue c"ec7Out (et"od.......................................................................................:,esson 4- * Finis" c"ec7Out (et"od............................................................................................:,esson 4 * Finis" (#,ibrar# (et"ods..........................................................................................:,esson 43 * Create main (et"od and JA/ File.............................................................................;

    Alp"abetical Inde= b# ,esson.............................................................................................................;'lossar# of Terms.............................................................................................................................42

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    2/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    Additional /esources........................................................................................................................4-Eclipse !ebsites..........................................................................................................................4Java !ebsites...............................................................................................................................4Java Boo7s...................................................................................................................................4

    Code )naps"ots...............................................................................................................................4,esson 4 * +erson class ?fields and constructor@........................................................................4,esson 2 * +erson class ?$it" get and set met"ods@...................................................................43,esson & ?scrapboo7 snaps"ots@..................................................................................................49,esson * +ersonTest class........................................................................................................49,esson 3 * Added to)tring?@ (et"od to +erson class..................................................................4:,esson 9 * TestBoo7 and Boo7 Classes......................................................................................2 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    5/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    cop# t"e 1ip file to #our root director# ?e.g.8 CM@ and t"en un1ip t"e do$nloaded 1ip file. T"is $illcreate a folder called CMeclipse. T"e Eclipse programs $ill be created in several subfolders?configuration8 features8 plugins8 readme@. T"e procedure for ,inu= is similar8 e=cept #our un1ip t"e.tar.g1 file.

    *laying the +essonsTo pla# t"e lessons8 follo$ t"ese steps.

    4. Do$nload t"e 43 lesson 1ip files ?totalbeginnerlesson==.1ip@.

    2. 6n1ip eac" lessonKs 1ip file into a director# on #our s#stem.

    &. Find t"e file called lesson==."tml8 $"ere == is t"e lesson number 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    6/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    +esson Outlines

    +esson 0 ) Create 1our &irst ava Class

    Create Java proect in Eclipse

    Create Java pac7age

    Discuss t"e ,ending ,ibrar# Application

    Introduce classes and obects8 naming conventions

    !rite a simple Java class ?+erson@

    +esson 2 ) Add Methods To Class

    Introduce Eclipse ie$s and +erspectives

    Introduce Eclipse user interface * drag drop8 conte=t menus8 "elp

    Add get and set met"ods to +erson class

    +esson 3 ) 4se "cli#se (cra#book

    Introduce Eclipse )crapboo7

    Introduce Java e=pressions8 statements

    Discuss Java pac7ages

    Create +erson obect in )crapboo7

    +esson 5 ) 4nit Testing in "cli#se6 *art 0

    Create test source folder

    Create +ersonTest class

    /un first J6nit test

    +esson 7 ) 4nit Testing Continued

    Test +erson class * part 2

    Create test met"ods for constructor8 getame8 and get(a=imumBoo7s

    )tatic met"ods

    +esson 8 ) 4sing Test-&irst Develo#'ent in "cli#se

    6se test0first approac" to $rite t"e +erson to)tring?@ met"od

    (et"od overriding

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    7/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    Field Hiding

    +esson 9 ) Create Book Class

    Create Boo7Test before creating Boo7 class

    Test Boo7 constructor

    Create get and set met"ods

    +esson : ) Add *erson to Book Class

    Create a relations"ip bet$een t"e Boo7 class and t"e +erson Class

    Test get+erson met"od

    Create J6nit Test )uite

    +esson ; ) My+ibrary Class and Array+ist

    Ho$ can $e "old boo7s8 etc. in a collection%

    (#,ibrar# obect to "old +erson N Entr# obects

    Introduce Arra#,ist in scrapboo7

    Introduce Java 'enerics

    (et"od c"aining

    +esson 0< ) (tart on My+ibrary Class Create (#,ibrar#Test J6nit test

    Create test(#,ibrar# to test (#,ibrar# constructor

    Create (#,ibrar# constructor

    Introduce instanceof operator

    Introduce assertTrue met"od

    +esson 00 ) Create first 'ethods in My+ibrary class

    Create test met"od for addBoo78 removeBoo7 met"ods Create addBoo78 removeBoo7 met"ods and test

    Create add+erson8 remove+erson met"ods

    Introduce Eclipse refactoring * move local variable to field

    +esson 02 ) Create checkOut6 checkIn Methods

    Create test for c"ec7Out8 c"ec7In met"ods

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    8/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    !rite c"ec7out met"od

    Introduce if t"en else s#nta=

    Introduce boolean met"od

    !rite c"ec7In met"od

    +esson 03 ) Continue checkOut Method

    Test c"ec7Out8 c"ec7In met"ods

    Fi= compiler error * misplaced P

    Add test for ma=imum boo7s

    Create test for getBoo7sFor+erson?@ met"od

    /efactoring * e=tract met"od

    +esson 05 ) &inish checkOut Method

    !rite getBoo7sFor+erson met"od

    Introduce for eac" loop

    Introduce logical and operator NN

    ull+ointerE=ception errors

    Complete c"ec7Out (et"od

    +esson 07 ) &inish My+ibrary Methods Create test for getAvailableBoo7s

    Create getAvailableBoo7s

    Create get6navailableBoo7s

    +esson 08 ) Create 'ain Method and A, &ile

    Introduce main met"od

    !rite a main met"od

    /un (#,ibrar# as Java application

    E=port to JA/ file and run from !indo$s

    Al#habetical Index by +esson

    Topic ,essonaddBoo7?@ met"od.....................................................................................................................00

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    9/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    add+erson?@ met"od..................................................................................................................00agile soft$are development........................................................................................................5

    Arra#,istQR class.........................................................................................................................;assertEuals?@ met"od ...............................................................................................................7

    assertTrue?@ met"od..................................................................................................................0 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    10/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    J6nit test class8 create ne$........................................................................................................5J6nit test librar#..........................................................................................................................5J6nit test8 running.......................................................................................................................7J6nit ie$...................................................................................................................................7

    J6nit8 compare actual $it" e=pected..........................................................................................7lending librar# application............................................................................................................0main?@ met"od...........................................................................................................................08met"ods ?class@...........................................................................................................................2(#,ibrar# class.........................................................................................................................0 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    11/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    testBoo7?@ met"od ......................................................................................................................9testC"ec7Out?@ met"od ............................................................................................................02test'etAvailableBoo7s?@ met"od..............................................................................................07test'etBoo7sFor+erson?@ met"od ...........................................................................................03

    test'et6navailableBoo7s?@ met"od..........................................................................................07t"is 7e#$ord............................................................................................................................26 8TODO comment8 tas7 list............................................................................................................9to)tring?@ met"od .......................................................................................................................8unit test........................................................................................................................................5vie$ ?Eclipse@..............................................................................................................................2$elcome screen ?Eclipse@...........................................................................................................0$or7benc" ?Eclipse@....................................................................................................................2$or7benc" regions......................................................................................................................2$or7space ?Eclipse@....................................................................................................................0V semicolon8 in Java.....................................................................................................................2

    operator ?not eual to@..........................................................................................................02? @ parent"eses8 in Java...............................................................................................................2 P curl# braces8 in Java...............................................................................................................2W W comments.............................................................................................................................2NN operator ?and operator@.......................................................................................................05 operator ?euals@..................................................................................................................02

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    12/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    %lossary of Ter's

    Ter' =uick Definition

    Access (odifier /eserved $ords public8 private8 protected in Java. Control $"et"erclasses and members ma# be accessed from an# class8 onl# t"is class8subclasses. Default is access from an# class in t"e pac7age.

    Agile ?or E=treme@Development

    (et"odolog# for developing soft$are t"at emp"asi1es8 among ot"ert"ings8 unit testing as part of development process.

    A+I ?Application+rogramming Interface@

    T"e $a# one program uses anot"er program. In Java8 t"e A+I can bet"oug"t of as t"e collection of public met"ods for a class or pac7age.

    Class (ain building bloc7 in Java. Contains members8 including fields andmet"ods. Classes are t"e blueprint for creating obects.

    Constructor )pecial bloc7 of code used to create an instance of a class ?or8 if #ouprefer8 an obect $"ose t#pe is t"e class@. 6sed $it" t"e ne$ 7e#$ord?e.g.8 +erson p ne$ +erson?@ calls t"e +erson?@ constructor@.

    Field (ember in a class t"at "olds data ?e.g.8 name8 age8 etc.@. 6suall# mar7edprivate so t"at ot"er programs cannot directl# access.

    IDE ?IntegratedDevelopmentEnvironment@

    +rogram8 li7e Eclipse8 t"at provides t"e different tools reuired to developa soft$are pac7age.

    J( ?Java irtual(ac"ine@ ?also 7no$n asJava /untime Engine orJ/E@

    T"e program t"at runs Java programs on a specific platform. Java sourcecode is compiled into .class files. T"ese contain t"e instructions used b#t"e J( to actuall# run t"e programs on a !indo$s +C8 a ,inu= computer8a (ac computer8 etc. T"e J( is $ritten for eac" platform supported b#Java.

    J6nit Test A Java class used to test individual met"ods in a class. 6sed to build testcases8 e.g.8 $"en using agile development met"odolog#.

    (et"od (ember in a class t"at does some processing ?e.g.8 li7e a subroutine orfunction in ot"er languages@.

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    13/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    Ter' =uick Definition

    (et"od Argument8 (et"od+arameter

    +arameters refers to t"e list of variables in a met"od declaration.Arguments are t"e actual values t"at are passed in $"en t"e met"od is

    invo7ed. !"en #ou invo7e a met"od8 t"e arguments used must matc" t"edeclarationKs parameters in t#pe and order. For e=ample8 in t"e met"od

    public setame?)tring name@ XP

    name is t"e parameter for t"is met"od. If t"is met"od is used as follo$s

    m#Obect.setame?Fred@V

    Fred is t"e argument of t"e met"od and it must matc" t"e t#pe of t"emet"odGs parameter.

    (et"od )ignature A met"odGs name plus itGs parameter list. For e=ample8 a met"od definedas setame ?)tring name@ "as a met"od signature of setame?)tring@.(et"od signatures are important because t"e# allo$ met"ods to beoverloaded ?i.e.8 "ave t"e same name but different signatures@. Fore=ample8 t"e met"od setame?)tring firstame8 )tring lastame@ couldbe an overload of setame?)tring name@ because it as a differentsignature ?setame?)tring8 )tring@@.

    Obect An instance of a class. For e=ample8 Coo7ie could be a class8 and acoo7ie ?e.g. t"isCoo7ie@ $ould be an obect created using t"e class. In

    ot"er $ords8 t"isCoo7ie is an obect of t#pe Coo7ie or an instance ofCoo7ie.

    Overload ?(et"od@ To provide multiple met"ods $it" t"e same name but different parameters?i.e.8 same name but different signatures@.

    Override ?(et"od@ !"en a subclass implements a met"od in"erited from t"e super class8 t"ismet"od is said to be overridden.

    +ac7age +ac7ages are imported into a source file to save t#ping t"e full name oft"e class ?e.g.8 can sa# +erson instead of

    org.eclipsetraining.librar#tutorial.+erson and to avoid t"e possibilit# oft$o classes "aving identical names.

    +roect In Eclipse8 a $a# to organi1e #our $or7. An Eclipse $or7space cancontain multiple proects. Eac" proect can contain multiple pac7ages.Eac" pac7age can contain multiple classes.

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    14/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    Ter' =uick Definition

    /efactor To improve a program $it"out c"anging t"e $a# it $or7s ?i.e.8 its [email protected]=ample include renaming fields or variables8 streamlining code8 etc. er#

    important in agile development because of emp"asis on self0documentingcode.

    /eference ariable In Java8 variable t"at "olds an obect reference ?e.g.8 p ne$ +erson?@V@.+oints to an area on t"e "eap $"ere t"e obect resides. Contrast $it"value variable.

    )crapboo7 +age Area in Eclipse $"ere #ou can e=ecute Java code snippets and see "o$t"e# $or7. 'reat for e=perimenting $it" Java statements.

    )tatic (et"od A met"od t"at belongs to t"e entire class instead of one instance of t"e

    class. Invo7ed $it" QClassR.Q(et"odR ?e.g.8 +erson.getTotalCount?@@.6sed for met"ods t"at donGt rel# on an# one instance of a class.

    )$ing A set of standard Java pac7ages t"at implement a grap"ical user interface$it"out using an# native code.

    )!T ?)tandard !idgetTool7it@

    )et of Java classes and native programs developed b# Eclipse to allo$Java programs to "ave t"e loo7 and feel of native programs on eac"platform. 6sed to create t"e Eclipse IDE.

    T#pe In Java8 an attribute of a variable to indicate eit"er a primitive t#pe ?int8

    boolean8 etc.@ or class members"ip. For obects8 t"e t#pe is t"e class to$"ic" it belongs. T#pes also include interfaces and enumerations.

    alue ariable In Java8 variable t"at "olds t"e value of a Java primitive ?e.g.8 integer8c"aracter8 etc.@. Held in t"e memor# stac7. Contrast $it" referencevariable.

    !or7space Top0level container for Eclipse $or7. Holds multiple proects. In a singleEclipse session8 onl# one $or7space can be active.

    Additional ,esourcesT"ere are man# resources available for learning more about Eclipse and Java. T"ese are ust a fe$t"at IGve found "elpful.

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    15/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    "cli#se Websites

    $$$.eclipse.orgresources. T"is lists a number of articles8 boo7s8 presentations8demonstrations and ot"er resources on a variet# of topics related to Eclipse.

    eclipse.ne$comer ne$sgroup. T"is is a friendl#8 active ne$sgroup $"ere ne$comers toEclipse can as7 uestions. T"e searc" feature of t"is and ot"er ne$sgroups can be especiall#valuable8 since t"ere is a good c"ance t"at #our uestion "as alread# been as7ed andans$ered.

    Beginning Eclipse Tutorial on ArcTec" )oft$are ,,C $ebsite. !ritten tutorial to get #ou started$it" Eclipse and Java. ,ogin reuired to do$nload. It "as a ver# good section on do$nloadingand installing t"e Java JD. ,in7 to tutorial is"ttps$$$.arctec"soft$are.comtutorialtutorial.do%subcatId4 . ,in7 to "ome page is"ttps$$$.arctec"soft$are.comtutorial$elcome+age.do .

    ava Websites T"e Java Tutorials from )un ?"ttpava.sun.comdocsboo7stutorialavainde=."tml @. T"e gold

    standard for learning Java8 and itGs free.

    Java/anc" Big (oose )aloon $eb site ?"ttpsaloon.avaranc".comcgi0binubbultimatebb.cgi%categor#4@. T"is "as a variet# of forums8 including Java in 'eneral?beginner@8 Java in 'eneral ?intermediate@8 and man# ot"er Java topics. er# active andfriendl#8 $it" 7no$ledgeable moderators.

    T"e Java Developers Almanac 4.- ?"ttp$$$.e=ampledepot.com@. Contains Java codesamples for man# topics.

    ava Books

    Head First Java8 b# at"# )ierra N Bert Bates. E=cellent8 fun8 creative boo7 for Java and OO+beginners.

    T"in7ing In Java8 b# Bruce Ec7el. E=cellent8 t"oroug" reference for Java. For all levels ofprogrammer.

    Effective Java8 b# Jos"ua Bloc". Concise boo7 documents specific recommendations for Javabest practices. For intermediate to advanced programmers.

    Code (na#shotsT"e follo$ing pages contain code snaps"ots as of t"e end of eac" lesson. T"ese can be used tocompare #our code to or to "elp #ou fi= an# problems #ou mig"t "ave. If needed8 #ou can cop# andpast t"is code into #our Eclipse Java source files. Also8 if #ou $ant to start t"e tutorial in t"e middle8t"ese can "elp #ou catc" up to t"e correct point.

    +esson 0 ) *erson class >fields and constructor?

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    16/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    packageorg.totalbeginner.tutorial;

    publicclassPerson { // fields privateString name; // name of the person privateintmaximumBooks; // most books the person can check out

    // constructors

    publicPerson() { name= unkno!n name; maximumBooks= "; ##

    +esson 2 ) *erson class >!ith get and set 'ethods?

    ote Hig"lig"ted code added in t"is lesson.

    packageorg.totalbeginner.tutorial;

    publicclassPerson {

    // fields privateString name; // name of the person privateintmaximumBooks; // most books the person can check out

    // constructors

    publicPerson() { name= unkno!n name; maximumBooks= "; #

    //methods

    publicString get$ame() { returnname; #

    publicvoidset$ame(String an%$ame) { name= an%$ame; #

    publicintget&aximumBooks() {

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    17/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    returnmaximumBooks; #

    public

    void

    set&aximumBooks(int

    maximumBooks) {

    this.maximumBooks= maximumBooks; ##

    +esson 3 >scra#book sna#shots?

    'xpressions

    *

    int a = +;

    a = a , -;

    a

    (after import of org.totalbeginner.tutorial.,)

    Person p = ne! Person();

    p.set$ame(0red1);P

    +esson 7 ) *ersonTest class

    packageorg.totalbeginner.tutorial;import2unit.frame!ork.3est4ase;publicclassPerson3est extends3est4ase {

    publicvoidtestPerson() { Person p- = newPerson(); assertEquals(unkno!n name5 p-.get$ame());

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    18/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    assertEquals("5 p-.get&aximumBooks());

    #

    public

    void

    testSet$ame() {

    Person p = newPerson(); p.set$ame(0red); assertEquals(0red5 p.get$ame()); #

    publicvoidtestSet&aximumBooks() { Person p" = newPerson(); p".set&aximumBooks(-); assertEquals(-5 p".get&aximumBooks()); #

    #

    +esson 8 ) Added to(tring>? Method to *erson class

    ote +erson and +ersonTest classes are complete at t"is point.

    packageorg.totalbeginner.tutorial;

    publicclassPerson { // fields privateString name; // name of the person privateintmaximumBooks; // most books the person can check out

    // constructors

    publicPerson() { name= unkno!n name; maximumBooks= "; #

    //methods

    publicString get$ame() { returnname; #

    publicvoidset$ame(String an%$ame) { name= an%$ame; #

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    19/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    publicintget&aximumBooks() { returnmaximumBooks;

    #

    publicvoidset&aximumBooks(intmaximumBooks) {

    this.maximumBooks= maximumBooks; #

    publicString toString() { returnthis.get$ame() * (* this.get&aximumBooks() * books); #

    #

    packageorg.totalbeginner.tutorial;import2unit.frame!ork.3est4ase;publicclassPerson3est extends3est4ase {

    publicvoidtestPerson() { Person p- = newPerson(); assertEquals(unkno!n name5 p-.get$ame()); assertEquals("5 p-.get&aximumBooks()); #

    publicvoidtestSet$ame() { Person p = newPerson(); p.set$ame(0red); assertEquals(0red5 p.get$ame()); #

    publicvoidtestSet&aximumBooks() { Person p" = newPerson(); p".set&aximumBooks(-); assertEquals(-5 p".get&aximumBooks()); #

    publicvoidtest3oString() { Person p6 = newPerson(); p6.set$ame(0red 0lintstone);

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    20/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    p6.set&aximumBooks(7);

    String testString = 0red 0lintstone (7 books); assertEquals(testString5 p6.toString());

    #

    #

    +esson 9 ) TestBook and Book Classes

    packageorg.totalbeginner.tutorial;import2unit.frame!ork.3est4ase;publicclassBook3est extends3est4ase {

    publicvoidtestBook() { Book b- = newBook(8reat 'xpectations); assertEquals(8reat 'xpectations5 b-.title); assertEquals(unkno!n author5 b-.author); ##

    packageorg.totalbeginner.tutorial;publicclassBook {

    String title;

    String author;

    publicBook(String string) {

    this.title= string; this.author= unkno!n author; #

    publicString get9uthor() { returnauthor; #

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    21/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    publicvoidset9uthor(String author) { this.author= author; #

    publicString get3itle() { returntitle; ##

    packageorg.totalbeginner.tutorial;

    import2unit.frame!ork.3est;import2unit.frame!ork.3estSuite;publicclass9ll3ests {

    publicstatic3est suite() { 3estSuite suite = new3estSuite(3est for org.totalbeginner.tutorial); //: 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    22/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    publicBook(String string) { this.title= string;

    this.author

    = unkno!n author;

    #

    publicString get9uthor() { returnauthor; #

    publicvoidset9uthor(String author) { this.author= author; #

    public

    String get3itle() {

    returntitle; #

    publicvoidsetPerson(Person p) { this.person= p; #

    publicPerson getPerson() { returnthis.person; #

    #

    packageorg.totalbeginner.tutorial;import2unit.frame!ork.3est4ase;publicclassBook3est extends3est4ase {

    publicvoidtestBook() {

    Book b- = newBook(8reat 'xpectations); assertEquals(8reat 'xpectations5 b-.title); assertEquals(unkno!n author5 b-.author); #

    publicvoidtest8etPerson() {

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    23/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    Book b = newBook(@ar 9nd Peace); Person p = newPerson(); p.set$ame('lAis);

    // method to sa% book is loaned to this person b.setPerson(p);

    // get the name of the person !ho has the book

    // Person testPerson = b.getPerson();

    // String test$ame = testPerson.get$ame();

    String test$ame = b.getPerson().get$ame();

    assertEquals('lAis5 test$ame);

    #

    #

    +esson ; ) My+ibrary Class and Array+ist

    )crapboo7 E=ample

    9rra%istCBookD list = new9rra%istCBookD();

    Book b- = newBook(8reat 'xpectations);Book b = newBook(@ar and Peace);list.add(b-);

    list.add(b);

    Person p- = newPerson();p-.set$ame(0red);

    b-.setPerson(p-);

    list.remoAe(b-);

    list

    +esson 0< ) (tart on My+ibrary Class

    ote From t"is point on8 if needed use t"e +erson and +ersonTest classes from ,esson 3 and t"eBoo7 and Boo7Test classes from ,esson :.

    packageorg.totalbeginner.tutorial;

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    24/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    import2aAa.util.9rra%ist;import2unit.frame!ork.3est4ase;

    publicclass&%ibrar%3est extends3est4ase {

    // test constructor

    publicvoidtest&%ibrar%() { &%ibrar% ml = new&%ibrar%(3est);

    assertEquals(3est5 ml.name);

    assertTrue(ml.booksinstanceof9rra%ist);

    assertTrue(ml.peopleinstanceof9rra%ist);

    ##

    packageorg.totalbeginner.tutorial;import2aAa.util.9rra%ist;publicclass&%ibrar% {

    String name;

    9rra%istCBookD books; 9rra%istCPersonD people;

    public&%ibrar%(String name) { this.name= name; books= new9rra%istCBookD(); people= new9rra%istCPersonD(); ##

    +esson 00 ) Create first 'ethods in My+ibrary class

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    25/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    packageorg.totalbeginner.tutorial;import2aAa.util.9rra%ist;

    import2unit.frame!ork.3est4ase;publicclass&%ibrar%3est extends3est4ase {

    privateBook b-; privateBook b; privatePerson p-; privatePerson p; private&%ibrar% ml;

    // test constructor

    publicvoidtest&%ibrar%() { &%ibrar% ml = new&%ibrar%(3est);

    assertEquals(3est5 ml.name);

    assertTrue(ml.booksinstanceof9rra%ist);

    assertTrue(ml.peopleinstanceof9rra%ist);

    #

    public

    void

    setup() {

    b-= newBook(Book-); b= newBook(Book); p-= newPerson(); p= newPerson(); p-.set$ame(0red); p.set$ame(Sue);

    ml= new&%ibrar%(3est);

    #

    publicvoidtest9ddBook() { //create test ob2ects setup();

    //test initial siEe is

    assertEquals(5 ml.getBooks().siEe());

    ml.addBook(b-);

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    26/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    ml.addBook(b);

    assertEquals(5 ml.getBooks().siEe());

    assertEquals(5 ml.getBooks().indexFf(b-));

    assertEquals(-5 ml.getBooks().indexFf(b));

    ml.remoAeBook(b-);

    assertEquals(-5 ml.getBooks().siEe()); assertEquals(5 ml.getBooks().indexFf(b));

    ml.remoAeBook(b);

    assertEquals(5 ml.getBooks().siEe()); ##

    packageorg.totalbeginner.tutorial;import2aAa.util.9rra%ist;publicclass&%ibrar% {

    String name;

    9rra%istCBookD books; 9rra%istCPersonD people;

    public&%ibrar%(String name) { this.name= name; books= new9rra%istCBookD(); people= new9rra%istCPersonD(); #

    publicString get$ame() { returnname; #

    public9rra%istCBookD getBooks() { returnbooks; #

    public9rra%istCPersonD getPeople() { returnpeople; #

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    27/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    publicvoidaddBook(Book b-) { this.books.add(b-);

    #

    publicvoidremoAeBook(Book b-) {

    this.books.remoAe(b-);

    #

    publicvoidaddPerson(Person p-) {

    this.people.add(p-); #

    public

    void

    remoAePerson(Person p-) {

    this.people.remoAe(p-); ##

    +esson 02 ) Create checkOut6 checkIn Methods

    packageorg.totalbeginner.tutorial;import2aAa.util.9rra%ist;import2unit.frame!ork.3est4ase;publicclass&%ibrar%3est extends3est4ase {

    privateBook b-; privateBook b; privatePerson p-;

    privatePerson p; private&%ibrar% ml;

    // test constructor

    publicvoidtest&%ibrar%() { &%ibrar% ml = new&%ibrar%(3est);

    assertEquals(3est5 ml.name);

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    28/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    assertTrue(ml.booksinstanceof9rra%ist); assertTrue(ml.peopleinstanceof9rra%ist);

    #

    publicvoidsetup() { b-= newBook(Book-); b= newBook(Book); p-= newPerson(); p= newPerson(); p-.set$ame(0red); p.set$ame(Sue);

    ml

    = new

    &%ibrar%(3est);

    #

    publicvoidtest9ddBook() {

    //create test ob2ects setup();

    //test initial siEe is

    assertEquals(5 ml.getBooks().siEe());

    ml.addBook(b-);

    ml.addBook(b);

    assertEquals(5 ml.getBooks().siEe());

    assertEquals(5 ml.getBooks().indexFf(b-)); assertEquals(-5 ml.getBooks().indexFf(b));

    ml.remoAeBook(b-);

    assertEquals(-5 ml.getBooks().siEe()); assertEquals(5 ml.getBooks().indexFf(b));

    ml.remoAeBook(b);

    assertEquals(5 ml.getBooks().siEe());

    #

    privatevoidadd>tems() {

    ml.addBook(b-); ml.addBook(b); ml.addPerson(p-);

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    29/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    ml.addPerson(p);

    #

    publicvoidtest4heckFut() { // set up ob2ects setup(); add>tems();

    assertTrue(Book did not check out correctl%5

    ml.checkFut(b-5p-));

    assertEquals(0red5 b-.getPerson().get$ame());

    assertFalse(Book !as alread% checked out5

    ml.checkFut(b-5p));

    assertTrue(Book check in failed5 ml.check>n(b-));

    assertFalse(Book !as alread% checked in5 ml.check>n(b-));

    assertFalse(Book !as neAer checked out5 ml.check>n(b));

    #

    #

    packageorg.totalbeginner.tutorial;import2aAa.util.9rra%ist;publicclass&%ibrar% {

    String name;

    9rra%istCBookD books; 9rra%istCPersonD people;

    public&%ibrar%(String name) { this.name= name; books= new9rra%istCBookD();

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    30/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    people= new9rra%istCPersonD(); #

    public

    String get$ame() {

    returnname; #

    public9rra%istCBookD getBooks() { returnbooks; #

    public9rra%istCPersonD getPeople() { returnpeople; #

    publicvoidaddBook(Book b-) { this.books.add(b-);

    #

    publicvoidremoAeBook(Book b-) {

    this.books.remoAe(b-);

    #

    public

    void

    addPerson(Person p-) {

    this.people.add(p-); #

    publicvoidremoAePerson(Person p-) { this.people.remoAe(p-); #

    publicbooleancheckFut(Book b-5 Person p-) { if((b-.getPerson() == null) ){ b-.setPerson(p-);

    return

    true;

    # else{ returnfalse; # #

    publicbooleancheck>n(Book b-) { if(b-.getPerson() G= null) {

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    31/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    b-.setPerson(null);

    returntrue; #

    else

    {

    returnfalse; # #

    #

    +esson 03 ) Continue checkOut Method

    packageorg.totalbeginner.tutorial;import2aAa.util.9rra%ist;publicclass&%ibrar% {

    String name;

    9rra%istCBookD books; 9rra%istCPersonD people;

    public&%ibrar%(String name) { this.name= name; books= new9rra%istCBookD(); people= new9rra%istCPersonD(); #

    publicString get$ame() { returnname;

    #public9rra%istCBookD getBooks() {

    returnbooks; #

    public9rra%istCPersonD getPeople() { returnpeople; #

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    32/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    publicvoidaddBook(Book b-) { this.books.add(b-);

    #

    publicvoidremoAeBook(Book b-) { this.books.remoAe(b-);

    #

    publicvoidaddPerson(Person p-) {

    this.people.add(p-); #

    publicvoidremoAePerson(Person p-) { this.people.remoAe(p-); #

    publicbooleancheckFut(Book b-5 Person p-) { if((b-.getPerson() == null) ){ b-.setPerson(p-); returntrue; # else{

    return

    false;

    # #

    publicbooleancheck>n(Book b-) { // TODO9utogenerated method stub returnfalse; #

    public9rra%istCBookD getBooks0orPerson(Person p-) { // TODO9utogenerated method stub

    return

    null;

    #

    #

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    33/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    +esson 05 ) &inish checkOut Method

    package

    org.totalbeginner.tutorial;

    import2aAa.util.9rra%ist;publicclass&%ibrar% {

    String name;

    9rra%istCBookD books; 9rra%istCPersonD people;

    public&%ibrar%(String name) {

    this.name

    = name;

    books= new9rra%istCBookD(); people= new9rra%istCPersonD(); #

    publicString get$ame() { returnname; #

    public9rra%istCBookD getBooks() { returnbooks;

    #

    public9rra%istCPersonD getPeople() {

    returnpeople; #

    publicvoidaddBook(Book b-) { this.books.add(b-);

    #

    public

    void

    remoAeBook(Book b-) {

    this.books.remoAe(b-);

    #

    publicvoidaddPerson(Person p-) {

    this.people.add(p-); #

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    34/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    publicvoidremoAePerson(Person p-) { this.people.remoAe(p-); #

    publicbooleancheckFut(Book b-5 Person p-) { intbooksFut = this.getBooks0orPerson(p-).siEe(); if((b-.getPerson() == null) HH booksFut C p-.get&aximumBooks()){ b-.setPerson(p-); returntrue; # else{ returnfalse; #

    #

    publicbooleancheck>n(Book b-) {

    if(b-.getPerson() G= null) { b-.setPerson(null); returntrue; # else{ returnfalse; # #

    public9rra%istCBookD getBooks0orPerson(Person p-) { 9rra%istCBookD result = new9rra%istCBookD(); for(Book aBook this.getBooks()) { if((aBook.getPerson() G= null) HH

    (aBook.getPerson().get$ame()

    .eIuals(p-.get$ame())))

    {

    result.add(aBook); #

    # returnresult; ##

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    35/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    +esson 07 ) &inish My+ibrary Methods

    package

    org.totalbeginner.tutorial;

    import2aAa.util.9rra%ist;import2unit.frame!ork.3est4ase;publicclass&%ibrar%3est extends3est4ase {

    privateBook b-; privateBook b; privatePerson p-;

    private

    Person p;

    private&%ibrar% ml;

    // test constructor

    publicvoidtest&%ibrar%() { &%ibrar% ml = new&%ibrar%(3est);

    assertEquals(3est5 ml.name);

    assertTrue(ml.booksinstanceof9rra%ist);

    assertTrue(ml.peopleinstanceof9rra%ist);

    #

    publicvoidsetup() { b-= newBook(Book-); b= newBook(Book); p-= newPerson(); p= newPerson(); p-.set$ame(0red); p.set$ame(Sue);

    ml

    = new

    &%ibrar%(3est);

    #

    publicvoidtest9ddBook() {

    //create test ob2ects setup();

    //test initial siEe is

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    36/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    assertEquals(5 ml.getBooks().siEe());

    ml.addBook(b-);

    ml.addBook(b);

    assertEquals(5 ml.getBooks().siEe());

    assertEquals(5 ml.getBooks().indexFf(b-)); assertEquals(-5 ml.getBooks().indexFf(b));

    ml.remoAeBook(b-);

    assertEquals(-5 ml.getBooks().siEe()); assertEquals(5 ml.getBooks().indexFf(b));

    ml.remoAeBook(b);

    assertEquals(5 ml.getBooks().siEe());

    #

    publicvoidtest4heckFut() { // set up ob2ects setup();

    add>tems();

    assertTrue(Book did not check out correctl%5ml.checkFut(b-5p-));

    assertEquals(0red5 b-.getPerson().get$ame());

    assertFalse(Book !as alread% checked out5

    ml.checkFut(b-5p));

    assertTrue(Book check in failed5 ml.check>n(b-));

    assertFalse(Book !as alread% checked in5 ml.check>n(b-));

    assertFalse(Book !as neAer checked out5 ml.check>n(b));

    // additional test for maximumBooks

    setup(); p-.set&aximumBooks(-); add>tems();

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    37/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    assertTrue(0irst book did not check out5

    ml.checkFut(b5 p-));

    assertFalse(Second book should not haAe checked out5

    ml.checkFut(b-5 p-));

    #

    privatevoidadd>tems() {

    ml.addBook(b-); ml.addBook(b); ml.addPerson(p-); ml.addPerson(p); #

    publicvoidtest8etBooks0orPerson() { setup(); add>tems(); assertEquals(5 ml.getBooks0orPerson(p-).siEe());

    ml.checkFut(b-5 p-);

    9rra%istCBookD testBooks = ml.getBooks0orPerson(p-);

    assertEquals(-5 testBooks.siEe()); assertEquals(5 testBooks.indexFf(b-));

    ml.checkFut(b5 p-); testBooks = ml.getBooks0orPerson(p-); assertEquals(5 testBooks.siEe()); assertEquals(-5 testBooks.indexFf(b));

    #

    publicvoidtest8et9AailableBooks() {

    setup(); add>tems();

    9rra%istCBookD testBooks = ml.get9AailableBooks();

    assertEquals(5 testBooks.siEe()); assertEquals(-5 testBooks.indexFf(b));

    ml.checkFut(b-5 p-);

    testBooks = ml.get9AailableBooks(); assertEquals(-5 testBooks.siEe()); assertEquals(5 testBooks.indexFf(b));

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    38/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    ml.checkFut(b5 p-);

    testBooks = ml.get9AailableBooks(); assertEquals(5 testBooks.siEe());

    #

    publicvoidtest8ettems(); assertEquals(5 ml.get

  • 7/22/2019 Eclipse Total Beginner Companion Document

    39/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    9rra%istCBookD books;

    9rra%istCPersonD people;

    public

    &%ibrar%(String name) {

    this.name= name; books= new9rra%istCBookD(); people= new9rra%istCPersonD(); #

    publicString get$ame() { returnname; #

    public9rra%istCBookD getBooks() {

    return

    books;

    #

    public9rra%istCPersonD getPeople() { returnpeople; #

    publicvoidaddBook(Book b-) { this.books.add(b-);

    #

    publicvoidremoAeBook(Book b-) { this.books.remoAe(b-);

    #

    publicvoidaddPerson(Person p-) {

    this.people.add(p-); #

    publicvoidremoAePerson(Person p-) {

    this.people.remoAe(p-);

    #

    publicbooleancheckFut(Book b-5 Person p-) { intbooksFut = this.getBooks0orPerson(p-).siEe(); if((b-.getPerson() == null) HH booksFut C p-.get&aximumBooks()){ b-.setPerson(p-); returntrue;

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    40/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    #

    else{ returnfalse;

    #

    #

    publicbooleancheck>n(Book b-) { if(b-.getPerson() G= null) { b-.setPerson(null); returntrue; # else{ returnfalse; #

    #

    public9rra%istCBookD getBooks0orPerson(Person p-) {

    9rra%istCBookD result = new9rra%istCBookD(); for(Book aBook this.getBooks()) { if((aBook.getPerson() G= null) HH

    (aBook.getPerson().get$ame()

    .eIuals(p-.get$ame())))

    {

    result.add(aBook);

    # # returnresult; #

    public9rra%istCBookD get9AailableBooks() { 9rra%istCBookD result = new9rra%istCBookD(); for(Book aBook this.getBooks()) { if(aBook.getPerson() == null) { result.add(aBook); #

    #

    returnresult; #

    public9rra%istCBookD get

  • 7/22/2019 Eclipse Total Beginner Companion Document

    41/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    result.add(aBook);

    #

    #

    returnresult; #

    publicString toString() { returnthis.get$ame() * *

    this.getBooks().siEe() * books; * this.getPeople().siEe() * people.; ##

    +esson 08 ) Create 'ain Method and A, &ile

    packageorg.totalbeginner.tutorial;import2aAa.util.9rra%ist;publicclass&%ibrar% {

    String name;

    9rra%istCBookD books; 9rra%istCPersonD people;

    public&%ibrar%(String name) { this.name= name; books= new9rra%istCBookD(); people= new9rra%istCPersonD(); #

    publicString get$ame() { returnname; #

    public9rra%istCBookD getBooks() { returnbooks; #

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    42/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    public9rra%istCPersonD getPeople() { returnpeople; #

    publicvoidaddBook(Book b-) { this.books.add(b-);

    #

    publicvoidremoAeBook(Book b-) {

    this.books.remoAe(b-);

    #

    public

    void

    addPerson(Person p-) {

    this.people.add(p-); #

    publicvoidremoAePerson(Person p-) { this.people.remoAe(p-); #

    publicbooleancheckFut(Book b-5 Person p-) { intbooksFut = this.getBooks0orPerson(p-).siEe(); if((b-.getPerson() == null) HH

    booksFut C p-.get&aximumBooks()){

    b-.setPerson(p-); returntrue; # else{ returnfalse; # #

    publicbooleancheck>n(Book b-) { if(b-.getPerson() G= null) {

    b-.setPerson(null); returntrue; # else{ returnfalse; # #

    public9rra%istCBookD getBooks0orPerson(Person p-) {

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    43/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    9rra%istCBookD result = new9rra%istCBookD(); for(Book aBook this.getBooks()) { if((aBook.getPerson() G= null) HH

    (aBook.getPerson().get$ame()

    .eIuals(p-.get$ame())))

    {

    result.add(aBook); # # returnresult; #

    public9rra%istCBookD get9AailableBooks() {

    9rra%istCBookD result = new

    9rra%istCBookD();

    for(Book aBook this.getBooks()) { if(aBook.getPerson() == null) { result.add(aBook); #

    #

    returnresult; #

    public9rra%istCBookD get

  • 7/22/2019 Eclipse Total Beginner Companion Document

    44/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    Book b = newBook(8reat 'xpectations); b-.set9uthor(3olsto%); b.set9uthor(?ickens);

    Person 2im = new

    Person();

    Person sue = newPerson(); 2im.set$ame(im); sue.set$ame(Sue);

    testibrar%.addBook(b-);

    testibrar%.addBook(b); testibrar%.addPerson(2im); testibrar%.addPerson(sue);

    S%stem.out.println(ust created ne! librar%);

    testibrar%.printStatus();

    S%stem.out.println(4heck out @ar 9nd Peace to Sue);

    testibrar%.checkFut(b-5 sue); testibrar%.printStatus();

    S%stem.out.println(?o some more stuff);

    testibrar%.check>n(b-); testibrar%.checkFut(b5 2im); testibrar%.printStatus();

    #

    privatevoidprintStatus() {

    S%stem.out .println(Status Leport of &%ibrar% Mn*

    this.toString());

    for(Book thisBook this.getBooks()) { S%stem.out.println(thisBook); #

    for(Person p this.getPeople()) { intcount = this.getBooks0orPerson(p).siEe(); S%stem.out.println(p * (has * count

    * of m% books));

    # S%stem.out.println(Books 9Aailable * this.get9AailableBooks().siEe()); S%stem.out.println( 'nd of Status Leport );

    Cop#rig"t > 2

  • 7/22/2019 Eclipse Total Beginner Companion Document

    45/45

    Eclipse and Java for Total BeginnersTutorial Companion Document

    #

    #