<jupyter_start><jupyter_text>Generics and Interfaces  Loading LibrariesLet's import all the necessary packages first!<jupyter_code>import java.util.*;
import java.lang.*;<jupyter_output><empty_output><jupyter_text>ObjectivesThe objectives of this worksheet are as follows:* Introduce the basics of interfaces and how they're utilized* Show the basics of creating an implementing interfaces* Show the basics of making generic classes and generic interfaces in Java Using JupyterA few things to remind you with regard to using this Jupyter environment:1. If the platform crashes don't worry. All of this is in the browser so just refresh and all of your changes up to your last save should still be there. For this reason we encourage you to **save often**.2. Be sure to run cells from top to bottom.3. If you're getting strange errors to go: Kernel->Restart & Clear Output. From there, run cells from top to bottom. Additionally keep an eye out for the badges in each section they give an indication for which sections are inclass activities .  Interfaces Before we dive in it is worth understanding the core objective of interfaces. Recall several worksheets ago when I stated the difference between:* `List lst = new ArrayList();`* `ArrayList lst = new ArrayList();`Recall that we prefer the former unless we specifically require some functionality or implementation details of `ArrayList`. This is because the `List` interface guarantees that all implementations of `List` are guaranteed to have some core set of methods implemented. We will be covering in this section why that is, how interfaces are created, how classes implement them, and how this impacts the code we write. We will be doing this with the intention of providing a deeper understanding of how ADTs are implemented in Java along with a deeper understanding of why we program to interfaces rather than implementations.  Defining an InterfaceDefining an interface is syntactically very similiar to defining a class. However, there are three key differences:1. We use the `interface` keyword rather than the `class` keyword.2. There are not attributes3. We provide method headers but **not the implementation of the method**.For example, the `List` interfaces we've been using in previous classes might look something like this:```javainterface List{    public boolean add(Object value);    public Object get(int index);    public void remove(int index);}```Your task is to create an interface for a `Shape`. This interface should have the following `public` methods:* getArea() which returns an double* getPerimeter() which also reaturns a double<jupyter_code>/* Define your shape interface here */<jupyter_output><empty_output><jupyter_text>Implementing InterfacesNow that our `Shape` interface has been defined lets create two classes that implement it. Here, we define a class and then use the `implements` keyword followed the interface we want to implement. For the `ArrayList` impelmentation of the `List` interface this process might look something like this:```javaclass ArrayList implements List{    public int add(Object value){        /* rest of definition here */    }        public Object get(int index){        /* rest of definition here */    }        public void remove(int index){        /* rest of definition here */    }    }```Your task will be to create two implementations of the `Shape` interface you created above.  The Hexagon ClassThe `Hexagon` class should be implemented to have:* Attributes:    * sideLength (int)* Implementations for the following methods from the `Shape` interface:    * getArea() where the area of a hexagon is defined by (3*sqrt(3))/2 * (sideLength^2)    * getPerimeter()<jupyter_code>/* Implement the interface */
/* Test your code here */
Shape hex1 = new Hexagon(3.43);
System.out.println(hex1.getArea());
System.out.println(hex1.getPerimeter());<jupyter_output><empty_output><jupyter_text>The Equilateral Triangle ClassThe `EquilateralTriangle` class should be implemented to have:* Attributes:    * sideLength (int)* Implementations for the following methods from the `Shape` interface:    * getArea() where the area of an equilateral triangle is defined by sqrt(3)/4 * (sideLength^2)    * getPerimeter()<jupyter_code>/* Implement the interface here */
/* Test your code here */
Shape hex1 = new EquilateralTriangle(3.43);
System.out.println(hex1.getArea());
System.out.println(hex1.getPerimeter());<jupyter_output><empty_output><jupyter_text>List of Shapes<jupyter_code>List<Shape> shapeList = new ArrayList<>();
shapeList.add(new EquilateralTriangle(1.4));
shapeList.add(new Hexagon(0.25));
shapeList.add(new Hexagon(7.0));
shapeList.add(new EquilateralTriangle(7.25));
shapeList.add(new Hexagon(100.5));
shapeList.add(new EquilateralTriangle(75.456));<jupyter_output><empty_output><jupyter_text>Now, use the list of shapes we made to compute the total area of all the shapes in the list.<jupyter_code>public double sumAllShapeAreas(List<Shape> shapes){

}
/* Tests here */
double totalArea = sumAllShapeAreas(shapeList);
System.out.println(totalArea);<jupyter_output><empty_output><jupyter_text>Next, compute the total perimeter of all shapes in the list.<jupyter_code>public double sumAllShapePerims(List<Shape> shapes){
    
}
/* Tests here */
double totalPerim = sumAllShapePerims(shapeList);
System.out.println(totalPerim);<jupyter_output><empty_output><jupyter_text>Generics  Generic InterfacesNow, you might recall that the actual syntax for declaring an instance of some implementation of the `List` interface has `` in it. In between these chatacters we put the object type we want this list to contain. For instance, if we wanted a list of integers we would declare it as `List intList = ...` or if we wanted a list of string we would use `List strList = ...`. This allowence of a collection of arbitrary objects is powered by the usage of generic data types. So, in reality, the Java list interface looks more like this. ```javainterface List{    public boolean add(E e);    public E get(int index);    public void remove(int index);}```The `E` is what is known as a generic type. Since the thing we are getting from the list or adding to the list is an element we use the character `E` to stand for "element". The following are the generic type conventions for the character used to represent the generic type being used:* T - Type* E - Element* K - Key* V - Value* N - NumberThe `E` is just a placeholder that allows us to declare the list with any kind of element. This allows us to have collections of elements without having to create a separate `List` or `add` method for each kind of data we want in our collections.Another example of this that you used in a prior assignment and will use again in future ones is the `Comparable` interface:```javapublic interface Comparable{    public int compareTo(T o);}```In this interface we use the `T` since we use `compareTo` to compare types.*Note:* I've simplified quite a bit just to illustrate the core concepts; however, if you're interest the full source code for the actual List interface can be found [here](http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/util/List.java);Create a generic interface of your choosing. It doesn't need to be anything fancy, just something that we can easily implement in the next phase.<jupyter_code>/* Your code here */<jupyter_output><empty_output><jupyter_text>Generic ClassesFor this class it is good to be aware of how generic interfaces work however, outside of the second mini-assignment we will be primarily focusing on generic classes for the remainder of the semester. These generic classes will allow us to make data structures that can be used to store collections of arbitrary types. The basic template for creating a generic class is as follows:```javaclass ClassName{    //...}```Once upon relying on lists in Java, the `ArrayList` class is a generic class that implements the generic interface `List`. ```javaclass ArrayList implements List{    public boolean add(E e){        /* Code here */    }    public E get(int index){        /* Code here */    }    public void remove(int index){        /* Code here */    }}```For this actvity implement the interface you created and test it by creating an instance and using some of the functions you defined.<jupyter_code>/* Generic class here */
/* Test here */<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07558623973336508, 'fraction_numerical': 0.005475538626354005, 'mean_word_length': 4.8550522648083625, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '496887', 'license': 'MIT', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2100, 'n_words': 1089, 'path': 'worksheet-2-generics-and-interfaces.ipynb', 'repo_name': 'CoffeePoweredComputers/487-data-structures-worksheets', 'size': '8401'}