Java面向对象程序设计课后答案(7)

2019-03-04 12:45

int score=0;

CheckboxGroup age=new CheckboxGroup(); EWindow()

{ super("英语单词学习"); 分数=new TextField(10);题目=new TextField(70); start=new Button("重新练习"); start.addActionListener(this);

next=new Button("下一题目"); next.addActionListener(this); checkbox=new Checkbox[4]; for(int i=0;i<=3;i++)

{ checkbox[i]=new Checkbox("",false,age); checkbox[i].addItemListener(this); }

try { file=new FileReader("English.txt"); in=new BufferedReader(file); }

catch(IOException e){} setBoun

ds(20,100,660,300); setVisible(true); Box box=Box.createVerticalBox(); Panel p1=new Panel(),p2=new Panel(),

p3=new Panel() ,p4=new Panel(),p5=new Panel(); p1.add(new Label("题目:"));p1.add(题目); p2.add(new Label("选择答案:")); for(int i=0;i<=3;i++) { p2.add(checkbox[i]); }

p3.add(new Label("您的得分:"));p3.add(分数); p4.add(start); p4.add(next);

box.add(p1);box.add(p2);box.add(p3);box.add(p4); addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) { System.exit(0); } });

add(box,BorderLayout.CENTER); reading(); }

public void reading()

{ int i=0; try { s=in.readLine();

if(!(s.startsWith("endend")))

{ StringTokenizer tokenizer=new StringTokenizer(s,"#"); while(tokenizer.hasMoreTokens()) { str[i]=tokenizer.nextToken(); i++; }

题目.setText(str[0]); for(int j=1;j<=4;j++)

{ checkbox[j-1].setLabel(str[j]); } }

else if(s.startsWith("endend")) { 题目.setText("学习完毕"); for(int j=0;j<4;j++)

{ checkbox[j].setLabel("end"); in.close();file.close(); } } }

catch(Exception exp){ 题目.setText("无试题文件") ; } }

public void actionPerformed(ActionEvent event) { if(event.getSource()==start) { score=0;

分数.setText("得分: "+score); try { file=new FileReader("English.txt"); in=new BufferedReader(file); }

catch(IOException e){} reading(); }

if(event.getSource()==next) { reading(); for(int j=0;j<4;j++)

{ checkbox[j].setEnabled(true); } } }

public void itemStateChanged(ItemEvent e) { for(int j=0;j<4;j++)

{ if(checkbox[j].getLabel().equals(str[5])&&checkbox[j].getState()) { score++;

分数.setText("得分: "+score); }

checkbox[j].setEnabled(false); } } }

习题13

1.一个使用链式结构,一个使用顺序结构。 2.8。 3.ABCD。

4.选用HashMap<K,V>来存储。 5.

import java.util.*;

class UFlashKey implements Comparable { double d=0; UFlashKey (double d) { this.d=d; }

public int compareTo(Object b) { UFlashKey st=(UFlashKey)b; if((this.d-st.d)==0) return -1; else retu

rn (int)((this.d-st.d)*1000); } }

class UFlash { String name=null; double capacity,price;

UFlash(String s,double m,double e) { name=s; capacity=m; price=e; } }

public class Xiti5 {

public static void main(String args[ ]) {

TreeMap<UFlashKey,UFlash> treemap= new TreeMap<UFlashKey,UFlash>(); String

str[]={"U1","U2","U3","U4","U5","U6","U7","U8","U9","U10"}; double capacity[]={1,2,2,4,0.5,10,8,4,4,2}; double price[]={30,66,90,56,50,149,120,80,85,65}; UFlash UFlash[]=new UFlash[10]; for(int k=0;k<UFlash.length;k++) {

UFlash[k]=new UFlash(str[k],capacity[k],price[k]);

}

UFlashKey key[]=new UFlashKey[10] ; for(int k=0;k<key.length;k++) {

key[k]=new UFlashKey(UFlash[k].capacity); //关键字按容量成绩排列大小 }

for(int k=0;k<UFlash.length;k++) { treemap.put(key[k],UFlash[k]); }

int number=treemap.size();

System.out.println("树映射中有"+number+"个对象,按容量成绩排序:"); Collection<UFlash> collection=treemap.values(); Iterator<UFlash> iter=collection.iterator(); while(iter.hasNext()) { UFlash stu=iter.next();

System.out.println("U盘 "+stu.name+" 容量 "+stu.capacity); }

treemap.clear();

for(int k=0;k<key.length;k++) {

key[k]=new UFlashKey(UFlash[k].price);//关键字按价格成绩排列大小 }

for(int k=0;k<UFlash.length;k++) { treemap.put(key[k],UFlash[k]); }

number=treemap.size();

System.out.println("树映射中有"+number+"个对象:按价格成绩排序:"); collection=treemap.values(); iter=collection.iterator(); while(iter.hasNext()) { UFlash stu=(UFlash)iter.next();

System.out.println("U盘 "+stu.name+" 价格 "+stu.price); } } }


Java面向对象程序设计课后答案(7).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2018年中国家禽养殖行业分析报告-市场深度分析与投资前景研究(目

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: