JAVA上機(jī)1參考代碼_第1頁
JAVA上機(jī)1參考代碼_第2頁
JAVA上機(jī)1參考代碼_第3頁
JAVA上機(jī)1參考代碼_第4頁
JAVA上機(jī)1參考代碼_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、上機(jī)實踐1 數(shù)據(jù)結(jié)構(gòu)實驗1 掃雷小游戲1答案:【代碼1】: new LinkedList();【代碼2】: list.add(blockij) ; 【代碼3】: list.size(); 【代碼4】: (Block)list.get(randomIndex);【代碼5】: list.remove(randomIndex); 2模板代碼 Block.javapublic class Block String name; int number; boolean boo=false; public void setName(String name) =name; public vo

2、id setNumber(int n) number=n; public int getNumber() return number; public String getName() return name; boolean isMine() return boo; public void setIsMine(boolean boo) this.boo=boo; LayMines.javaimport java.util.LinkedList;public class LayMines public void layMinesForBlock(Block block,int mineCount

3、) int row=block.length; int column=block0.length; LinkedList list=【代碼1】 /創(chuàng)建空鏈表list for(int i=0;irow;i+) for(int j=0;j0) int size=【代碼3】 / list返回節(jié)點(diǎn)的個數(shù) int randomIndex=(int)(Math.random()*size); Block b=【代碼4】 / list返回索引為randomIndex的節(jié)點(diǎn)中的數(shù)據(jù) b.setName(雷); b.setIsMine(true); 【代碼5】 /list刪除索引值為randomIndex的節(jié)點(diǎn)

4、 mineCount-; for(int i=0;irow;i+) for(int j=0;jcolumn;j+) if(blockij.isMine() else int mineNumber=0; for(int k=Math.max(i-1,0);k=Math.min(i+1,row-1);k+) for(int t=Math.max(j-1,0);t=Math.min(j+1,column-1);t+) if(blockkt.isMine() mineNumber+; blockij.setName(+mineNumber); blockij.setNumber(mineNumber)

5、; BlockView.javaimport java.awt.*;public class BlockView extends Panel Label blockName; Button blockCover; CardLayout card; BlockView() card=new CardLayout(); setLayout(card); blockName=new Label(); blockCover=new Button(); add(cover,blockCover); add(name,blockName); public void setName(String name)

6、 blockName.setText(name); public String getName() return blockName.getText(); public void seeBlockName() card.show(this,name); validate(); public void seeBlockCover() card.show(this,cover); validate(); public Button getBlockCover() return blockCover; MineFrame.javaimport java.awt.*;import java.awt.e

7、vent.*;public class MineFrame extends Frame implements ActionListener Button reStart; Block block; BlockView blockView; LayMines lay; int row=10,colum=12,mineCount=22; int colorSwitch=0; Panel pCenter,pNorth; public MineFrame() reStart=new Button(重新開始); pCenter=new Panel(); pNorth=new Panel(); pNort

8、h.setBackground(Color.cyan); block=new Blockrowcolum; for(int i=0;irow;i+) for(int j=0;jcolum;j+) blockij=new Block(); lay=new LayMines(); lay.layMinesForBlock(block,mineCount); blockView=new BlockViewrowcolum; pCenter.setLayout(new GridLayout(row,colum); for(int i=0;irow;i+) for(int j=0;jcolum;j+)

9、blockViewij=new BlockView(); blockViewij.setName(blockij.getName(); pCenter.add(blockViewij); blockViewij.getBlockCover().addActionListener(this); reStart.addActionListener(this); pNorth.add(reStart); add(pNorth,BorderLayout.NORTH); add(pCenter,BorderLayout.CENTER); setSize(200,232); setVisible(true

10、); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); validate(); public void actionPerformed(ActionEvent e) Button source=(Button)e.getSource(); if(source!=reStart) int m=-1,n=-1; for(int i=0;irow;i+) for(int j=0;jcolum;j+) if(source=blockViewij.getBlo

11、ckCover() m=i; n=j; break; if(blockmn.isMine() for(int i=0;irow;i+) for(int j=0;j0) blockViewmn.seeBlockName(); else if(blockmn.getNumber()=0) for(int k=Math.max(m-1,0);k=Math.min(m+1,row-1);k+) for(int t=Math.max(n-1,0);t=Math.min(n+1,colum-1);t+) blockViewkt.seeBlockName(); if(source=reStart) for(

12、int i=0;irow;i+) for(int j=0;jcolum;j+) blockij.setIsMine(false); lay.layMinesForBlock(block,mineCount); for(int i=0;irow;i+) for(int j=0;jcolum;j+) blockViewij.setName(blockij.getName(); blockViewij.seeBlockCover(); blockViewij.getBlockCover().addActionListener(this); MineExample.javapublic class L

13、ayMineMainClass public static void main(String args) new MineFrame(); 實驗2 排序與查找2模板代碼 Book.javapublic class Book implements Comparable double price; String name; public void setPrice(double c) price=c; public double getPrice() return price; public void setName(String n) name=n; public String getName(

14、) return name; public int compareTo(Object object) Book bk=(Book)object; int difference=(int)(this.getPrice()-bk.getPrice()*10000); return difference; SortSearchMainClass.javaimport java.util.*;public class SortSearchMainClass public static void main(String args) LinkedList bookList=new LinkedList()

15、; String bookName=Java編程,XML基礎(chǔ),JSP基礎(chǔ),C+基礎(chǔ),J2ME編程,操作系統(tǒng),數(shù)據(jù)庫技術(shù); double bookPrice=29,21,22,29,34,32,29; Book book=new BookbookName.length; for(int k=0;k=0) Book bk=(Book)bookList.get(m); System.out.println( +bk.getName()+(+bk.getPrice()+); bookList.remove(m); System.out.println(價錢相同); 實驗3 使用TreeSet排序1答案

16、:【代碼1】: new TreeSet();【代碼2】: treeSet.add(stu); 【代碼3】: tree.iterator(); 【代碼4】: te.hasNext()【代碼5】: (Student)te.next(); 2模板代碼 Student.javapublic class Student implements Comparable String name; int score; Student(String name,int score) =name; this.score=score; public int compareTo(Object b) St

17、udent st=(Student)b; int m=this.score-st.score; if(m!=0) return m; else return 1; public int getScore() return score; public String getName() return name; StudentFrame.javaimport java.awt.*;import java.awt.event.*;import java.util.*;public class StudentFrame extends Frame implements ActionListener T

18、extArea showArea; TextField inputName,inputScore; Button button; TreeSet treeSet; StudentFrame() treeSet=【代碼1】 /使用無參數(shù)構(gòu)造方法創(chuàng)建treeSet showArea=new TextArea(); showArea.setFont(new Font(,Font.BOLD,20); inputName=new TextField(5); inputScore=new TextField(5); button=new Button(確定); button.addActionListen

19、er(this); Panel pNorth=new Panel(); pNorth.add(new Label(Name:); pNorth.add(inputName); pNorth.add(new Label(Score:); pNorth.add(inputScore); pNorth.add(button); add(pNorth,BorderLayout.NORTH); add(showArea,BorderLayout.CENTER); setSize(300,320); setVisible(true); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論