namesnamesclass •stores a list of unique names in alphabetical order. •allows look-up, insert,...

13
Names example Example: Names class practice with coding array algorithms implementing classes and using good development techniques incremental development for lookup, remove, insert: design test cases first implement code code refactoring test code Names ex [Bono] 1

Upload: others

Post on 21-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NamesNamesclass •Stores a list of unique names in alphabetical order. •Allows look-up, insert, and removal of names in the list. •Uses partially-filled array representation •Names.java

Names example • Example: Names class• practice with– coding array algorithms– implementing classes– and using good development techniques

• incremental development• for lookup, remove, insert:– design test cases first– implement code• code refactoring

– test code

Names ex [Bono] 1

Page 2: NamesNamesclass •Stores a list of unique names in alphabetical order. •Allows look-up, insert, and removal of names in the list. •Uses partially-filled array representation •Names.java

Announcements

• MT 1: Tues. Sept. 22 9:30 – 10:50am Pacific (alternate time for students in Asia: 6:30pm Pacific)

• IMPORTANT: join CS 455 gradescope course soon:– on d2l, choose "CS 455 Tools" menu (top of page)– choose "gradescope"– it will send you email to complete registration

• Sample exams will be published in a few days(link on web page)

Names ex [Bono] 2

Page 3: NamesNamesclass •Stores a list of unique names in alphabetical order. •Allows look-up, insert, and removal of names in the list. •Uses partially-filled array representation •Names.java

Names class

• Stores a list of unique names in alphabetical order.

• Allows look-up, insert, and removal of names in the list.

• Uses partially-filled array representation

• Names.java has a partial implementation• MinNamesTester.java is a program to test that

subset.

Names ex [Bono] 3

Page 4: NamesNamesclass •Stores a list of unique names in alphabetical order. •Allows look-up, insert, and removal of names in the list. •Uses partially-filled array representation •Names.java

Names representation

Names ex [Bono] 4

Names

Don Sam Sue Zhou

0 1 2 3 4 5 6 7namesArr

numNames 4

namesArr.length 8

Page 5: NamesNamesclass •Stores a list of unique names in alphabetical order. •Allows look-up, insert, and removal of names in the list. •Uses partially-filled array representation •Names.java

Lookup test cases• Returns true iff target is present in names

Names ex [Bono] 5

AnneBobCarolDonEd

0

1

2

3

4

namesArr Test cases

numNames 5

Page 6: NamesNamesclass •Stores a list of unique names in alphabetical order. •Allows look-up, insert, and removal of names in the list. •Uses partially-filled array representation •Names.java

Lookup code notes• Returns true iff target is present in names

Names ex [Bono] 6

AnneBobCarolDonEd

0

1

2

3

4

namesArr

numNames 5

Page 7: NamesNamesclass •Stores a list of unique names in alphabetical order. •Allows look-up, insert, and removal of names in the list. •Uses partially-filled array representation •Names.java

Remove test casesRemoves target from names object, and returns true.If target wasn't present in names, returns false and no change

made to names.

Names ex [Bono]

AnneBobCarolDonEd

0

1

2

3

4

namesArr

Test cases

numNames 5

7

Page 8: NamesNamesclass •Stores a list of unique names in alphabetical order. •Allows look-up, insert, and removal of names in the list. •Uses partially-filled array representation •Names.java

Reuse code to test removepublic static void testRemove() {

Names names = new Names();names.loadNames();System.out.println("Attempt remove: Scotty");boolean removed = names.remove("Scotty");if (!removed) {

System.out.println("Scotty was not present");}System.out.println("Names in list [exp: Anne Bob Carol Don Ed]: ");

names.printNames();System.out.println(

"Number of names in list [exp: 5]: "+ names.numNames());

}Names ex [Bono] 8

Page 9: NamesNamesclass •Stores a list of unique names in alphabetical order. •Allows look-up, insert, and removal of names in the list. •Uses partially-filled array representation •Names.java

Implementing remove: outlineRemoves target from names object, and returns true.If target wasn't present in names, returns false and no change made to names.

public boolean remove(String target) {

Names ex [Bono]

AnneBobCarolDonEd

0

1

2

3

4

namesArr

numNames 5

9

Page 10: NamesNamesclass •Stores a list of unique names in alphabetical order. •Allows look-up, insert, and removal of names in the list. •Uses partially-filled array representation •Names.java

Minimize amount of code

• Reuse lookup loop?• It returns boolean• Refactor!

Names ex 2 [Bono] 10

Page 11: NamesNamesclass •Stores a list of unique names in alphabetical order. •Allows look-up, insert, and removal of names in the list. •Uses partially-filled array representation •Names.java

New helper function/**

lookupLoc returns index of target in namesArror NOT_FOUND if it is not present

*/

private int lookupLoc(String target)

Names ex 2 [Bono] 11

Page 12: NamesNamesclass •Stores a list of unique names in alphabetical order. •Allows look-up, insert, and removal of names in the list. •Uses partially-filled array representation •Names.java

Refactored lookup that uses lookupLoc

public boolean lookup(String target)

Names ex 2 [Bono] 12

Page 13: NamesNamesclass •Stores a list of unique names in alphabetical order. •Allows look-up, insert, and removal of names in the list. •Uses partially-filled array representation •Names.java

Implementing remove

Names ex 2 [Bono]

AnneBobCarolDonEd

0

1

2

3

4

namesArr

numNames 5

13

Removes target from names object, and returns true.If target wasn't present in names, returns false and no change made to names.

public boolean remove(String target) {