I know it's generally not a good thing to bring up homework assignments on a website such as this. I am not asking for handouts, nor do I wish anyone to finish anything for me. I said this at the beginning because you never know, someone on here may have had this same assignment. I don't want to be seen as a cheater or in any way relying on this community to complete my homework assignments like a spoiled child. I just sincerely love to program, and I am dying to understand more about it.
I aim merely to understand Generics, and how I can use this to benefit my algorithms. I am a Sophomore in a Data Structures class, and the professor wants us to create a program that creates Key, Value pairs. These key value pairs are individual objects that are created from books such as Tom Sawyer. Each unique word is a key, then the value represents how many times this unique word appeared in a text. He says every Key would be a String, and every Value would be an Integer. Eventually we will have to analyze these books and compare them to each other. I knew that this would require an array of Pair objects. It was simple enough to create the Pairs.
I understand this much, and I was able to successfully create and print these Pair objects without using Generics. However the project gets more complex. I need more then one array and these Pairs wont have an integer as a Value but a Double so that I can find the frequency of words, so I have realized that continuing without using Generics will result in more code than necessary. Others are doing it without Generics but my professor constantly makes remarks that "they are making their life harder for no reason". In all of my previous programming classes there was a lab to accompany them, and we had hands on experience when it came to learning about code. In those classes there was no mention of anything called generics.
So naturally, I want to make my life easier. I want to understand and use this thing called generics, that I have never seen before.
Many other individuals are having trouble with the current assignment, and I have presented my code to tutors and the professor himself. I have sent emails and etc. I have literally tried every avenue I can think of. I have read the Oracle Doc such as this
https://docs.oracle.com/javase/tutorial/java/generics/types.html
But the example here is nowhere near specific enough to help me in my case, either that or the point is just flying right over my head.
He has even extended the deadline for the assignment a whole week, because he was receiving so many emails regarding how to do this.
But no matter who I see, or what they suggest I always end up staring at my code that just refuses to compile. It gives me errors that say "generic array creation", but I am not sure why I can't create this array. I have read many people suggest to not use Arrays, but instead use ArrayList. However the issue is that my professor doesn't want us to use ArrayList. He wants us to essentially build our own. For example I named mine arrayPair.
I have created a generic Pair class. As suggested by him. I have created the generic arrayPair class. This class is really the one that seems to refuse to compile. I have a Driver class which contains the main function, this uses the Scanner and StringTokenizer with delimiters to retrieve all the individual words. I then convert them to lowercase.
And I also have a interface that I named Dictionary
Here is the dictionary interface:
public interface Dic<K,V>
{
void set(K key, V value);
void remove(K key);
void reset();
boolean isFound(K key);
void next();
K getCurrentKey();
V getCurrentValue();
void changeValue(V value);
}
So this compiles, and all is fine.
So far I know what generics do, and why they are useful(at least I think). They allow you to implement the same code in many different ways. You can specify what these "types" are later on, and they can be a String, Integer, or whatever you wish, even objects as well. My professor said that generics will work with integers, strings, and also doubles. But then I do some reading around the internet and I then see this.
http://www.tutorialspoint.com/java/java_dictionary_class.htm
I notice notice that the key and value are objects. Not a String or Integer.
Then I see this:
Why don't Java Generics support primitive types?
So wait a second, Java Generics doesn't support primitive types? So what is my professor talking about?
Also many of the examples or solutions provided on this website use the ArrayList, then I get disappointed because that is exactly what I was told NOT to use.
So perhaps I am missing something very simple, and just the notion of that drives me crazy.
I guess all I can do is provide to you good people, my arrayPair class as it is.
I have changed the way I write the syntax of lines 22 and 40 so many times it's crazy. I've stared at this thing for HOURS. Please note that some of these fields are just there because I was screwing around with different ideas for a while. Some are just useless fields. The error shows up on line 11 and 21 (not including white lines).
public class arrayPair<K, V> implements Dictionary<K, V>
{
private int capacity = 100; //Default capacity for pair array.
private int card = 0;
private Pair reference; //Used to store referenceerence to an array slot.
private int iterator = 0; //Used to iterate through the array.
private String currentKey;
private double currentValue;
private boolean contains;
private boolean stop;
arrayPair[] pairs = new Pair<K,V>[capacity];
private K key;
private V value;
private String temp;
@SuppressWarnings("Unchecked") // I added this because I saw it around online, no idea if it is needed. Or what it dooes.
@Override
public void set(K key, V value)
{
K k = key;
V v = value;
Pair<K, V> pa = new Pair<K, V>();
reference =pa;
if(card == (capacity)-1)
{
doubleSize();
}
if(!this.isFound(key)){
pairs[card] = pa;
card++;
reset();
}
else{
reference.increaseValue();
reset();
}
}
Any help or insight is much appreciated. Just nudge me in the right direction, how in the world should I approach this using Generics? What does it even look like? Then I have seen on this website, that you cast things to an object, I was never told this in my previous classes. So I don't know the syntax required, nor do I know if it is necessary in my case. I also don't see how it can create objects when no class is present to represent them. I have just become so incredibly confused during this semester, but last semester I crushed it. I figured this post is already way to long, so I didn't want to add my other classes. If you need to see the Pair class as well I can provide it. But it is written exactly like I was told it should be. It compiles as well, but to increase the value of Value by 1 is impossible. It says that "the binary operator + 1 cannot be applied to type V".
So something is going on with Generics and the use of Primitives. I just don't understand what. Hell I don't know what to believe anymore!
EDIT:
Here is the Pair class as requested.
public class Pair<K, V>
{
public K key;
public V value;
public Pair(K k, V v)
{
this.key=k;
this.value=v;
}
public K getKey()
{
return this.key;
}
public V getValue()
{
return this.value;
}
public void setValue(V v)
{
this.value = v + 1;
}
public String toString()
{
String statement = ("Key: " + this.key + " " +"Value: " + this.value);
return statement;
}
}