當前位置:編程學習大全網 - 編程語言 - 幫我做個JAVA題

幫我做個JAVA題

先給妳看下下面的程序的運行結果!!單詞:academic 出現次數:1 單詞長度:8

單詞:and 出現次數:1 單詞長度:3

單詞:applies 出現次數:1 單詞長度:7

單詞:correctly 出現次數:1 單詞長度:9

單詞:exception 出現次數:1 單詞長度:9

單詞:frequency 出現次數:1 單詞長度:9

單詞:grasps 出現次數:1 單詞長度:6

單詞:handling 出現次數:1 單詞長度:8

單詞:mechanism 出現次數:1 單詞長度:9

單詞:society 出現次數:1 單詞長度:7 下面是程序的代碼:壹***有3個類,分別是:Word.java(模型層) FileService.java(業務層) Test.java(視圖層) /************************************************************************************************/package question3.model;import java.io.Serializable;/**

* 實體類Word

* @author admin

*

*/

public class Word implements Serializable,Comparable<Word>{

private String value;//保存單字的值

private Integer frequency ;//單詞出現的頻率

private Integer length;//單詞的長度

/**

* 對單詞分別以字母順序、出現頻率和單詞長度進行排序

*/

@Override

public int compareTo(Word word) {

//先比較字母的順序

if(this.value.hashCode()!=word.getValue().hashCode()){

//區分大小寫

return this.value.trim().compareTo(word.getValue().trim());

// //不區分大小寫

// return this.value.trim().compareToIgnoreCase(word.getValue().trim());

}

//比較出現的頻率

if(this.frequency!=word.getFrequency()){

return this.frequency-word.getFrequency();

}

//比較長度

if(this.length!=word.getLength()){

return this.length-word.getLength();

}

return 0;

}

public String getValue() {

return value;

}

public void setValue(String value) {

this.value = value;

}

public Integer getFrequency() {

return frequency;

}

public void setFrequency(Integer frequency) {

this.frequency = frequency;

}

public Integer getLength() {

return length;

}

public void setLength(Integer length) {

this.length = length;

}

}

/*****************************************************************************************************************/ package question3.service;import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.util.Collection;

import java.util.TreeSet;import question3.model.Word;public class FileService { /**

* 讀取文件

* @param filePath

* @return

*/

public static String readFile(String filePath){

File file=null;

byte[] content=null;

Integer length=null;

BufferedInputStream bis=null;

file=new File(filePath);

try {

bis=new BufferedInputStream(new FileInputStream(file));

length=bis.available();

content=new byte[length];

bis.read(content, 0, length);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally{

if(bis!=null)try{bis.close();}catch(Exception e1){}

}

return new String(content);

}

/**

* 將字符串中的單詞根據空格讀取出來,並保存到集合中

* @param content

* @return

*/

public Collection<Word> handleString(String content){

Collection<Word> wordCol=new TreeSet<Word>();

String[] strArray=content.split(" ");

for(String s:strArray){

if(s!=null && s.trim().length()>0){

Word exitedWord=isWordExisted(wordCol, content);

//不存在

if(exitedWord==null){

Word word=new Word();

word.setFrequency(1);

word.setLength(s.trim().length());

word.setValue(s.trim());

wordCol.add(word);

}else{//存在

exitedWord.setFrequency(exitedWord.getFrequency()+1);

wordCol.add(exitedWord);

}

}

}

return wordCol;

}

/**

* 判斷該單詞是否在集合中存在

* @param wordCol

* @param wordValue

* @return

*/

private Word isWordExisted(Collection<Word> wordCol,String wordValue){

for(Word word:wordCol){

if(word.getValue().trim().equals(wordValue.trim())){

return word;

}

}

return null;

}

/**

* 查看集合中的單詞

* @param wordColl

*/

public void show(Collection<Word> wordCol){

for(Word word:wordCol){

System.out.println("單詞:"+word.getValue()+"\t出現次數:"+word.getFrequency()+"\t單詞長度:"+word.getLength());

}

}

/**

* 將集合中的單詞保存的文件中

* @param wordCol

* @param filePath

*/

public void storeWords(Collection<Word> wordCol,String filePath){

String content=escapeValue(wordCol);

ObjectOutputStream oos=null;

try {

oos=new ObjectOutputStream(new FileOutputStream(new File(filePath)));

oos.write(content.getBytes());

oos.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally{

if(oos!=null)

try{oos.close();}catch(Exception e){}

}

}

/**

* 將集合中的單詞拼成壹個字符串

* @param wordCol

* @return

*/

private String escapeValue(Collection<Word> wordCol){

StringBuffer buffer=new StringBuffer();

for(Word word:wordCol){

if(word!=null && word.getValue()!=null && word.getValue().trim().length()>0){

buffer.append(" "+word.getValue());

}

}

return buffer.toString();

}

}

/**************************************************************************************************************/ package question3.client;import java.util.Collection;import question3.model.Word;

import question3.service.FileService;public class Test {

public static void main(String[] args){

FileService fs=new FileService();

String content=fs.readFile("input.txt");//從改文件中讀取單詞

Collection<Word> wordCol=fs.handleString(content);

fs.show(wordCol);

fs.storeWords(wordCol, "db.txt");//將單詞存在到該文件中

}

}

  • 上一篇:怎樣做好SMT銷售
  • 下一篇:什麽叫遠動裝置?
  • copyright 2024編程學習大全網