Question
write a program Seuss2.java that uses a map to get the number of occurrences of each word in the book Green Eggs and Ham. The
write a program Seuss2.java that uses a map to get the number of occurrences of each word in the book Green Eggs and Ham. The program should work similarly to the program Seuss.java that you wrote for Lab 4 (i.e., ignore punctuation and capitalization on the words). The data that gets written to the output file should look like this:
[thank=2, good=2, if=1, try=4, so=5, boat=3, goat=4, rain=4, dark=7, the=11, say=5, on=7, train=9, be=4, me=4, let=4, tree=6, see=4, will=21, may=4, are=2, they=2, car=7, could=14, fox=7, box=7, eat=25, mouse=8, with=19, house=8, a=59, in=40, anywhere=8, there=9, or=8, here=11, would=26, them=61, ham=11, and=25, eggs=11, green=11, you=34, like=44, not=84, do=37, that=3, sam=19, am=16, i=85]
//original suess
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Seuss {
public static void main(String[] args) throws FileNotFoundException {
String input = "greenEggs.txt";
PrintWriter a = new PrintWriter(new File("words.txt"));
LinkedSet
try {
Scanner text = new Scanner(new File(input));
String word;
while(text.hasNext()){
word = text.next();
word = removePunc(word);
linkedset.add(word);
}
a.write(linkedset.toString());
text.close();
a.close();
System.out.println(linkedset);
} catch (FileNotFoundException ex) {
System.out.println("File not found!");
}
}
public static String removePunc (String lowercase){
String removepunc = "";
lowercase = lowercase.toLowerCase();
for(int i = 0; i < lowercase.length(); i++){
char ch = lowercase.charAt(i);
if(isLetter(ch))
removepunc += ch;
}
return removepunc;
}
public static boolean isLetter(char c){
if(c >= 'a' && c <= 'z')
return true;
else if(c >= 'A' && c <= 'Z')
return true;
else
return false;
}
}
// Incomplete driver method
public class Seuss2 {
public static void main(String[] args) throws FileNotFoundException {
String input = "greenEggs.txt";
PrintWriter a = new PrintWriter(new File("words.txt"));
Map
try {
} catch (FileNotFoundException ex) {
System.out.println("File not found!");
}
}
public static String removePunc (String lowercase){
String removepunc = "";
lowercase = lowercase.toLowerCase();
for(int i = 0; i < lowercase.length(); i++){
char ch = lowercase.charAt(i);
if(isLetter(ch))
removepunc += ch;
}
return removepunc;
}
public static boolean isLetter(char c){
if(c >= 'a' && c <= 'z')
return true;
else if(c >= 'A' && c <= 'Z')
return true;
else
return false;
}
}
/*
* interface for a map
*
* data stored by key, with no duplicate keys allowed
*/
public interface Map
{
V add (K key, V value);
V remove (K key);
V get(K key);
boolean containsKey(K key);
String getKeys();
int size();
String toString();
}
/*
* linked implementation of a map
*
* data stored by key, with no duplicate keys allowed
*/
public class LinkedMap
{
private class Node
{
private K key;
private V value;
private Node next;
public Node(K key, V value)
{
this.key = key;
this.value = value;
next = null;
}
public String toString()
{
return key + "=" + value;
}
}
private Node head;
private int size;
public LinkedMap ()
{
head = null;
size = 0;
}
/*
* if key in map, replaces old value with new and returns old value
* otherwise adds key value pair and returns null
*/
public V add (K key, V value)
{
checkForNull(key, value);
Node current = head;
for (int i = 0; i < size; i++)
{
if (current.key.equals(key))
{
V oldValue = current.value;
current.value = value;
return oldValue;
}
current = current.next;
}
Node newest = new Node(key, value);
newest.next = head;
head = newest;
size++;
return null;
}
/*
* removes key value pair from map and returns value
* if key not found, returns null
*/
public V remove (K key)
{
Node current = head,
prev = null;
for (int i = 0; i < size; i++){
if (current.key.equals(key)){
V value = current.value;
if(prev != null) {
prev.next = current.next;
}
size--;
return value;
}
previous = current;
current = current.next;
}
return null;
}
/*
* returns value associated with key
* if key not found, returns null
*/
public V get(K key)
{
Node current = head;
for (int i = 0; i < size; i++)
{
if (current.key.equals(key))
{
return current.value;
}
current = current.next;
}
return null;
}
/*
* returns true if key is in map
*/
public boolean containsKey(K key)
{
Node current = head;
while (current != null)
{
if (current.key.equals(key))
{
return true;
}
current = current.next;
}
return false;
}
public int size()
{
return size;
}
public String getKeys()
{
String list = "[";
Node current = head;
while (current.next != null)
{
list += current.key + ", ";
current = current.next;
}
return list + current.key + "]";
}
public String toString()
{
if (size == 0)
{
return "{}";
}
String list = "{";
Node current = head;
while (current.next != null)
{
list += current + ", ";
current = current.next;
}
return list + current + "}";
}
private void checkForNull (K key, V value)
{
if (key == null || value == null)
{
throw new IllegalArgumentException("Key/value can't be null");
}
}
private Node getNode (K key)
{
Node current = head;
int count = 0;
while (current != null)
{
if (current.key.equals(key))
{
return current;
}
current = current.next;
count++;
}
return null;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started