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

Help finishing this java homework. I already did the first part all i have to do

ID: 3576938 • Letter: H

Question

Help finishing this java homework.

I already did the first part all i have to do now is to add a class and done but i can't manage to do it.

The following code is my homework done:

File 1: ScannerWithLineno

import java.util.*;
import java.io.*;

public class ScannerWithLineno implements AutoCloseable {
protected Scanner sc;

protected String buffer;
protected Scanner bufferScanner;

protected int lineno = 0;

public int getLineno() {
return lineno;
}

public String getCurrentLine() {
return this.buffer;
}

public ScannerWithLineno(File source) throws FileNotFoundException {
sc = new Scanner(source);
getNextLine(); // Initialize starting line
}

public ScannerWithLineno(InputStream source) {
sc = new Scanner(source);
getNextLine(); // Initialize starting line
}

public void close() {
sc.close();
}

private void getNextLine() {
if (sc.hasNextLine()) {
// You have a line to read so do it.
buffer = sc.nextLine();
lineno++;
// Set up Scanner for that line
bufferScanner = new Scanner(buffer);
bufferScanner.useDelimiter("([\s\.?!`,;:'"\)\(\*\[\]]|--)+");
} else {
// No line so null out important vars
buffer = null;
bufferScanner = null;
}
}

public boolean hasNext() {
do {
if (buffer == null || bufferScanner == null)
return false;
if (bufferScanner.hasNext())
return true;
getNextLine();
} while (true);
}

// Should only be called if hasNext() is true
public String next() {
return hasNext() ? bufferScanner.next() : null;
}

static public void main(String args[]) throws Exception
{
ScannerWithLineno swl = new ScannerWithLineno(new File("alice30.txt"));
while (swl.hasNext())
{
String word = swl.next();
System.out.println(swl.getLineno() + ": " + swl.getCurrentLine() + " " + word);

}
swl.close();
}
}

File 2: TemplateForProjects

import java.io.File;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.TreeMap;

public class TemplateForProjects {


//**********************************************

private static void process(Scanner sc, String args[]) {
TreeMap<String, LinkedHashSet<Integer>> hash=new TreeMap<>();
try
{
ScannerWithLineno swl = new ScannerWithLineno(new File("alice30.txt"));
while (swl.hasNext()) {
String word = swl.next();
if(hash.containsKey(word))
{
hash.get(word).add(swl.getLineno());
}
else
{
LinkedHashSet<Integer> set=new LinkedHashSet<>();
set.add(swl.getLineno());
hash.put(word,set);
}
}
swl.close();

//print
for(String key:hash.keySet())
{
System.out.println(key+": "+hash.get(key));
}
}
catch (Exception e) {
}
}

//**********************************************

private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.equalsIgnoreCase("Y");
}

//**********************************************

public static void main(String args[]) {
final String TITLE = "CSC111 Project Template";
final String CONTINUE_PROMPT = "Do this again? [y/N] ";

System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do
{
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println("Thank you for using " + TITLE);
}

}

____________________________________________________________________

This is the missing part:

This is the homework URL to download the files needed: http://cs.stcc.edu/csc-220-hw9-word-cross-reference-assignment/

Thanks.

Update: Friday 12/9/2016 on creating a solution for this assignment, I noticed that the output had a lot of noise words in it. Why in the world would I care what lines the word the was located? So let's add a feature where the user gets to decide whether the noise words are allowed in the output. Ask the users whether noise (common) words are allowed. If they indicate No, then don't include them. In collaboration with Mr. Elias Phillips, he made available a list of what are apparently the top 50o most common words. You decide how best to handle these words. Phillips' Common Word List The nice thing about this list is you could download it and edit out the words you think aren't noise for your application. oh and here is a copy of War and Peace a fairly long novel by Leo Tolstoy you can use to also test out your code. Alice in Wonderland has a lot of strange punctuation in it. War and Peace is more ordinary when it comes to punctuation. Happy Coding!

Explanation / Answer

I have got the complete solution as you needed. The change in the code will be in the file TemplateForProjects.java

There is no change required in ScannerWithLineNo.java.

Source Code:


TemplateForProjects.java

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Scanner;
import java.util.TreeMap;

