аЯрЁБс>ўџ ?Aўџџџ>џџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџџьЅС!` №П bjbj\­\­ 7.>Ч>Ч џџџџџџЄ8888888LД Д Д Д Ш 4Lˆ'Ж!!!!!!!!' ' ' ' ' ' '$>(hІ*z-'8!!!!!-'88!!B'###!8!8!'#!'##V#i88Я&!ќ !sЬчиЦД !њП&'X'0ˆ'Ч& +"  +Я& +8Я&8!!#!!!!!-'-'# !!!ˆ'!!!!LLLdА LLLАLLL888888џџџџ Design & Java Interfaces Chapter 1 ADTs & Java Interfaces (review) A major goal of software engineering is to write reusable code One way to make code reusable is to encapsulate the data with the methods that manipulate the data Abstract data type (ADT): The combination of data together with the methods that manipulate the data. A Java interface is a way to specify an ADT The interface specifies the names, parameters, and return values of the ADT methods It does not specify how the methods perform their operations and Does not specify how the data is internally represented Designing a program We want to write a program to provide users with an electronic phone directory. By looking at the I/O necessary and using top-down design we arrived at these sub-problems Read the data from an existing file Add or change an entry Retrieve a phone number given a name Remove an entry Save the modified directory to a file so it can be reloaded The Interface Each of these sub-problems becomes a method in our program Answer question about the Interface Remember that the interface contains methods for the class that will implement the phone directory. Thus, they are instance methods, and must be called with an object of the class. Take the next five or ten minutes to answer the questions on the sheet. What classes will we need Include both classes we write, and classes from the Java API A class that holds the phone directory This is the one that implements the PhoneDirectory interface Class with a main method to test (or use) our class—this is a client of our class Class to hold a single entry of our phone directory I/O classes Scanner class for keyboard & file input PrintWriter class for file output Design of data structures for our classes ArrayBasedPD implements PhoneDirectory We need an array to hold our entries; call it theDirectory Other instance variables size to hold the actual number of entries capacity to hold the length of the array A boolean modified to hold whether the directory has been modified or not We may think of others as we begin to implement The only variables declared as instance variables are those that will be needed by every object of the class DirectoryEntry This is the class that hold the information for a single entry The instance variables would be name and number This class will also need set and get methods to access and change the instance variables Pseudocode for the methods Using top-down design again, we think about the steps to accomplish the goal of each method Before we start, we look over our tasks, and see that some actions need to be taken more than once several method need to add to the array, and several need to find a phone number given a name So, at this point, let’s assume methods add given name and number, and find, given name, and returning number We will worry about writing them later. void loadData(String filename) This method must read the data from a data file and put it in the array. The filename is passed as an argument. Exactly what are the steps to do this? Create a Scanner object to read from the file While not at the eof Read the name Read the number Add the entry to the array Close the file Using top-down design, we won’t worry too much now about adding to the array; that can be done later. String addOrChangeEntry (String name, String number) Look at the comments on the interface for this method to see exactly what it does. Now we have to figure out how to do it Call find see if the entry is new if the name is in the directory Change the number using setNumber from class DirectoryEntry Return the previous value of the number else Add a new entry by calling add Return null Pseudocode for add void add(String na, String num) You want to add to the end of the array size contains the index of the first empty slot, so add there You must create a new object of type DirectoryEntry theDirectory[size] = new DirectoryEntry(name, number); size++ //increment size so you are ready for the next entry There is a possibility that the array is full, in which case this would cause an index out of bounds error So need to check if size >= capacity before adding to the array Pseudocode for find Have to call getName from DirectoryEntry class to do comparisons Also have to call equals from Directory Entry class, since if you use = =, it will compare addresses, not names Loop from 0 to size if theDirectory[i].getname.equals(name) return i else return -1 You write the pseudocode for lookUpEntry() Check the comments on the interface to see What it does what the arguments are, and what it returns Use find and add if necessary Pre- and Postconditions Precondition: a statement of any assumptions or constraints on the method data before the method begins execution Postcondition: a statement that describes the result of executing a method "#$<Ev„ЈГчљћўO ] G H I  Ў § ў џ  ! У б !nu–ЁЗИЙу 8E^bˆЛУ+—˜™Ј 25:=rtзкіњDEF§ѕэцѕиѕиѕиѕиѕиѕиѕэцѕФѕэцѕэцѕФѕиѕФѕФѕэцѕФѕФѕДцДцДцѕэцФѕФѕФѕФѕФѕФѕиѕиѕэцѕh`Bph`Bp5OJQJ\^J&h`Bph`Bp5CJOJQJ\^JaJh`Bph`Bp5CJ\aJ h`Bph`Bph`BpCJaJh`Bph`BpCJaJF#$E„чM y Ю  H I ^ Ў - D i y Е У ў џ їчтнееееЭЭЭтнееЭЭЭЭЭеЭт & Fgd`Bp & Fgd`Bpgd`Bpgd`Bp$„„^„`„a$gd`Bp$a$gd`Bp ўџ # ‡ и ! ; x Ÿ м .bn–ИЙу E^ˆБћњђђђэњххнхххееэњЭннХХН & Fgd`Bp & Fgd`Bp & Fgd`Bp & Fgd`Bp & Fgd`Bp & Fgd`Bpgd`Bp & Fgd`Bpgd`Bpћ+˜™ЈчrstыN|ЎзђEFeж§+їяътяяянниааяяаяяаъияяШ & Fgd`Bp & Fgd`Bpgd`Bpgd`Bp & Fgd`Bpgd`Bp & Fgd`Bp & Fgd`Bp+@N^yˆюя*}ЄЦц"JPo{|ЏзїяяяїчтнееЭЭїїЭїїШнРчч & F gd`Bpgd`Bp & F gd`Bp & Fgd`Bpgd`Bpgd`Bp & Fgd`Bp & F gd`Bp & Fgd`Bp§@yˆэюяЄЉ­цўJPko|Џ:ˆ9MghiŠ‘—Ѕажмы.‚ƒ„"%234LYОЬ   ішінеЮншКшіЊішіЊінКнКншнеЮнКнКнКнКншŸЮнКнКнеЮншншн˜Ю h`BphМp„h`Bp5CJ\aJh`Bph`Bp5OJQJ\^J&h`Bph`Bp5CJOJQJ\^JaJ h`Bph`Bph`BpCJaJh`Bph`BpCJaJh`Bph`Bp5CJ\aJh`Bph`Bp5\4I€Н(hi}О.Bƒ„Џлш34LО  їяяїїъхннннъхеЭЭЭеъхееШgd`Bp & F gd`Bp & Fgd`Bp & Fgd`Bpgd`Bpgd`Bp & Fgd`Bp & Fgd`Bp   §§50P:p`BpАа/ Ар=!А"А# $ %ААаАа а†œ@@ёџ@ NormalCJ_HaJmH sH tH T`T `Bp Heading 17$8$@&H$5CJ,OJQJ\^JaJ,R`R `Bp Heading 2„„ђў7$8$@&H$^„`„ђўCJ aJ R`R `Bp Heading 3„I„џ7$8$@&H$^„I`„џCJaJJ`J `Bp Heading 4„„„Lџ7$8$@&H$^„„`„LџR`R `Bp Heading 5„ь„Lџ7$8$@&H$^„ь`„LџCJaJDA@ђџЁD Default Paragraph FontRi@ѓџГR  Table Normalі4ж l4жaі (kєџС(No List .џџџџ#$E„чMyЮHI^Ў -DiyЕУўџ#‡и !;xŸм.bn–ИЙу E^ˆБћ+˜™Јч r s t  ы N | Ў з ђ  E F e ж § + @ N ^ y ˆ ю я * } Є Ц ц "JPo{|ЏзI€Н(hi}О.Bƒ„Џлш34LО   0€€0€˜0€0€€ 0€$ 0€$ 0€$ 0€$( 0€M( 0€M( 0€M˜0€0€€ 0€I 0€I( 0 €Ў( 0 €Ў( 0 €Ў( 0 €Ў( 0 €Ў 0€I( 0€Е€˜0€У0€€ 0€џ 0€џ 0€џ€˜0€и0€€ 0€! 0€!( 0€x 0€! 0€! 0€!( 0€b( 0€b˜0€–0€€ 0€Й( 0€у( 0€у8 0€E8 0€E8 0 €E8 0!€E( 0"€у€˜0€+ 0#€Й( 0$€™( 0%€™( 0&€™(0€™(0€™0€€ 0'€t  0(€t ( 0)€ы ( 0*€ы  0+€t ( 0,€Ў ( 0-€Ў  0.€t ˜0€ 0€€( 0/€F ( 00€F 8 01€ж 8 02€ж H 03€+ H 04€+ H 05€+ 8 06€ж ( 07€F ˜0€ˆ 0€€ 08€я  09€я ( 0:€} ( 0;€} 8 0<€Ц 8 0=€Ц ( 0>€} 8 0?€J8 0@€J80€J0€€ 0A€|( 0B€( 0C€( 0D€( 0E€( 0F€( 0G€( 0H€€˜0€(0€€ 0I€i 0J€i 0K€i 0L€i˜0€B0€€ 0M€„( 0N€Џ( 0O€Џ( 0P€Џ 0€€€˜0€€0€€ 0€€ 0€€0€€˜0€€˜0€€$E„чMyЮI^Ў -DiyЕУџ#‡и!;xŸм.bn–Йу E^ˆБћ+™Јч r s t  ы N | Ў з ђ  F e ж § + @ N ^ y ˆ я * } Є Ц ц "JPo{|ЏзI€Н(i}О.B„Џлш4LО   0€€0€0€€ 0€# 0€# 0€# 0€#( 0€L( 0€L* 0€L0€€ 0€G 0€G( 0 €Ќ( 0 €Ќ( 0 €Ќ( 0 €Ќ( 0 €Ќ 0€G* 0€Г0€€ 0€ќ 0€ќ 0€ќ0€€ 0€ 0€( 0€t 0€ 0€ 0€( 0€^* 0€^0€€ 0€Д( 0€о( 0€о8 0€@8 0€@8 0 €@8 0!€@* 0"€о 0#€Д( 0$€“( 0%€“( 0&€“(0€“(0€“0€€ 0'€n  0(€n ( 0)€х ( 0*€х  0+€n ( 0,€Ј ( 0-€Ј  0.€n 0€€( 0/€? ( 00€? 8 01€Я 8 02€Я H 03€$ H 04€$ H 05€$ 8 06€Я * 07€? 0€€ 08€ч  09€ч ( 0:€u ( 0;€u 8 0<€О 8 0=€О ( 0>€u 8 0?€B8 0@€B80€B0€€ 0A€t( 0B€‡( 0C€‡( 0D€‡( 0E€‡( 0F€‡( 0G€‡* 0H€‡0€€ 0I€` 0J€` 0K€` 0L€`0€€ 0M€z( 0N€Ѕ( 0O€Ѕ( 0P€Ѕ 0Q€z0€€ 0R€) 0S€)0€)0€ўxŠ0˜ § џ ћ+   $+^bˆГКN U } „ з к ђ ѕ F J Ц Ш JN“злIV…‰BDЁЎшь 343333333333333333333$EI^-DiyЕУbn–Зу E^™Јt  F e Џ ж + ^ y ˆ JPoi}.Bл34Lўџџџ\ŠЈџџџџџџџџџ* ўџџџ€=\ўџџџ^\ўџџџp^\ўџџџШ^\ўџџџ _\ўџџџ45\ўџџџДм\ўџџџн\ўџџџ\н\ўџџџДн\ўџџџ о\џџџџд]\ @CJOJQJ^Jo(" џџџџ$^\рqL@CJOJQJ^Jo( џџџџ|^\ €@CJ OJQJ^Jo(" џџџџд^\ 5М@CJOJQJ^Jo( џџџџ,_\ Bќ@CJOJQJ^Jo(" џџџџpм\ $@CJOJQJ^Jo(" џџџџРм\ cV@CJOJQJ^Jo(" џџџџн\ nŒ@CJOJQJ^Jo(" џџџџhн\ zŠ@CJOJQJ^Jo( џџџџРн\ —в@CJOJQJ^Jo( џџџџо\ Љr@CJOJQJ^Jo(" џџџџх` K-`Bp*