i need answer only question 7.
please as soon as possible
thank you
i send you again as soon as possible answer please. help me please.
thank you
Algorithms 204th%20dition.pdf Learning Manager.. Chapter 2 basic.. Estale Student A Welcome to Acad. . Other Bookmarks 97 / 969 100% Implementing an abstract data type. As with libraries of static methods, we implement Abys with a Java class, putting the code in a file with the same name as the class, followed by the java extension. The first statements in the file declare in stance variables that define the data type values. Following the instance variables are the constructor and the instance methods that implement operations on datatype values. Instance methods may be public specified in the API) or private used to organize the computation and not available to clients). A data-type definition may have multiple constructors and may also include definitions of static methods. In particular, unit test client main is normally useful for testing and debugging. As a first cumple, we consideran implementation of the Counter ADT that we defined on page 65. A full annotated implementation is shown on the facing page for reference as we discuss its constituent parts. Evely ADT implementation that you will develop has the same basic ingredients as this simple example Instance wariables. To define data type valos (the state of each object), we de rivate Final String clare insie wariables in much the same paint count way as we declare local variables. There is a critical distinction between instance vari Instance variables in Arriva ables and the local variables within a static method or a block that you are accustomed to there is just one value corresponding to each local variable at a given time, but there are numerous values corresponding to each instance variable (one for each object that is an instance of the data type). There is no ambiguity with this arrangement, because cach time that we access an instance variable we do so with an object name that object is the one whose value we are accessing Also, ench declaration is qualified by a visibility mediber. In ADT implementation, we se private, using Java language mechanim to enforce the idea that the representa tion of an ADT is to be hidden from the client, and also final, if the value is not to be changed once it is initialized Counter has two instance variables a string value ane and an int value count. If we were to se public instance variables allowed in lava) the data type would, by definition, not be abstract, so we do not do sa w 0 F F ho 22 * % 5 6 & 7 ) 0 ll + 8 delete { T Y 0 entes H J L retur @ 09 Tab Window Help X thms%204th%20Edition.pdf > Learning Manage. Chapter 2 R basic.. Estalee: Student A... Welcome to Acad... 97 / 969 100% + men DIRK you are ICUSTOM LITUTE IS JUSTO Val corresponinyo each local variable at a given time, but there are numerous values corresponding to each instance variable (one for each object that is an instance of the data type). There is no ambiguity with this arrangement, because each time that we access an instance variable, we do so with an object name-that object is the one whose value we are accessing. Also, each declaration is qualified by a visibility modifier. In ADT implementations, we use private, using a Java language mechansim to enforce the idea that the representa- tion of an ADT is to be hidden from the client, and also final, if the value is not to be changed once it is initialized. Counter has two instance variables: a String value name and an int value count. If we were to use public instance variables (allowed in Java) the data type would, by definition, not be abstract, so we do not do so. Constructors. Every Tava class has at least one constructor that establishes an object's identity. A constructor is like a static method, but it can refer directly to instance vari- ables and has no return value. Generally, the purpose of a constructor is to initialize the instance variables. Every constructor creates an object and provides to the client a reference to that object. Constructors always share the same name as the class. We can overload the name and have multiple constructors with different signatures, just as with methods. If no other constructor is defined, a default no-argument constructor is 1.2 Data Abstraction 85 otv SA AW Mon Feb 8 2:08 AM eople Tab Window Help w + 9 3 JAlgorithms%204th%20Edition pdf nter Learning Manage Chapter 2 basic { Estate Student Welcome to Acad. Other Bookmarks 98 / 969 100% public class Counter private final String name: private int count: public Counter string id) na id) public void increment count) public int tally { return count) public String tostring return count" public static void main(String[] args) Counter heads - new Counter("head") Counter tails = new Countertails"); heads.increment): heads.incremento: tails.incremento de Stout.printintheads.+ tais); Stdout.printinheads tallyo .tails.tally). w Anatomy of a class that defines a data type 3 atv SA Tomas To W Q Mint 207 AM ch Tis Oh for a reusive Runction. This program.com is Vindruma wewe ndo intermed. Find them. What is the problem with this function, conching you can see by just looking the code? We thing in com ce these in cartime Sherw why the new 1. Designing med era ca plecy, the work with the content This could be packaged up in me wwwwwwwww Foresample piwuld brewed 2014. Chacha ed Without the world where the world and ung ing Make the image whes private for apon of the Greefwerd un certior and interfere were tale med med It when the Deeltiplement que dur het toe heet het nett for just carrying data from one place to her er de Note the strong co the names and those who will we can seep 25 od SAW forhold implementatie How can also make the ownevale fiecare anche ile the honoured tincere is no water for the words the imance variables a private prododen Review Jus principles pically the that allow them have all the mother of the house they don't show up in the lead for the con Seesaw pe 101. You can see the Art only the standay following "IK API is the cheape unter Res. Choose her in the panettled All Code APE will be displayed on the night What are the fourtant je met themelet alles have the method, we can set any Show palen ww by writing that he weher Songshal Seminar by character Explain what this comme le doing its Find the values for By Ahow the provale Waters SS. . SE A 2 $ 4 % 5 & 7 1 0 6 8 9 Q W E R 1 T C 1 O S D F G I K L z C V B N . 38 have word "the" and count 2034. Create such a class, named Word Usage, with a constructor taking both able 'word" and "count". For example if the showed up 2034 times in the text, this object would SHO he word and the count, and another constructor taking only the word and using using count 1. Make the instance variables private for proper encapsulation of the objects Give the class getters for word and con netement that adds one to the count. Don't implement equals or other Object methods here: this is a imple object meant for just carrying data from one place to another in our code. Note the strong Java convention that class names are capitalized, but method names and variables start with lower case. See pp. variable as final because we can't change it once the object is created, since there is no setter for the word, and the instance variables are private, protected against direct access. Review Java 00 principles, specifically the idea that all Java classes are subclasses of the object class, and thus must have all the methods of the Object class even if they don't show up in the Java code for the class. See S&W page 101. You can see the Object class API (and any other standard class) by following the link "JDK APT on the class web page under Resources. Choose "Object" in the pane titled All Classes, and the API will be displayed on the right. a. What are the four most important Object methods? (Hint: their names start with e, hg, and t). Because all objects have these methods, we can use HashSet to contain almost any set of objects (but we'll stick to sets of similar type objects in our work). b. Show equals in use by writing one if (...) that checks whether s Strings is equal to a String t. character by character. Explain what this comparison is doing ifs "abc" and t="abx". Compare this to what happens in if(s == 1). c. Find the hashCode and toString values for String s="xyz". Also for the Integer of value 8. 9. Interfaces a. What is a Java interface? See S&W pape 100 for an example. Note that an interface can have constants as well as methods, so the statement there that an interface is nothing more than a list of instance methods" is not completely true b. Write a Java interface source file Union Find java to (as far as possible) express the API on page 219. the union-lind AP1. Also rewrite just the first line of UFjava to assert that class UF implements this interface. c. What methods constructors of UF javn, page 221, are not in the interface? d. Does WeightedQuickUf, page 228. qualify for implementing the interface? Explain why or why not Edit View Go Tools Window Help Algorithms 4th Edition.pdf Page 13 of 69 a a Q Search 100 CHAPTER 1 - Fundamentals Interface inheritance. Java provides language support for defining relationships among objects, known as inheritance. These mechanisms are widely used by software developers, so you will study them in detail if you take a course in software engineer ing. The first inheritance mechanism that we consider is known as subtyping, which allows us to specify a relationship between otherwise unrelated classes by specifying in an interface a set of common methods that each implementing class must contain. An interface is nothing more than a list of instance methods. For example, instead of using our informal API, we might have articulated an interface for Date: public interface Datable int month: int day : int year: and then referred to the interface in our implementation code public class Date implements Datable // implementation code (same as before) } so that the Java compiler will check that it matches the interface. Adding the code implements Datable to any dass that implements month day, and year pro- vides a guarantee to any client that an object of that class can invoke those methods. This arrangement is known as interface inheritance-an implementing class inherits the interface. Interface inheritance allows us to write client programs that can manipulate 8 stv RA 20 t View Go Tools Window Help Algorithms 4th Edition.pdf Page 113 of 960 Search interface section 1/ implementation code (same as before) } so that the Java compiler will check that it matches the interface. Adding the code implements Datable to any class that implements month, day, and year pro- vides a guarantee to any client that an object of that class can invoke those methods. This arrangement is known as interface inheritance-an implementing class inherits the interface. Interface inheritance allows us to write client programs that can manipulate objects of any type that implements methods the interface (even a type to be creat- java.lang.Comparable compare To ed in the future), by invoking meth- ods in the interface. We might have comparison java.util.Comparator compare used interface inheritance in place of our more informal APIs, but chose not to do so to avoid dependence on java.lang. Iterable iterator specific high-level language mecha- nisms that are not critical to the hasNext understanding of algorithms and java.util. Iterator next() 1.3 to avoid the extra baggage of inter- face files. But there are a few situa- Java Interfaces used in this book tions where Java conventions make 2.1 2.5 iteration remove tv RA Edit View Go Tools Window Help 15 Algorithms 4th Edition.pdf Page 232 of 960 a Search LEVER connectivity problem specification requires only that our program be able to determine whether or not any given pair p q is connected, and not that it be able to demonstrate a set of connections that connect that pair. Such a requirement makes the problem more difficult and leads us to a different family of algorithms, which we consider in SECTION 4.1. To specify the problem, we develop an API that encapsulates the basic operations that we need: initialize, add a connection between two sites, identify the component containing a site, determine whether two sites are in the same component, and count the number of components. Thus, we articulate the following API: public class UF UF(int N) initialize N sites with integer names (O to N-1) void union(int p. int 4) add connection between panda int find(int p) component identifier for p (o to N-1) boolean connected (int p, int a return true if p and a are in the same component int counto number of components Union-find API H The union operation merges two components if the two sites are in different com- ponents, the find operation returns an integer component identifier for a given site, the connected operation determines whether two sites are in the same component, and the count method returns the number of components. We start with N compo- nents, and each union that merges two different components decrements the num- ber of components by 1. As we shall soon see, the development of an algorithmic solution for dynamic con 8 View Go Tools Window Help * Algorithms 4th Edition.pdf Q Search Page 232 of 969 Define a data structure to represent the known connections Develop efficient union(), find, connected, and count() implementa- tions that are based on that data structure As usual, the nature of the data structure has a direct impact on the efficiency of the algorithms, so data structure and algorithm design go hand in hand. The API already specifies the convention that both sites and components will be identified by int val. ues between 0 and N-1, so it makes sense to use a site-indexed array id[] as our basic 220 CHAPTER1 Fundamentals data structure to represent the components. We always use the name of one of the sites in a component as the component identifier, so you can think of each component as being represented by one of its sites. Initially, we start with N components, each site in its own component, so we initialize id[1] to i for all i from 0 to N-1. For each site 1, we keep the information needed by find to determine the component contain- ing i in id[i], using various algorithm-dependent strategies. All of our implementa- tions use a one-line implementation of connected that returns the boolean value find(p) -- find(q). 001 otv RAW o Of 969 a u Search ALGORITHM 1.5 Union-find implementation public class UF public private int[] id; // access to component id (site indexed) private int count; // number of components public UF(int N) { // Initialize component id array. count - N; id - new int[N]: % java UF Learning Manage. Chapter 2 R basic.. Estalee: Student A... Welcome to Acad... 97 / 969 100% + men DIRK you are ICUSTOM LITUTE IS JUSTO Val corresponinyo each local variable at a given time, but there are numerous values corresponding to each instance variable (one for each object that is an instance of the data type). There is no ambiguity with this arrangement, because each time that we access an instance variable, we do so with an object name-that object is the one whose value we are accessing. Also, each declaration is qualified by a visibility modifier. In ADT implementations, we use private, using a Java language mechansim to enforce the idea that the representa- tion of an ADT is to be hidden from the client, and also final, if the value is not to be changed once it is initialized. Counter has two instance variables: a String value name and an int value count. If we were to use public instance variables (allowed in Java) the data type would, by definition, not be abstract, so we do not do so. Constructors. Every Tava class has at least one constructor that establishes an object's identity. A constructor is like a static method, but it can refer directly to instance vari- ables and has no return value. Generally, the purpose of a constructor is to initialize the instance variables. Every constructor creates an object and provides to the client a reference to that object. Constructors always share the same name as the class. We can overload the name and have multiple constructors with different signatures, just as with methods. If no other constructor is defined, a default no-argument constructor is 1.2 Data Abstraction 85 otv SA AW Mon Feb 8 2:08 AM eople Tab Window Help w + 9 3 JAlgorithms%204th%20Edition pdf nter Learning Manage Chapter 2 basic { Estate Student Welcome to Acad. Other Bookmarks 98 / 969 100% public class Counter private final String name: private int count: public Counter string id) na id) public void increment count) public int tally { return count) public String tostring return count" public static void main(String[] args) Counter heads - new Counter("head") Counter tails = new Countertails"); heads.increment): heads.incremento: tails.incremento de Stout.printintheads.+ tais); Stdout.printinheads tallyo .tails.tally). w Anatomy of a class that defines a data type 3 atv SA Tomas To W Q Mint 207 AM ch Tis Oh for a reusive Runction. This program.com is Vindruma wewe ndo intermed. Find them. What is the problem with this function, conching you can see by just looking the code? We thing in com ce these in cartime Sherw why the new 1. Designing med era ca plecy, the work with the content This could be packaged up in me wwwwwwwww Foresample piwuld brewed 2014. Chacha ed Without the world where the world and ung ing Make the image whes private for apon of the Greefwerd un certior and interfere were tale med med It when the Deeltiplement que dur het toe heet het nett for just carrying data from one place to her er de Note the strong co the names and those who will we can seep 25 od SAW forhold implementatie How can also make the ownevale fiecare anche ile the honoured tincere is no water for the words the imance variables a private prododen Review Jus principles pically the that allow them have all the mother of the house they don't show up in the lead for the con Seesaw pe 101. You can see the Art only the standay following "IK API is the cheape unter Res. Choose her in the panettled All Code APE will be displayed on the night What are the fourtant je met themelet alles have the method, we can set any Show palen ww by writing that he weher Songshal Seminar by character Explain what this comme le doing its Find the values for By Ahow the provale Waters SS. . SE A 2 $ 4 % 5 & 7 1 0 6 8 9 Q W E R 1 T C 1 O S D F G I K L z C V B N . 38 have word "the" and count 2034. Create such a class, named Word Usage, with a constructor taking both able 'word" and "count". For example if the showed up 2034 times in the text, this object would SHO he word and the count, and another constructor taking only the word and using using count 1. Make the instance variables private for proper encapsulation of the objects Give the class getters for word and con netement that adds one to the count. Don't implement equals or other Object methods here: this is a imple object meant for just carrying data from one place to another in our code. Note the strong Java convention that class names are capitalized, but method names and variables start with lower case. See pp. variable as final because we can't change it once the object is created, since there is no setter for the word, and the instance variables are private, protected against direct access. Review Java 00 principles, specifically the idea that all Java classes are subclasses of the object class, and thus must have all the methods of the Object class even if they don't show up in the Java code for the class. See S&W page 101. You can see the Object class API (and any other standard class) by following the link "JDK APT on the class web page under Resources. Choose "Object" in the pane titled All Classes, and the API will be displayed on the right. a. What are the four most important Object methods? (Hint: their names start with e, hg, and t). Because all objects have these methods, we can use HashSet to contain almost any set of objects (but we'll stick to sets of similar type objects in our work). b. Show equals in use by writing one if (...) that checks whether s Strings is equal to a String t. character by character. Explain what this comparison is doing ifs "abc" and t="abx". Compare this to what happens in if(s == 1). c. Find the hashCode and toString values for String s="xyz". Also for the Integer of value 8. 9. Interfaces a. What is a Java interface? See S&W pape 100 for an example. Note that an interface can have constants as well as methods, so the statement there that an interface is nothing more than a list of instance methods" is not completely true b. Write a Java interface source file Union Find java to (as far as possible) express the API on page 219. the union-lind AP1. Also rewrite just the first line of UFjava to assert that class UF implements this interface. c. What methods constructors of UF javn, page 221, are not in the interface? d. Does WeightedQuickUf, page 228. qualify for implementing the interface? Explain why or why not Edit View Go Tools Window Help Algorithms 4th Edition.pdf Page 13 of 69 a a Q Search 100 CHAPTER 1 - Fundamentals Interface inheritance. Java provides language support for defining relationships among objects, known as inheritance. These mechanisms are widely used by software developers, so you will study them in detail if you take a course in software engineer ing. The first inheritance mechanism that we consider is known as subtyping, which allows us to specify a relationship between otherwise unrelated classes by specifying in an interface a set of common methods that each implementing class must contain. An interface is nothing more than a list of instance methods. For example, instead of using our informal API, we might have articulated an interface for Date: public interface Datable int month: int day : int year: and then referred to the interface in our implementation code public class Date implements Datable // implementation code (same as before) } so that the Java compiler will check that it matches the interface. Adding the code implements Datable to any dass that implements month day, and year pro- vides a guarantee to any client that an object of that class can invoke those methods. This arrangement is known as interface inheritance-an implementing class inherits the interface. Interface inheritance allows us to write client programs that can manipulate 8 stv RA 20 t View Go Tools Window Help Algorithms 4th Edition.pdf Page 113 of 960 Search interface section 1/ implementation code (same as before) } so that the Java compiler will check that it matches the interface. Adding the code implements Datable to any class that implements month, day, and year pro- vides a guarantee to any client that an object of that class can invoke those methods. This arrangement is known as interface inheritance-an implementing class inherits the interface. Interface inheritance allows us to write client programs that can manipulate objects of any type that implements methods the interface (even a type to be creat- java.lang.Comparable compare To ed in the future), by invoking meth- ods in the interface. We might have comparison java.util.Comparator compare used interface inheritance in place of our more informal APIs, but chose not to do so to avoid dependence on java.lang. Iterable iterator specific high-level language mecha- nisms that are not critical to the hasNext understanding of algorithms and java.util. Iterator next() 1.3 to avoid the extra baggage of inter- face files. But there are a few situa- Java Interfaces used in this book tions where Java conventions make 2.1 2.5 iteration remove tv RA Edit View Go Tools Window Help 15 Algorithms 4th Edition.pdf Page 232 of 960 a Search LEVER connectivity problem specification requires only that our program be able to determine whether or not any given pair p q is connected, and not that it be able to demonstrate a set of connections that connect that pair. Such a requirement makes the problem more difficult and leads us to a different family of algorithms, which we consider in SECTION 4.1. To specify the problem, we develop an API that encapsulates the basic operations that we need: initialize, add a connection between two sites, identify the component containing a site, determine whether two sites are in the same component, and count the number of components. Thus, we articulate the following API: public class UF UF(int N) initialize N sites with integer names (O to N-1) void union(int p. int 4) add connection between panda int find(int p) component identifier for p (o to N-1) boolean connected (int p, int a return true if p and a are in the same component int counto number of components Union-find API H The union operation merges two components if the two sites are in different com- ponents, the find operation returns an integer component identifier for a given site, the connected operation determines whether two sites are in the same component, and the count method returns the number of components. We start with N compo- nents, and each union that merges two different components decrements the num- ber of components by 1. As we shall soon see, the development of an algorithmic solution for dynamic con 8 View Go Tools Window Help * Algorithms 4th Edition.pdf Q Search Page 232 of 969 Define a data structure to represent the known connections Develop efficient union(), find, connected, and count() implementa- tions that are based on that data structure As usual, the nature of the data structure has a direct impact on the efficiency of the algorithms, so data structure and algorithm design go hand in hand. The API already specifies the convention that both sites and components will be identified by int val. ues between 0 and N-1, so it makes sense to use a site-indexed array id[] as our basic 220 CHAPTER1 Fundamentals data structure to represent the components. We always use the name of one of the sites in a component as the component identifier, so you can think of each component as being represented by one of its sites. Initially, we start with N components, each site in its own component, so we initialize id[1] to i for all i from 0 to N-1. For each site 1, we keep the information needed by find to determine the component contain- ing i in id[i], using various algorithm-dependent strategies. All of our implementa- tions use a one-line implementation of connected that returns the boolean value find(p) -- find(q). 001 otv RAW o Of 969 a u Search ALGORITHM 1.5 Union-find implementation public class UF public private int[] id; // access to component id (site indexed) private int count; // number of components public UF(int N) { // Initialize component id array. count - N; id - new int[N]: % java UF