public class TemplateForProjects {

private static void process(Scanner sc, String args[]) {
TreeMap<String, LinkedHashSet<Integer>> hash = new TreeMap<>();

List<String> noisewordslist = new ArrayList<>();

try {
ScannerWithLineno swl = new ScannerWithLineno(new File("warandpeace.txt"));

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Do you want to use the noise words? (hit Y or y to say yes) ");
char ch = (char) br.read();

Scanner scanner2 = null;
scanner2 = new Scanner(new File("CommonWords.txt"));

while (scanner2.hasNext()) {
String word = scanner2.next();
noisewordslist.add(word);
}
scanner2.close();

while (swl.hasNext()) {
String word = swl.next();
  
if(noisewordslist.contains(word) && (ch=='y'|| ch=='Y'))
{
continue;
}
  
else
{
if (hash.containsKey(word)) {
hash.get(word).add(swl.getLineno());
} else {
LinkedHashSet<Integer> set = new LinkedHashSet<>();
set.add(swl.getLineno());
hash.put(word, set);
}
}
}
swl.close();
//print
for (String key : hash.keySet()) {
System.out.println(key + ": " + hash.get(key));
}

} catch (Exception e) {
}
}

//**********************************************
private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.equalsIgnoreCase("Y");
}

//**********************************************
public static void main(String args[]) {
final String TITLE = "CSC111 Project Template";
final String CONTINUE_PROMPT = "Do this again? [y/N] ";

System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println("Thank you for using " + TITLE);
}

}

Explanation:

First I got all the common words from CommonWords.txt and added in a list. After that, if user has decided to discard them, i.e. the 'ch' variable value is 'y' or 'Y' and then if the word taken from the file is present in that list, continue; statement discards the whole word adding. Otherwise, the implementation is same as above.

I have tested this code for both alice30.txt and warandpeace.txt and it is working like a charm.

Output:

I can't mention the whole outout which is of so much length. I can just say I ran the code for warandpeace.txt and then I first selected 'y' so that noise words gets eliminated. So, the output didn't contain the words. Say for instance, first word of the Commonword.txt was "the". The output doesn't contaiin its entry. Next time I ran the code with 'n' so I got the entry with that word as follows:

the: [9, 11, 15, 17, 18, 20, 23, 28, 33, 38, 41, 45, 51, 53, 54, 60, 61, 67, 73, 77, 83, 90, 91, 93, 94, 95, 97, 100, 102, 107, 112, 113, 119, 120, 121, 122, 124, 126, 127, 132, 133, 136, 139, 142, 148, 151, 161, 162, 163, 172, 173, 186, 187, 196, 208, 209, 210, 212, 218, 219, 220, 226, 232, 233, 237, 250, 251, 252, 255, 267, 268, 271, 275, 278, 279, 284, 285, 286, 288, 294, 298, 300, 307, 312, 316, 321, 322, 326, 329, 331, 333, 334, 343, 344, 347, 349, 350, 360, 367, 368, 371, 373, 376, 378, 381, 382, 384, 385, 387, 398, 400, 402, 403, 410, 414, 415, 417, 423, 429, 430, 431, 432, 438, 439, 442, 443, 446, 448, 449, 450, 453, 457, 466, 468, 470, 471, 472, 475, 480, 484, 487, 491, 495, 502, 505, 509, 514, 518, 520, 521, 522, 526, 527, 530, 533, 534, 535, 539, 540, 541, 542, 543, 547, 548, 550, 551, 552, 557, 562, 563, 564, 565, 567, 568, 578, 579, 583, 589, 591, 593, 594, 599, 603, 604, 608, 614, 618, 622, 624, 626, 627, 628, 632, 633, 643, 644, 650, 651, 656, 660, 664, 665, 668, 670, 672, 675, 678, 688, 693, 701, 712, 718, 727, 732, 737, 742, 746, 748, 759, 760, 762, 763, 764, 766, 770, 777, 781, 783, 784, 785, 786, 790, 791, 792, 793, 801, 802, 804, 810, 813, 814, 815, 817, 818, 823, 826, 829, 830, 831, 836, 839, 844, 845, 847, 850, 851, 855, 857, 863, 869, 874, 875]

So, the code is working as per requirements. Do tell me if there is any confusion or something. Thank you. :)