(Solved) : Objective Lab Able Make Use Generic Data Types Junit Testing Java Overview 1 Write Generi Q41393401 . . .

The objective of this lab is to be able to make use of generic data types and JUnit testing in Java; . OVERVIEW: 1. Write a g

Please write the answer code for this in Java

Thanks

The objective of this lab is to be able to make use of generic data types and JUnit testing in Java; . OVERVIEW: 1. Write a generic class Matched. 2. Create a generic class Node. 3. Write a JUnit tester for Stack data structure. 4. Download and use a tester module to ensure that your programs are correct. The programs can also be tested using Dr Java’s interactive window. 5. Prepare the assignment for submission and submit it This is a lab exercise on which collaboration (including discussing the solution on Piazza or searching online sources) is explicitly permitted. It is still in your interest to understand and prepare your final submission yourself. CLASS WITH GENERICS 1. A senior computer science student is working on a social networkig app for the “Senior Project” course. As a part of their project, they want to be able to detect the degree of similarity between two users. To do this, they want to create a data structure in the app called Matched. The data strcuture stores data about a specific user, and two Matched objects can be paired to find the similarity between them. For example, if one user lists the hobbies coding and reading, while the other lists coding, cooking, and fishing, the two have coding in common. A Matched object can compare different types of things, such as hobbies, interests, numerical attributes, etc. Thus, the student would like to create the class using generics. The elements in the Matched are distinct objects by their own right. The objects that make up the Matched (also known as the elements or members) can be anything: numbers, people, countries, letters of the alphabet, other Matched data structures, and so on. Define a generic class to represent Matched that contains elements of some class type. You should choose between one of two ways to store the elements in your Matched class: either it should have an attribute called elements, which is an ArrayList, together with public get and set methods: or, the Matched should derive directly from an ArrayList. Regardless of which way you choose, the Matched must support the following operations: o A default constructor. o public combinedMatched (secondMatched): returns a new object containing the combined elements of the current Matched object with the secondMatched, which is also a Matched. For example, if the object has {reading, cooking, hiking) and secondMatched has cooking, running), the output will be a Matched that contains reading, cooking, and hiking, and running as its four elements. (Note: the data type of the return type as well as the parameter of the method has been purposely omitted – be sure to include it in your implementation) o public commonMatched (secondMatched): returns a new object containing all common elements from this object and secondMatched. For example, if the object has {reading, cooking, hiking} while secondMatched has cooking, running), the output will be a Matched that contains only cooking, o public onlyFirst (secondMatched, element) : returns true if the specified element is only in the current Matched object but not in secondMatched. Returns false if it is not in this object or if it is in secondMatched. o public int cardinality () : returns the number of elements in the Matched. o public boolean equalMatches (secondMatched) : returns true if the two Matched are the same (the number of elements in both are the same, and every element in each is also in the other). 2. Define a generic class called Node. The class has two attributes, key and value. The class Node can represent many things, such as a word in a dictionary where the word as a key and the definition is a value; or an entry in a phone directory where last name is a key and phone number is a value; or NBA player statistics, where the player name is a key and their shooting percentage is a value. The Node class has public set and get methods for both key and value. It also has the following method: o public boolean unique(node): returns true if the key of the this object is different from the key of the node object passed as input: otherwise false. JUNIT TESTING TASK: Stack is one of the basic data structures in programming. It is a linear data structure which follows a particular order in which the operations are performed. The order is called LIFO (Last In First Out). i.e., the last element to be added will be the first one to be processed. A stack data structure typically has the following basic operations: • push (item): Adds the specified item to the top of the stack (we will assume that the stack cannot “fill up”). • pop (): Removes and returns the top item (the most recently added) from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is an error which is known as an underflow error. • peek (): Returns the top element of the stack, without actually removing the item from the stack. • isEmpty(): Returns true if stack is empty, false otherwise. • clear(): Removes all the elements from a stack. Use the link below to download the generic interface which we will use to describe a stack, which contains all of the methods described above. You may also download the sample (working) implementation of a stack below, ArrayListStack, which uses an ArrayList to implement the stack functionality. Your task is to write a JUnit tester class called StackTester for the above methods of a Stack data structure. Assume that the class you will be testing is called ArrayListStack, and that it will implement the Stack data structure. We will test your JUnit tester by using a broken implementation of ArrayListStack in which exactly one of the Stack methods is malfunctioning. Your tester should be able to correctly distinguish the fully working implementation from the broken implementation. Use as many test cases as you think you need, but at least one of them should fail for the broken implementation • https://cs.gmu.edu/-iavramo2/classes/cs211/19/ArrayListStack.java • https://cs.gmu.edu/~iavramo2/classes/cs211/f19/StackInterface.java TESTING: Download the following (if you don’t already have it): • https://cs.gmu.edu/~avramo2/classes/cs211/junit-cs211.jar • https://cs.gmu.edu/ avramo2/classes/cs211/f19/E9Tester.java This is a unit tester which is what we will use to test to see if your classes are working correctly (applies to the first two parts) When you think your classes are ready (you can try them even without the tester), do the following from the command prompt to run the tests. On Windows: javac -cp .; junit-C5211.jar *.java java -cp .; junit-cs211.jar E9Tester On Mac or Linux: javac -cp.junit-C5211.jar *.java java -cp.junit-C5211.jar E9Tester In Dr Java: • open the files, including E9Tester.java, and click the Test button. Show transcribed image text The objective of this lab is to be able to make use of generic data types and JUnit testing in Java; . OVERVIEW: 1. Write a generic class Matched. 2. Create a generic class Node. 3. Write a JUnit tester for Stack data structure. 4. Download and use a tester module to ensure that your programs are correct. The programs can also be tested using Dr Java’s interactive window. 5. Prepare the assignment for submission and submit it This is a lab exercise on which collaboration (including discussing the solution on Piazza or searching online sources) is explicitly permitted. It is still in your interest to understand and prepare your final submission yourself. CLASS WITH GENERICS 1. A senior computer science student is working on a social networkig app for the “Senior Project” course. As a part of their project, they want to be able to detect the degree of similarity between two users. To do this, they want to create a data structure in the app called Matched. The data strcuture stores data about a specific user, and two Matched objects can be paired to find the similarity between them. For example, if one user lists the hobbies coding and reading, while the other lists coding, cooking, and fishing, the two have coding in common. A Matched object can compare different types of things, such as hobbies, interests, numerical attributes, etc. Thus, the student would like to create the class using generics. The elements in the Matched are distinct objects by their own right. The objects that make up the Matched (also known as the elements or members) can be anything: numbers, people, countries, letters of the alphabet, other Matched data structures, and so on. Define a generic class to represent Matched that contains elements of some class type. You should choose between one of two ways to store the elements in your Matched class: either it should have an attribute called elements, which is an ArrayList, together with public get and set methods: or, the Matched should derive directly from an ArrayList. Regardless of which way you choose, the Matched must support the following operations: o A default constructor. o public combinedMatched (secondMatched): returns a new object containing the combined elements of the current Matched object with the secondMatched, which is also a Matched. For example, if the object has {reading, cooking, hiking) and secondMatched has cooking, running), the output will be a Matched that contains reading, cooking, and hiking, and running as its four elements. (Note: the data type of the return type as well as the parameter of the method has been purposely omitted – be sure to include it in your implementation) o public commonMatched (secondMatched): returns a new object containing all common elements from this object and secondMatched. For example, if the object has {reading, cooking, hiking} while secondMatched has cooking, running), the output will be a Matched that contains only cooking, o public onlyFirst (secondMatched, element) : returns true if the specified element is only in the current Matched object but not in secondMatched. Returns false if it is not in this object or if it is in secondMatched. o public int cardinality () : returns the number of elements in the Matched. o public boolean equalMatches (secondMatched) : returns true if the two Matched are the same (the number of elements in both are the same, and every element in each is also in the other). 2. Define a generic class called Node. The class has two attributes, key and value. The class Node can represent many things, such as a word in a dictionary where the word as a key and the definition is a value; or an entry in a phone directory where last name is a key and phone number is a value; or NBA player statistics, where the player name is a key and their shooting percentage is a value. The Node class has public set and get methods for both key and value. It also has the following method: o public boolean unique(node): returns true if the key of the this object is different from the key of the node object passed as input: otherwise false. JUNIT TESTING TASK: Stack is one of the basic data structures in programming. It is a linear data structure which follows a particular order in which the operations are performed. The order is called LIFO (Last In First Out). i.e., the last element to be added will be the first one to be processed. A stack data structure typically has the following basic operations: • push (item): Adds the specified item to the top of the stack (we will assume that the stack cannot “fill up”). • pop (): Removes and returns the top item (the most recently added) from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is an error which is known as an underflow error. • peek (): Returns the top element of the stack, without actually removing the item from the stack. • isEmpty(): Returns true if stack is empty, false otherwise. • clear(): Removes all the elements from a stack. Use the link below to download the generic interface which we will use to describe a stack, which contains all of the methods described above. You may also download the sample (working) implementation of a stack below, ArrayListStack, which uses an ArrayList to implement the stack functionality. Your task is to write a JUnit tester class called StackTester for the above methods of a Stack data structure. Assume that the class you will be testing is called ArrayListStack, and that it will implement the Stack data structure. We will test your JUnit tester by using a broken implementation of ArrayListStack in which exactly one of the Stack methods is malfunctioning. Your tester should be able to correctly distinguish the fully working implementation from the broken implementation. Use as many test cases as you think you need, but at least one of them should fail for the broken implementation • https://cs.gmu.edu/-iavramo2/classes/cs211/19/ArrayListStack.java • https://cs.gmu.edu/~iavramo2/classes/cs211/f19/StackInterface.java TESTING: Download the following (if you don’t already have it): • https://cs.gmu.edu/~avramo2/classes/cs211/junit-cs211.jar • https://cs.gmu.edu/ avramo2/classes/cs211/f19/E9Tester.java This is a unit tester which is what we will use to test to see if your classes are working correctly (applies to the first two parts) When you think your classes are ready (you can try them even without the tester), do the following from the command prompt to run the tests. On Windows: javac -cp .; junit-C5211.jar *.java java -cp .; junit-cs211.jar E9Tester On Mac or Linux: javac -cp.junit-C5211.jar *.java java -cp.junit-C5211.jar E9Tester In Dr Java: • open the files, including E9Tester.java, and click the Test button.

Expert Answer


Answer to The objective of this lab is to be able to make use of generic data types and JUnit testing in Java; . OVERVIEW: 1. Writ…