I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code.
I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below my code for those two classes, a test harness I was provided with, and a sample text file used for the assignment.
INDEXER CLASS
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Indexer
{
private List<Word> index;
private String fn;
public Indexer(String filename)
{
this.fn = filename;
index = new ArrayList<Word>();
}
public boolean index()
{
indexfile();
return true;
}
private void lineSplitter(String line, int lineno)
{
String[] words = null;
if (line.length() > 0) {
words = line.split("\\W+");
for (int i = 0; i < words.length; i++) {
boolean flag = false;
Word w = new Word(words[i]);
for (int j = 0; j < index.size(); j++) {
if (index.get(j).getWord().equals(w.getWord())) {
w = index.get(j);
w.addLine(lineno);
flag = true;
break;
}
}
if (!flag) {
w.addLine(lineno);
index.add(w);
}
}
}
}
private void indexfile()
{
try {
Scanner scan = new Scanner(new File(fn));
int count = 1;
String line;
while (scan.hasNextLine()) {
line = scan.nextLine();
lineSplitter(line, count);
count++;
}
scan.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public List find(String w)
{
Word word = null;
for (int j = 0; j < index.size(); j++) {
if (index.get(j).getWord().equals(w)) {
word = index.get(j);
return word.getLines();
}
}
return null;
}
public void dumpList()
{
for (int i = 0; i < index.size(); i++) {
System.out.println(index.get(i).getWord() + " : " +
index.get(i).getLines());
}
}
}
WORD CLASS
import java.util.ArrayList;
import java.util.List;
public class Word
{
private String w;
private List<Integer> lines;
public Word(String word)
{
this.w = word.toLowerCase();
lines = new ArrayList<Integer>();
}
public boolean addLine(int lineno)
{
if(!lines.contains(lineno)){
lines.add(lineno);
}
return true;
}
public String getWord()
{
return w;
}
public List getLines()
{
return lines;
}
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
Word word = (Word) obj;
return word.w.equals(this.w);
}
}
TEST HARNESS
public class PA5 {
public static void main(String[] args) {
Indexer idx = new Indexer(args[0]);
boolean rc = idx.index();
idx.dumpList();
System.out.println("*****************");
List<Integer> x = idx.find("PEACE");
if ( x!= null)
System.out.println(x);
}
}
SAMPLE TEXT
But soft what light through yonder window breaks It is the east and Juliet is the sun Arise fair sun and kill the envious moon Who is already sick and pale with grief
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;
class Indexer {
private NavigableMap<String, Word> wordMap;
private String fn;
public Indexer(String filename) {
this.fn = filename;
wordMap = new TreeMap<String, Word>();
}
public boolean index() {
indexfile();
return true;
}
private void lineSplitter(String line, int lineno) {
String[] words = null;
if (line.length() > 0) {
words = line.split("\\W+");
for (int i = 0; i < words.length; i++) {
if(!wordMap.containsKey(words[i])) {
wordMap.put(words[i], new Word(words[i]));
}
wordMap.get(words[i]).addLine(lineno);
}
}
}
private void indexfile() {
try {
Scanner scan = new Scanner(new File(fn));
int count = 1;
String line;
while (scan.hasNextLine()) {
line = scan.nextLine();
lineSplitter(line, count);
count++;
}
scan.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public NavigableSet<Integer> find(String w) {
if(wordMap.containsKey(w)) {
return wordMap.get(w).getLines();
}
return null;
}
public void dumpList() {
for(String w: wordMap.keySet()) {
System.out.println(w + ": " + Arrays.toString(find(w).toArray()));
}
}
}
class Word {
private String w;
private NavigableSet<Integer> lines;
public Word(String word) {
this.w = word.toLowerCase();
lines = new TreeSet<Integer>();
}
public boolean addLine(int lineno) {
if (!lines.contains(lineno)) {
lines.add(lineno);
}
return true;
}
public String getWord() {
return w;
}
public NavigableSet<Integer> getLines() {
return lines;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
Word word = (Word) obj;
return word.w.equals(this.w);
}
}
public class PA5 {
public static void main(String[] args) {
Indexer idx = new Indexer(args[0]);
idx.index();
idx.dumpList();
System.out.println("*****************");
NavigableSet<Integer> x = idx.find("PEACE");
if (x != null)
System.out.println(Arrays.toString(x.toArray()));
}
}
please upvote.
I've previously completed a Java assignment where I wrote a program that reads a given text...
PLEASE EDIT THE LAST 3 METHODS WHERE I PUT STARS AND DO WHAT THE COMMENTS SAY. THE CODE IS IN JAVA PROGRAMMING LANGUAGE. import java.util.AbstractList; import java.util.List; import java.util.RandomAccess; import java.lang.RuntimeException; import java.util.Arrays; public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess { protected Object[] data; protected int size; public int size() { return size; } private void rangeCheck(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException(""); } @SuppressWarnings("unchecked") private E...
I have a Graph.java which I need to complete four methods in the java file: completeGraph(), valence(int vid), DFS(int start), and findPathBFS(int start, int end). I also have a JUnit test file GraphTest.java for you to check your code. Here is Graph.java: import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /* Generic vertex class */ class Vertex<T> { public T data; public boolean visited; public Vertex() { data = null; visited = false; } public Vertex(T _data) { data =...
I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...
Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...
Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class LibraryCard { private String id; private String cardholderName; ...
I have a text file that this project needs to read from that has every possible combination of words but I am unsure how to do it. Also I'm pretty sure this program could use a program called "myArraylist" to sort through the text file but I am unsure how to implement this. I am unsure how to show the text file because it is massive, and I don't think it is possible to upload a text file to chegg....
I need help with this. I need to create the hangman game using char arrays. I've been provided with the three following classes to complete it. I have no idea where to start. HELP!! 1. /** * This class contains all of the logic of the hangman game. * Carefully review the comments to see where you must insert * code. * */ public class HangmanGame { private final Integer MAX_GUESSES = 8; private static HangmanLexicon lexicon = new HangmanLexicon();...
Look for some finshing touches java help with this program. I just two more things added to this code. A loop at the end that will ask the user if they want to quit if they do want to quit the program stops if they don't it loads a new number sequence. import java.util.Random; import java.util.ArrayList; import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.util.Collections; public class main { public static void main(String[] args) { List < Sequence > list =...
How can the java code be edited that when someone exits the lot, they receive an exiting timestamp. And also i would need that exit timestamp to be subtracted from the timestamp the driver gets when entering the lot to find out the total amount of time they were in the parking lot. please explain. should only need a couple of lines added. //////////////////////////////ParkingCarInfo.java/////////////////////////////////////////// package test; import java.sql.Timestamp; //Class: ParkingCarInfo public class ParkingCarInfo { private String name; //name private Timestamp...
Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...