Objectorientedmain class
/** * */package com.jason.kwic.objectoriented;import java.io.ioexception;/** * @author Jasonzhang * */public class Object Orientedmain {public static void main (string[] args) throws IOException {Output output = new output (); Alphabetizer sorter = new Alphabetizer (output); Shift shift = new Shift (sorter), input input = new input (shift), Input.input ("D:\\temp\\mykwic_in.txt"); Shift.shift (); Sorter.sort ();}}
Output class
/** * */package com.jason.kwic.objectoriented;import java.util.arraylist;/** * @author Jasonzhang * */public class Output {public void output (arraylist<string> lines) {System.out.println (' output is '); for (String line:lines) {System.ou T.println (line); }}}
Alphabetizer class
/** * */package com.jason.kwic.objectoriented;import java.util.arraylist;import java.util.Collections;import java.util.Comparator;/** * @author jasonzhang * */public class Alphabetizer {private ArrayList<String> lines = New arraylist<string> ();p Rivate output output;public alphabetizer (Output output ) {this.output = output;} Public void addline (string line) {this.lines.add (line);} Public void sort () { collections.sort (This.lines, new alphaabetizercomparator ()) ; This.output.output (this.lines);} private class alphaabetizercomparator implements comparator<string> {@ Overridepublic int compare (STRING&NBSP;O1,&NBSP;STRING&NBSP;O2) {if (o1 == null | | o2 == null) &NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp; throw new nullpointerexception (); }int comparevalue = 0;char o1firstcharacter = o1.charat (0);char O2firstcharacter = o2.charat (0); if (This.isletter (o1firstcharacter) && This.isletter (o2firstcharacter)) {//If the value of a lowercase letter, the value of the corresponding uppercase letter is converted O1firstcharacter = this.touppercase (O1firstcharacter); O2firstcharacter = this.touppercase (O2firstcharacter);compareValue = O1firstcharacter - o2firstcharacter;} else {throw new runtimeexception ("must be a letter");} Return comparevalue;} Private boolean isletter (char c) {return (c >= 65 && c &NBSP;<=&NBSP;90) | | (c >= 97 && c <= 122);} Private char touppercase (char c) {if (Character.islowercase (c)) {return Character.touppercase (c);} Return c;} }}
Shift class
/** * */package com.jason.kwic.objectoriented;import java.util.arraylist;import java.util.StringTokenizer;/** * @author jasonzhang * */public class Shift {private arraylist<string> lines = new arraylist<string> ();// Private arraylist<string> shifedlines = new arraylist<string> ();p rivate alphabetizer sorter;public shift (Alphabetizer sorter) {this.sorter = Sorter;} Public void addline (string line) {this.lines.add (line);} Public void shift () {for (string line : this.lines) {this.shiftline ( line);}} Private void shiftline (String line) {arraylist<string> wordlist = new ArrayList<String> (); Stringtokenizer st = new stringtokenizer (line, " ");while (St.hasMoreTokens ()) {woRdlist.add (St.nexttoken ());} Int wordnums = wordlist.size (); Stringbuilder templine = new stringbuilder ();//system.out.println ("Shifed is"); (int i=0; i<wordnums; i++) {for (int j= (wordnums - 1 -i) ; j < wordnums; j++) {templine.append (Wordlist.get (j)). Append (' ');} for (int k=0; k< (wordnums - 1 -i); k++) {if (k != ( WORDNUMS&NBSP;-&NBSP;I&NBSP;-&NBSP;2)) {templine.append (Wordlist.get (k)). Append (' ');} else {templine.append (Wordlist.get (k));}} System.out.println (Templine.tostring ());//this.shifedlines.add (templine.tostring ()); This.sorter.addLine ( Templine.tostring ()); Templine.delete (0, templine.length ());}}}
Input class
/** * */package com.jason.kwic.objectoriented;import java.io.file;import Java.io.ioexception;import java.util.scanner;/** * @author Jasonzhang * */public class Input {private shift shift;public input (shift shift) {This.shi FT = shift;} public void input (String filePath) throws IOException {file infile = new File (FilePath); Scanner sc = new Scanner (infile); String templine = null; SYSTEM.OUT.PRINTLN ("Input is"), while (Sc.hasnextline ()) {templine = Sc.nextline (); System.out.println (Templine); This.shift.addLine (Templine);}}
Implementation of KWIC based on object-oriented (abstract data type) style