Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java question /* * interface for a set * * unordered collection, no duplicates allowed */ public interface Set { /* * adds item to

Java question

image text in transcribed

image text in transcribed

/* * interface for a set * * unordered collection, no duplicates allowed */ public interface Set { /* * adds item to set if not already present * * returns true if item successfully added, false if not * */ boolean add (T item); T remove(); T get(); boolean contains (T item); int size(); public String toString(); } 
 /* * ArraySet * * array-based implementation of a set * * unordered collection with no duplicates allowed */ public class ArraySet implements Set { 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  
 /* * LinkedSet * * linked structure implementation of a set * * unordered collection with no duplicates allowed */ public class LinkedSet implements Set { /* * 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) { // to be implemented } /* * 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  
 /* * Sets * * program for testing add() method in ArraySet and LinkedSet */ public class Sets { public static void main (String [] args) { try { Set words = new ArraySet(); //Set words = new LinkedSet(); // create set of fruits String [] fruits = {"orange", "grape", "kiwi", "coconut", "lime"}; for (int i = 0; i  

greenEggs.txt

I am Sam I am Sam Sam I am That Sam I am! That Sam I am! I do not like that Sam I am! Do you like green eggs and ham? I do not like them, Sam I am. I do not like green eggs and ham. Would you like them here or there? I would not like them here or there. I would not like them anywhere. I do not like green eggs and ham. I do not like them, Sam I am. Would you like them in a house? Would you like them with a mouse? I do not like them in a house. I do not like them with a mouse. I do not like them here or there. I do not like them anywhere. I do not like green eggs and ham. I do not like them, Sam I am. Would you eat them in a box? Would you eat them with a fox? Not in a box. Not with a fox. Not in a house. Not with a mouse. I would not eat them here or there. I would not eat them anywhere. I would not eat green eggs and ham. I do not like them, Sam I am. Would you? Could you? In a car? Eat them! Eat them! Here they are. I would not, could not, in a car. You may like them. You will see. You may like them in a tree! I would not, could not in a tree. Not in a car! You let me be! I do not like them in a box. I do not like them with a fox. I do not like them in a house. I do not like them with a mouse. I do not like them here or there. I do not like them anywhere. I do not like green eggs and ham. I do not like them, Sam I am. A train! A train! A train! A train! Could you, would you, on a train? Not on a train! Not in a tree! Not in a car! Sam! Let me be! I would not, could not, in a box. I could not, would not, with a fox. I will not eat them with a mouse. I will not eat them in a house. i will not eat them here or there. I will not eat them anywhere. I do not eat green eggs and ham. I do not like them, Sam I am. Say! In the dark? Here in the dark! Would you, could you, in the dark? I would not, could not, in the dark. Would you, could you, in the rain? I would not, could not, in the rain. Not in the dark. Not on a train. Not in a car. Not in a tree. I do not like them, Sam, you see. Not in a house. Not in a box. Not with a mouse. Not with a fox. I will not eat them here or there. I do not like them anywhere! You do not like green eggs and ham? I do not like them, Sam I am. Could you, would you with a goat? I would not, could not, with a goat! Would you, could you, on a boat? I could not, would not, on a boat. I will not, will not, with a goat. I will not eat them in the rain. I will not eat them on a train. Not in the dark! Not in a tree! Not in a car! You let me be! I do not like them in a box. I do not like them with a fox. I will not eat them in a house. I do not like them with a mouse. I do not like them here or there. I do not like them ANYWHERE I do not like green eggs and ham! I do not like them, Sam I am. You do not like them. So you say. Try them! Try them! And you may. Try them and you may, I say. Sam! If you will let me be, I will try them. You will see. Say! I like green eggs and ham! I do! I like them, Sam I am! And I would eat them in a boat. And I would eat them with a goat... And I will eat them in the rain. And in the dark. And on a train. And in a car. And in a tree. They are so good, so good, you see! So I will eat them in a box. And I will eat them with a fox. And I will eat them in a house. And I will eat them with a mouse. And I will eat them here and there. Say! I will eat them ANYWHERE! I do so like green eggs and ham! Thank you! Thank you, Sam I am! 
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 tem 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) To convert a word to lowercase, you can use the String method toLowerCase(

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions