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

JAVA Helpful classes for use in project: Sets - http://web.cse.ohio-state.edu/so

ID: 3706244 • Letter: J

Question

JAVA

Helpful classes for use in project:

Sets - http://web.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/29.Set.pdf

Queue's - http://web.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/27.Queue.pdf

Stack's - http://web.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/32.Stack.pdf

Map's - http://web.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf

The Problem

-Your customer, Cy Burnett, heads a major publisher of textbooks who is migrating toward on-line access. Cy wants a relatively easy-to-maintain glossary facility. (A glossary is a list of difficult or specialized words, or terms, with their definitions, that is usually near the end of a book.) His initial requirements are as follows:

The output shall be a group of HTML files. There shall be a top-level index which merely lists each term in the glossary, and separate pages for each of the terms with their definitions. Clicking on a term in the index shall take you to the page for that term and its associated definition. Moreover, clicking on any term in the glossary that happens to appear in a definition shall take you to the page for that term and its associated definition.

Every term shall consist of a single "word" (i.e., a term contains no whitespace characters).

The terms shall appear in the glossary index in alphabetical order. In each term's page, that term shall appear in red boldface italics just before its definition.

The entire glossary shall be generated in batch fashion by your program from an input file. This input shall consist of a single term on the first line, its definition on the next one or more lines (terminated by an empty line), another term on the next line, its definition on the next one or more lines (terminated by an empty line), etc. The input shall continue in this fashion through the definition of the last term, which shall end with its terminating empty line. The program shall not check for invalid input; the customer shall be responsible for providing input that meets the stated conditions.

The program shall ask the user for the name of the input file and for the name of a folder where all the output files will be saved. The output folder must already exist and will not be created by the program. The top-level index shall be named index.html and the page for each term shall be named term.html where term is the actual term.

In the preceding item, "name of the input file" and "name of a folder" are to be understood as follows. Each includes the notion of a path to the terminal name. The path may either be relative to a current folder or be absolute from the top of the file system. It would be bad form for the program to insert an implied sub-path or folder prior to or after what the user supplies as input. Similarly, it would be bad form for the program to supply an implied filename extension such as ".txt" after what the user supplies as input. Therefore, the program shall respect the user input as being the complete relative or absolute path as the name of the input file, or the name of a folder, and will not augment the given path in any way, e.g., it will not supply its own filename extension. For example, a reasonable user response for the name of the input file could directly result in the String value "data/terms.txt"; similarly, a reasonable user response for the name of a folder could directly result in the String value "data".

*A sample input file is available at:

*A sample of the corresponding program output is available at:

You should not assume that your glossary must look like this one, which is merely a sample of what the output might be. This sample is just something your requirements analyst and the customer cobbled together by hand for a small glossary, to serve as a possible prototype for the final program output. You should feel free to improve on it.

Explanation / Answer

MainClass.java

import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class MainClass {
public static void main(String args[]) {
  
Scanner sc = new Scanner(System.in);
System.out.println("Please enter input file path:");
String inputFile = sc.next();
System.out.println("Please enter output path:");
String outputPath = sc.next();
  
Input ip = new Input(inputFile);
Map <String, String> terms = new TreeMap<String, String>();
terms = ip.getTerms();
  
Output op = new Output(outputPath);
op.writeTerms(terms);
}

}

Input.java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Input {

private String inputFilename;

public Input(String inputFilename){
this.inputFilename = inputFilename;
}

public Map<String,String> getTerms(){
Map<String,String> map = new TreeMap<String,String>();

BufferedReader br = null;
FileReader fr = null;

try {

//br = new BufferedReader(new FileReader(FILENAME));
fr = new FileReader(inputFilename + "\");
br = new BufferedReader(fr);

String sCurrentLine="",temp="";

while ((sCurrentLine = br.readLine()) != null) {
//key
  
String key = sCurrentLine;
sCurrentLine = br.readLine(); //value
while((temp = br.readLine())!=null && !temp.isEmpty()) {
sCurrentLine += temp;
}

map.put(key,sCurrentLine);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (br != null)
br.close();

if (fr != null)
fr.close();

} catch (IOException ex) {

ex.printStackTrace();
return null;
}

}

return map;
}
}

Output.java

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

public class Output {
String path;
public Output (String path){
this.path = path;
}

public void writeTerms(Map <String, String> terms) {
String data = "";
FileOutputStream out = null;
String first = "<html> " +
"<head> " +
"<title>Sample Glossary</title> " +
"</head> " +
"<body> " +
"<h2>Sample Glossary</h2> " +
"<hr /> " +
"<h3>Index</h3> " +
"<ul> ";
  
String last = "</ul> " +
"</body> " +
"</html> " +
" ";
  
String prefix = "<li><a href="";
String suffix_1 = ".html">";
String suffix_2 = "</a></li> ";
  
File file = new File(path +"\index.html");
try {
file.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(terms.entrySet());
String temp = "";
for (Map.Entry<String, String> term : terms.entrySet()) {

createFileAndWriteTerm(term);
temp = temp + prefix + term.getKey() + suffix_1 + term.getKey()
+ suffix_2;
}
data = first + temp + " " + last;
try {

out = new FileOutputStream(path + "\" + file.getName());
out.write(data.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
  
}

public void createFileAndWriteTerm(Map.Entry<String, String> term) {
  
String title_first = "<html> " +
"<head> " +
"<title>book</title> " +
"</head> " +
"<body> " +
"<h2><b><i><font color="red">";
String title_last = "</font></i></b></h2> ";
  
String def_first = "<blockquote>";
String def_last = "</blockquote> " +
"<hr /> " +
"<p>Return to <a href="index.html">index</a>.</p> " +
"</body> " +
"</html> " +
"";
  
FileOutputStream out = null;
try {
File file = new File(path + "\" + term.getKey() + ".html");
file.createNewFile();
out = new FileOutputStream(path+ "\" + file.getName());
String data = title_first + term.getKey() + title_last +
def_first + term.getValue() + def_last;
out.write(data.getBytes());

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
  
  
}

}

Instructions for execution:

> javac MainClass.java

>javac Input.java

>javac Output.java

>java MainClass