Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Sets and File I/O A set is an unordered data structure like a bag, but with

ID: 3587738 • Letter: J

Question

Java Sets and File I/O

A set is an unordered data structure like a bag, but with the restriction that no duplicates are allowed. Implement the add() method for ArraySet and LinkedSet. The method should add the item to the set only if it is not already present. It should return true if the item was successfully added, false otherwise:

boolean add (T item);

Test your method using the driver program Sets.java. The output should look like this:

Here's what's in our set

[orange, grape, kiwi, coconut, lime]

Let's add a banana

Was the operation successful? yes

Our set looks like this

[orange, grape, kiwi, coconut, lime, banana]

Let's try to add another orange

Was the operation successful? no

Our set looks like this

[orange, grape, kiwi, coconut, lime, banana]

Trying to add null to our set

null not a possible value!

Next write a program Seuss.java that reads in the text of Green Eggs and Ham (greenEggs.txt), adds the words to a set, and then outputs the contents to another file (words.txt). To ensure that each word is counted only once, you will want to eliminate punctuation and capitalization before adding it to the set. One way to do this is to use a helper method that copies just the letters to a new string:

public static String removePunc (String s)

To determine whether a character is a letter, you could use a method like this:

public static boolean isLetter(char c) {

if (c >= ‘a’ && ...)

… }

To convert a word to lowercase, you can use the String method toLowerCase(). The output file should contain the following 50 words (unalphabetized since we haven’t covered sorting yet):

i am sam that do not like you green eggs and ham them would here or there anywhere in a house with mouse eat box fox could car they are may will see tree let me be train on say the dark rain goat boat so try if good thank

You may also want to print the words on the. screen while you are working on the program.

//Driver Program

//Green Eggs and Ham Text (to be put into a file not a java program)

Explanation / Answer

Given below are the modified and new files . Output is also shown. Please don't forget to rate the answer if it helped. Thank you.


/*
* LinkedSet
*
* linked structure implementation of a set
*
* unordered collection with no duplicates allowed
*/
public class LinkedSet<T> implements Set<T>
{
/*
* inner class to represent node
*/
private class Node
{
private T data;
private Node next;
public Node(T item)
{
data = item;
next = null;
}
}
private Node head;
private int size;
/*
* adds item to bag if not already present
*
* returns true if item successfully added, false if not
*/
public boolean add(T item)
{
checkForNull(item);
if(!contains(item)) //only if not in the set, add it
{
Node n = new Node(item);
if(head == null)
head = n;
else
{
Node curr = head;
while(curr.next != null)
curr = curr.next;
curr.next = n;

}
size++;
return true;

}
else
return false;
}
/*
* removes item from set
*
* returns removed item
*/
public T remove()
{
if (size == 0)
{
return null;
}
T removed = head.data;
head = head.next;
size--;
return removed;
}
/*
* returns item from set
*/
public T get()
{
if (size == 0)
{
return null;
}
return head.data;
}
/*
* returns true if item is in set, false otherwise
*/
  
public boolean contains(T item)
{
Node current = head;
for (int i = 0; i < size; i++)
{
if (current.data.equals(item))
{
return true;
}
current = current.next;
}
return false;
}
/*
* returns size of set
*/
public int size()
{
return size;
}
/*
* returns string representation of contents of set
*/
public String toString()
{
if (size == 0)
{
return "[ ]";
}
String s = "[";
Node current = head;
while (current.next != null)
{
s += current.data + ", ";
current = current.next;
}
return s + current.data + "]";
}
/*
* checks if item is null and throws exception if so
*/
private void checkForNull (T item)
{
if (item == null)
{
throw new IllegalArgumentException("null not a possible value!");
}
}
}


/*
* ArraySet
*
* array-based implementation of a set
*
* unordered collection with no duplicates allowed
*/
public class ArraySet<T> implements Set<T>
{
public static final int DEFAULT_CAPACITY = 10;
private T [] set;
private int size;
/*
* default constructor
*
* set initially set to 10 items
*/
public ArraySet()
{
this(DEFAULT_CAPACITY);
}
/*
* argument constructor
*
* size of set specified by user
*/
@SuppressWarnings("unchecked")
public ArraySet(int capacity)
{
if (capacity <= 0)
{
throw new IllegalArgumentException("size must be positive!");
}
  
set = (T []) new Object [capacity];
size = 0;
}
  
/*
* adds item to set if not already present
*
* returns true if item successfully added, false if not
*/
public boolean add (T item)
{
checkForNull(item);
ensureSpace();
if(!contains(item))
{
set[size++] = item;
return true;
}
else
return false;
}
/*
* removes item from set
*
* returns removed item
*/
public T remove ()
{
if (size == 0)
{
return null;
}
T removed = set[size-1];
set[size-1] = null;
size--;
return removed;
}
/*
* returns item from set
*/
public T get()
{
if (size == 0)
{
return null;
}
return set[size-1];
}
/*
* returns true if item is in set, false otherwise
*/
  
public boolean contains(T item)
{
for (int i = 0; i < size; i++)
{
if (set[i].equals(item))
{
return true;
}
}
return false;
}
  
/*
* returns size of set
*/
public int size()
{
return size;
}
/*
* returns string representation of contents of set
*/
public String toString()
{
if (size == 0)
{
return "[ ]";
}
String s = "[";
for (int i = 0; i < size()-1; i++)
{
s+= set[i] + ", ";
}
return s + set[size-1] + "]";
}
/*
* checks if item is null and throws exception if so
*/
private void checkForNull (T item)
{
if (item == null)
{
throw new IllegalArgumentException("null not a possible value!");
}
}
/*
* doubles size of array if at capacity
*/
private void ensureSpace()
{
if (size == set.length)
{
@SuppressWarnings("unchecked")
T [] larger = (T []) new Object [size * 2];
  
for (int i = 0; i < size; i++)
{
larger[i] = set[i];
}
set = larger;
larger = null;
}
}
}


import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Seuss {
//returs a string retaining only characters and converting to lower case
//all punctuations are removed
public static String removePunc (String s)
{
String result = "";
s = s.toLowerCase();
for(int i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
if(isLetter(ch))
result += ch;
}
return result;
}
public static boolean isLetter(char ch) {
if(ch >= 'a' && ch <= 'z')
return true;
else if(ch >= 'A' && ch <= 'Z')
return true;
else
return false;
}
public static void main(String[] args) {
String filename = "greenEggs.txt";
try {
Scanner inFile = new Scanner(new File(filename));
String word;
LinkedSet<String> set = new LinkedSet<String>();
while(inFile.hasNext())
{
word = inFile.next();
word = removePunc(word);
set.add(word);
}
inFile.close();
//write the set contents to a file
PrintWriter pw = new PrintWriter(new File("words.txt"));
pw.write(set.toString());
pw.close();
System.out.println("Saved the words in set to file words.txt ");
System.out.println(set.toString());
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}

output


Here's what's in our set
[orange, grape, kiwi, coconut, lime]

Let's add a banana
Was the operation successful? yes
Our set looks like this
[orange, grape, kiwi, coconut, lime, banana]

Let's try to add another orange
Was the operation successful? no
Our set looks like this
[orange, grape, kiwi, coconut, lime, banana]

Trying to add null to our set
null not a possible value!

==========

Saved the words in set to file words.txt


[i, am, sam, that, do, not, like, you, green, eggs, and, ham, them, would, here, or, there, anywhere, in, a, house, with, mouse, eat, box, fox, could, car, they, are, may, will, see, tree, let, me, be, train, on, say, the, dark, rain, goat, boat, so, try, if, good, thank]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote