JAVA作业单词统计.docx

上传人:b****7 文档编号:11156366 上传时间:2023-02-25 格式:DOCX 页数:17 大小:419.20KB
下载 相关 举报
JAVA作业单词统计.docx_第1页
第1页 / 共17页
JAVA作业单词统计.docx_第2页
第2页 / 共17页
JAVA作业单词统计.docx_第3页
第3页 / 共17页
JAVA作业单词统计.docx_第4页
第4页 / 共17页
JAVA作业单词统计.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

JAVA作业单词统计.docx

《JAVA作业单词统计.docx》由会员分享,可在线阅读,更多相关《JAVA作业单词统计.docx(17页珍藏版)》请在冰豆网上搜索。

JAVA作业单词统计.docx

JAVA作业单词统计

《Java语言》课程作业

(第二次)

 

题目第1题

学院计算机学院

专业网络工程

班别09级

(2)班

学号3109006536

姓名罗晓然

2011年10月24日

一、课程题目

读入一个TXT文本(仅有英文单词序列组成),统计各个单词出现的次数,并以适当的图形形式表示结果。

二、题目分析与设计:

1.题目的需求:

1)按照题意,程序需要有读取TXT文本的功能;

2)读入的TXT文件仅由英文单词序列组成;

3)需要统计各个单词的出现次数;

4)需要以适当的图形形式表示结果;

2.界面设计过程及布局策略:

下图是运行时的程序界面:

作为特定功能的程序,从布局上来看,需要表现出“单词序列”以及“图形”两个主题部分,当然图形中也有单词标记;

考虑到随着读入文件的增大,单词的数量以及单词的出现次数可能会达到一个相当的高度,因此图形部分必须是ViewPort和画板划分的形式,考虑到JAVA里SWING组件的便捷性,对于图形部分使用了一个JPanel和一个JScrollPane存放;

为了便于查找特定的单词,模仿了当今大部分词典程序的设计,在左侧添加了一个列表框用于存放所有单词,双击列表框中的单词项目时,右侧的图表部分可以自动定位到相应的单词位置;

而在菜单栏部分,则是“文件”菜单,菜单下有“打开”选项,通过此选项可以更换当前统计的TXT文件;

程序窗体大小可自由更改,通过相关处理,更改窗体大小不会影响到图表的表现;

综上,我用了BorderLayout的布局方式,这种方式在这个程序中的优点是能让列表框保持一定宽度便于查找单词,同时不会影响图表为主的表现需求;

对于图表的表现形式,我才用了使用最为广泛、兼容性强的柱形图+折线图方式,并通过相应运算使得图形表现更为直观;

3.程序逻辑的实现:

本程序思维导图如下:

程序主要由窗体事件处理、文件操作子程序、绘图相关处理以及数据结构四部分组成;

其中,窗体事件处理部分包括建立窗体、列表框事件处理、菜单事件处理、鼠标事件处理等;

文件操作子程序由打开文件、处理文件数据、单词统计功能组成;

本程序概要流程图如下(完整流程图见下页):

单词统计功能是本程序的主要功能,实现原理为,以非英文字母字符为分隔符划分字节缓存为字符串数组,然后通过比对逐个字符串是否存在于列表框中判定单词是否重复,重复则计数数组相应项计数值加一,不重复则加入到列表中。

最后,单词计数数组、单词数、单词最大长度和单词最大计数作为绘制图表的主要数据用以绘图,其中单词最大长度用以确定图表的横向坐标轴单元格宽度,单词最大计数用以确定图表的高度,单词数用以确定宽度。

绘图部分,使用了JPanel类的paintComponent方法通过Graohics类进行2D绘图。

本程序完整流程图如下:

4.所使用的开发环境:

EclipseSDK;

关键代码解释及实现见流程图及注释;

三、测试分析

运行程序,打开文件:

加载窗口:

移动滑块条,显示正常:

双击列表框,跳转正常:

缩放窗口,绘图正常:

点击菜单更改文件,列表框及绘图正常:

附录:

源代码

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

importjava.io.*;

importjava.util.regex.Pattern;

publicclassIndiWordStatistics{

privateGraphicsEnvironmentGE;

privateGraphicsDeviceGD;

privateDisplayModeDM;

publicIndiWordStatistics(){

/*

*获取图形显示环境信息

*/

GE=GraphicsEnvironment.getLocalGraphicsEnvironment();

GD=GE.getDefaultScreenDevice();

DM=GD.getDisplayMode();

/*

*建立主窗体

*/

newWinMain();

}

publicstaticvoidmain(String[]args){

newIndiWordStatistics();

}

publicclassWinMainextendsJFrameimplementsMouseListener,ActionListener{

privatestaticfinallongserialVersionUID=0;

publicListKeyList;//列表框

publicDiaPanelDPanel;//主画板

publicJScrollPanejsp;

publicJScrollBarvsb;

publicJScrollBarhsb;

publicMenuItemmiOpen;//打开文件

publicintcounts[];//单词计数数组

publicintcount=0;//单词数

publicintMaxLength=0;//单词长度最大值

publicintMaxCount=0;//单词计数最大值

publicStringfn;//文件名

publicWinMain(){

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(newBorderLayout(3,3));

setSize(800,480);

/*

*设置窗体居中

*/

setLocation((DM.getWidth()-800)/2,(DM.getHeight()-600)/2);

MenuBarmb=newMenuBar();

setMenuBar(mb);

Menum=newMenu("文件",true);

m.add(miOpen=newMenuItem("打开"));

miOpen.addActionListener(this);

mb.add(m);

KeyList=newList(10,false);

KeyList.addActionListener(this);

if(!

OpenFile())

System.exit(0);

this.setTitle("单词统计:

"+fn);

DPanel=newDiaPanel(this);

/*

*根据单词数等信息设置画板大小

*/

DPanel.setPreferredSize(newDimension((8*MaxLength+12)*count+40,MaxCount+72));

jsp=newJScrollPane(DPanel);

jsp.addMouseListener(this);

jsp.validate();

vsb=jsp.getVerticalScrollBar();

hsb=jsp.getHorizontalScrollBar();

getContentPane().add("Center",jsp);

getContentPane().add("West",KeyList);

setVisible(true);

vsb.setValue(vsb.getMaximum());

}

publicvoidactionPerformed(ActionEvente){

if(e.getSource()==miOpen){

/*

*更换当前文件

*/

if(OpenFile()){

setTitle("单词统计:

"+fn);

DPanel.GWidth=8*MaxLength+12;

DPanel.setPreferredSize(newDimension((8*MaxLength+12)*count+40,MaxCount+72));

DPanel.repaint();

vsb.setValue(vsb.getMaximum());

hsb.setValue(hsb.getMinimum());

}

}elseif(e.getSource()==KeyList){

/*

*定位到目标单词

*/

hsb.setValue((8*MaxLength+12)*KeyList.getSelectedIndex()+40);

}

}

publicvoidmouseClicked(MouseEvente){

if(e.getSource()==jsp){

DPanel.repaint();

}

}

publicvoidmousePressed(MouseEvente){

}

publicvoidmouseReleased(MouseEvente){

}

publicvoidmouseEntered(MouseEvente){

}

publicvoidmouseExited(MouseEvente){

}

publicbooleanOpenFile(){

/*

*读入文本到字节数组

*/

FileDialogfd=newFileDialog(this,"请选择将要统计的TXT文件:

");

fd.setVisible(true);

if(fd.getDirectory()==null||fd.getFile()==null)

returnfalse;

FileflData=newFile(fd.getDirectory(),fd.getFile());

fn=fd.getFile().toString();

if(fn==null)

returnfalse;

KeyList.removeAll();

count=0;

bytebuffer[]=newbyte[(int)flData.length()];

try{

FileInputStreamfis=newFileInputStream(flData);

try{

fis.read(buffer);

}catch(IOExceptione){

returnfalse;

}

}

catch(FileNotFoundExceptione){

JOptionPane.showMessageDialog(null,"IO错误!

");

returnfalse;

}

/*

*初始化列表框及相关变量

*/

Stringstr=newString(buffer);

Stringwords[]=Ppile("[^a-zA-Z]").split(str);

counts=newint[words.length];

for(StringthisWord:

words){

if(!

thisWord.matches("[a-zA-Z]+"))

continue;

intside=getListSite(thisWord.toLowerCase());

if(side<0){

/*

*获取单词长度最大值

*/

if(thisWord.length()>MaxLength)

MaxLength=thisWord.length();

count++;

KeyList.add(thisWord.toLowerCase());

counts[KeyList.getItemCount()-1]=1;

}else{

counts[side]++;

}

}

/*

*获取单词计数最大值

*/

for(inti:

counts)

if(i>MaxCount)

MaxCount=i;

returntrue;

}

publicintgetListSite(Stringstr){

for(inti=0;i

if(str.equals(KeyList.getItem(i)))

returni;

return-1;

}

}

publicclassDiaPanelextendsJPanel{

privatestaticfinallongserialVersionUID=0;

WinMainWnd;

publicintGWidth;

publicintGHeight;

publicDiaPanel(WinMainwnd){

Wnd=wnd;

GWidth=8*Wnd.MaxLength+12;

GHeight=10;

}

publicvoidpaintComponent(Graphicsg){

intlines;//行数

intlastX=40;//上一个坐标点,用于绘制折线图

intlastY;

Colorca=newColor(74,153,191);

Colorcb=newColor(83,136,51);

Colorcc=newColor(165,22,97);

if(Wnd.MaxCount

lines=(this.getHeight()-72)/10+1;

else

lines=Wnd.MaxCount/10+2;

lastY=lines*10-10;

g.setColor(Color.WHITE);

g.fillRect(0,0,this.getWidth(),this.getHeight());

//绘制底板

g.setColor(Color.LIGHT_GRAY);

/*

*绘制行线

*/

for(inti=1;i

g.drawLine(40,i*10,this.getWidth(),i*10);

}

g.drawLine(0,this.getHeight()-45,this.getWidth(),this.getHeight()-55);

g.setColor(Color.BLACK);

g.drawString("Word:

",0,this.getHeight()-50);

g.drawString("Count:

",0,this.getHeight()-30);

for(inti=0;i

g.setColor(Color.LIGHT_GRAY);

g.drawLine(GWidth*i+40,0,GWidth*i+40,lines*10-10);

//绘制列线

g.setColor(Color.BLACK);

g.drawString(Wnd.KeyList.getItem(i),GWidth*i+45,this.getHeight()-50);

//绘制单词

g.drawString(String.valueOf(Wnd.counts[i]),GWidth*i+45,this.getHeight()-30);

//绘制单词计数

/*

*设置柱状图颜色

*/

switch(i%2){

case0:

g.setColor(ca);

break;

case1:

g.setColor(cb);

break;

}

g.fillRect(GWidth*i+40,lines*10-Wnd.counts[i]-10,12,Wnd.counts[i]);

//绘制柱状图

g.setColor(cc);

g.drawLine(lastX,lastY,GWidth*i+40,lines*10-Wnd.counts[i]-10);

//绘制折线

lastX=GWidth*i+40;

lastY=lines*10-Wnd.counts[i]-10;

}

g.setColor(Color.BLACK);

/*

*绘制刻度

*/

intline=lines/5;

for(inti=line;i>0;i--){

g.drawString(String.valueOf(i*50),0,lines*10-i*50-5);

}

}

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1