版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)實驗一 經(jīng)典軟件體系結(jié)構(gòu)風(fēng)格(一)實驗?zāi)康模?)理解管道-過濾器軟件體系結(jié)構(gòu)、面向?qū)ο筌浖w系結(jié)構(gòu)的原理(2)掌握管道-過濾器軟件體系結(jié)構(gòu)、面向?qū)ο筌浖w系結(jié)構(gòu)的實例(3)管道-過濾器軟件體系結(jié)構(gòu)、面向?qū)ο筌浖w系結(jié)構(gòu)的編程實現(xiàn)實驗內(nèi)容1管道-過濾器軟件體系結(jié)構(gòu)(1)在dos提示符下輸入下面的命令:dir | more使得當(dāng)前目錄列表在屏幕上逐屏顯示。dir的輸出的是整個目錄列表,它不出現(xiàn)在屏幕上而是由于符號“|”的規(guī)定,成為下一個命令more的輸入,more命令則將其輸入一屏一屏地顯示,成為命令行的輸出。(2)Java I/O流中的管道流類Piped
2、InputStream和PipedOutputStream可以方便地實現(xiàn)管道-過濾器體系結(jié)構(gòu),這兩個類的實例對象要通過connect方法連接。下面程序的功能是sender發(fā)送“Hello,receiver! Im sender”給receiver,然后receiver接受后顯示出來并且在前面加上“the following is from sender”的信息。管道流內(nèi)部在實現(xiàn)時還有大量的對同步數(shù)據(jù)的處理,管道輸出流和管道輸入流執(zhí)行時不能互相阻塞,所以一般要開啟獨立線程分別執(zhí)行,順便復(fù)習(xí)了多線程操作。import java.io.*;import java.util.*;public clas
3、s TestPipedpublic static void main(String args)sender s = new sender();receiver r = new receiver(); PipedOutputStream out = s.getOut(); PipedInputStream in = r.getIn(); try in.connect(out); s.start(); r.start(); catch(Exception e) e.printStackTrace(); class sender extends Thread PipedOutputStream ou
4、t = new PipedOutputStream(); public PipedOutputStream getOut() return out; public void run() String str = "Hello,receiver ! Im sendern" try out.write(str.getBytes(); out.close(); catch(Exception e) e.printStackTrace(); class receiver extends Thread PipedInputStream in = new PipedInputStrea
5、m(); public PipedInputStream getIn() return in; public void run() byte buf = new byte1024; try int len = in.read(buf); System.out.println("the following is from sender:n"+new String(buf,0,len); in.close(); catch(Exception e) e.printStackTrace(); 程序的執(zhí)行結(jié)果: the following is from sender:Hello,
6、receiver ! Im sender2數(shù)據(jù)抽象和面向?qū)ο筌浖w系結(jié)構(gòu)有一個已知的二維坐標(biāo)系,在坐標(biāo)系中定義了若干種規(guī)則的圖形:圓、正方形、矩形和橢圓。使用Java語言進行面向?qū)ο蟮某绦蛟O(shè)計:(1)首先考慮數(shù)據(jù)封裝性,(2)考慮繼承性,(3)考慮抽象類。abstract class Graphprotected double x,y;/ x,y是規(guī)則圖形的中心點坐標(biāo) public Graph(double x,double y)/ 構(gòu)造函數(shù)初始化中心點坐標(biāo) this.x=x; this.y=y; protected void changeX(double x)/ 修改橫坐標(biāo) this.x=x
7、; protected void changeY(double y)/ 修改縱坐標(biāo) this.y=y; public abstract double area();/ 計算面積的抽象方法class MySquare extends Graph private double length; public MySquare(double x,double y,double length) super(x,y); this.length=length; protected void changLength(double length) / 修改邊長length this.length=length;
8、 public double area() return length*length; class MyCircle extends Graph private double radius; public MyCircle(double x,double y,double radius) super(x,y); this.radius=radius; protected void changRadius(double radius) / 修改半徑radius this.radius=radius; public double area() return 3.1416*radius*radius
9、; class MyRectangle extends Graph private double a,b; public MyRectangle(double x,double y,double a,double b) super(x,y); this.a=a; this.b=b; protected void changLength(double length) / 修改長length a=length; protected void changWidth(double width) / 修改寬width b=width; public double area() return a*b; c
10、lass MyEllipse extends Graph private double a,b; public MyEllipse (double x,double y,double a,double b) super(x,y); this.a=a; this.b=b; protected void changA(double a) / 修改長軸a this.a=a; protected void changB(double b) / 修改短軸b this.b=b; public double area() return 3.1416*a*b; public class Area public
11、 static void main (String arg)MyCircle c=new MyCircle(1,1,3); MySquare s=new MySquare(2,2,4); MyRectangle r=new MyRectangle(12,9,1,2); MyEllipse e=new MyEllipse(2,-1,3,2); System.out.println("圓c的面積是"+c.area(); System.out.println("正方形s的面積是"+s.area(); System.out.println("矩形r的面
12、積是"+r.area(); System.out.println("橢圓e的面積是"+e.area(); 該程序的運行結(jié)果為: 圓c的面積是28.2744正方形s的面積是16.0矩形r的面積是2.0橢圓e的面積是18.8496思考與提高1、管道-過濾器軟件體系結(jié)構(gòu)與批處理軟件體系結(jié)構(gòu)的區(qū)別和聯(lián)系是什么?2、面向?qū)ο筌浖w系結(jié)構(gòu)與主程序-子程序軟件體系結(jié)構(gòu)的區(qū)別和聯(lián)系是什么?實驗二 經(jīng)典軟件體系結(jié)構(gòu)風(fēng)格(二)實驗?zāi)康模?)理解基于事件的隱式調(diào)用軟件體系結(jié)構(gòu)、層次軟件體系結(jié)構(gòu)的原理(2)掌握事件的隱式調(diào)用軟件體系結(jié)構(gòu)、層次軟件體系結(jié)構(gòu)的實例(3)事件的隱式調(diào)用軟件體系
13、結(jié)構(gòu)、層次軟件體系結(jié)構(gòu)的編程實現(xiàn)實現(xiàn)內(nèi)容1基于事件的隱式調(diào)用風(fēng)格常用控制組件的事件按鈕與動作事件(ActionEvent),參見下例。按鈕與動作事件運行結(jié)果import java.awt.*;import java.awt.event.*; /引入java.awt.event包處理事件class BtnLabelAction extends Frame implements ActionListener/聲明窗口類(BtnLabelAction)并實現(xiàn)動作事件接口(ActionListener)Label prompt;Button btn;void CreateWindow() /自定義方法
14、setTitle("MyButton");prompt = new Label("你好");/創(chuàng)建標(biāo)簽對象btn = new Button("操作");/創(chuàng)建按鈕對象setLayout(new FlowLayout();/布局設(shè)計,用于安排按鈕、標(biāo)簽的位置add(prompt);/將標(biāo)簽放入容器add(btn);/將按鈕放入容器btn.addActionListener(this);/將監(jiān)聽器(窗體對象本身)注冊給按鈕對象setSize(300,100);setVisible(true);public void actionPerfo
15、rmed(ActionEvent e)/接口ActionListener的事件處理方法if(e.getSource()=btn) /判斷動作事件是否是由按鈕btn引發(fā)的if(prompt.getText()="你好")prompt.setText("再見");elseprompt.setText("你好"); public class BtnTestpublic static void main (String args)BtnLabelAction bla=new BtnLabelAction();bla.CreateWindow(
16、); 2層次軟件體系結(jié)構(gòu)基于層次軟件體系結(jié)構(gòu)的軟件測試系統(tǒng)。第一層為用戶圖形界面層import java.awt.*;import java.util.*;import javax.swing.*;import java.awt.event.*;import com.sun.java.swing.plaf.windows.*;public class TestingGUI extends JPanel private JTextArea txtTestInfo, txtTestcase; private JLabel lblTestcases; private JPanel buttonPan
17、el; private JComboBox cmbTestcases; private static final String CASE_BUBBLE= "TC1-Test Bubble Sort" private static final String CASE_HEAP= "TC2-Test Heap Sort" private static final String CASE_INSERTION= "TC3-Test Insertion Sort" private static final String EXECUTE = &q
18、uot;Execute" private static final String EXIT = "Exit" public TestingGUI() txtTestInfo=new JTextArea("Test output from source shown heren", 6, 20); txtTestInfo.setLineWrap(true); txtTestcase = new JTextArea("Testcase info and test validation shown heren", 4, 15); t
19、xtTestcase.setLineWrap(true); buildUpScrollGUI(); private void buildUpScrollGUI() setUpButtonPanel(); JScrollPane btnPane = new JScrollPane(buttonPanel); JScrollPane textPane = new JScrollPane(txtTestcase); textPane.setMinimumSize(new Dimension(250, 150); JScrollPane testDataPane = new JScrollPane(t
20、xtTestInfo); JSplitPane upSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); upSplitPane.setLeftComponent(btnPane); upSplitPane.setRightComponent(testDataPane); JScrollPane downPane = new JScrollPane(textPane); Dimension minimumSize = new Dimension(130, 100); btnPane.setMinimumSize(minimumSize
21、); textPane.setMinimumSize(new Dimension(100, 100); upSplitPane.setDividerLocation(270); upSplitPane.setPreferredSize(new Dimension(500, 300); JSplitPane bigSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upSplitPane, downPane); bigSplitPane.setDividerLocation(190); add(bigSplitPane); setSize(
22、new Dimension(500, 400); setVisible(true); private void setUpButtonPanel() lblTestcases = new JLabel("Test Cases:"); cmbTestcases = new JComboBox(); cmbTestcases.addItem(CASE_BUBBLE); cmbTestcases.addItem(CASE_HEAP); cmbTestcases.addItem(CASE_INSERTION); /Create the open button JButton exe
23、cuteBtn = new JButton(EXECUTE); executeBtn.setMnemonic(KeyEvent.VK_S); JButton exitButton = new JButton(EXIT); exitButton.setMnemonic(KeyEvent.VK_X); BtnListener objButtonHandler = new BtnListener(); / add action Listener executeBtn.addActionListener(objButtonHandler); exitButton.addActionListener(o
24、bjButtonHandler); buttonPanel = new JPanel(); GridBagLayout gridbag = new GridBagLayout(); buttonPanel.setLayout(gridbag); GridBagConstraints gbc = new GridBagConstraints(); buttonPanel.add(lblTestcases); buttonPanel.add(cmbTestcases); buttonPanel.add(executeBtn); buttonPanel.add(exitButton); gbc.in
25、sets.top = 5; gbc.insets.bottom = 5; gbc.insets.left = 5; gbc.insets.right = 5; gbc.anchor = GridBagConstraints.EAST; gbc.gridx = 0; gbc.gridy = 0; gridbag.setConstraints(lblTestcases, gbc); gbc.anchor = GridBagConstraints.WEST; gbc.gridx = 1; gbc.gridy = 0; gridbag.setConstraints(cmbTestcases, gbc)
26、; gbc.anchor = GridBagConstraints.EAST; gbc.insets.left = 2; gbc.insets.right = 2; gbc.insets.top = 25; gbc.anchor = GridBagConstraints.EAST; gbc.gridx = 0; gbc.gridy = 7; gridbag.setConstraints(executeBtn, gbc); gbc.anchor = GridBagConstraints.WEST; gbc.gridx = 1; gbc.gridy = 7; gridbag.setConstrai
27、nts(exitButton, gbc); public void showTestInfo(int str ) txtTestInfo.setText(""); for(int n=0; n< str.length; n+) txtTestInfo.append(""+strn+" "); public void showErrors(String err) txtTestcase.append(err+"n"); public String getSelectedTestcase() return (St
28、ring) cmbTestcases.getSelectedItem(); class BtnListener implements ActionListener private Testcase test; private String selectedTestcase; public void actionPerformed(ActionEvent e) String searchResult = null; int output=null; if (e.getActionCommand().equals(EXIT) System.exit(1); if (e.getActionComma
29、nd().equals(EXECUTE) selectedTestcase = getSelectedTestcase(); if(selectedTestcase.equals(CASE_BUBBLE) test = new TestcaseBubble(); else if(selectedTestcase.equals(CASE_HEAP) test = new TestcaseHeap(); else if(selectedTestcase.equals(CASE_INSERTION) test = new TestcaseInsertion(); output = test.exec
30、ute(3000); showTestInfo(output); showErrors(selectedTestcase); boolean result = ResultVerification.isResultCorrect(output ); showErrors("No Error found = " +result); long timeTaken = test.getTimeTaken(); showErrors("Testing Time takes = " + timeTaken+"n"); / End of clas
31、s BtnListener private static void createAndShowGUI() JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Layered Architecture- Software Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); TestingGUI newContentPane = new TestingGUI(); newContentPane.setOpaqu
32、e(true); frame.setContentPane(newContentPane); frame.pack(); frame.setVisible(true); static public void main(String argv) javax.swing.SwingUtilities.invokeLater(new Runnable() public void run() createAndShowGUI(); ); public class ResultVerification static boolean flag = true; public static boolean i
33、sResultCorrect(int arr) for(int k=0; k<arr.length-1; k+) if(arrk > arrk+1) flag=false; System.out.println("error "+ k); /break; return flag; 第二層為測試案例層,包括軟件測試工程師所編寫的測試案例public interface Testcase public abstract int execute(int len); public abstract long getTimeTaken();class Context So
34、rtAlgorithm alg; / Constructor public Context(SortAlgorithm alg) this.alg = alg; public int sortIntArray(int a) return this.alg.sort(a); import java.util.Random;public class IntegerArrGenerator public static int generateInput(int len) int input= new intlen; Random r = new Random(); for(int m=0; m<
35、; len; m+) inputm = r.nextInt(len); return input; import java.util.*;public class TestcaseBubble implements Testcase private long startTime; private long timeTaken=0; public int execute(int len) startTime = System.currentTimeMillis(); int input = IntegerArrGenerator.generateInput(len); SortAlgorithm
36、 sa = new BubbleSort(); Context context = new Context(sa); int intArray = context.sortIntArray(input); timeTaken = System.currentTimeMillis() - startTime; return intArray; public long getTimeTaken() return timeTaken;import java.util.*;public class TestcaseHeap implements Testcase /static long time;
37、private long startTime; private long timeTaken=0; public int execute(int len)startTime = System.currentTimeMillis();int input = IntegerArrGenerator.generateInput(len);SortAlgorithm sa = new HeapSort();Context context = new Context(sa);int intArray = context.sortIntArray(input);timeTaken = System.cur
38、rentTimeMillis()-startTime; return intArray; public long getTimeTaken() return timeTaken;import java.util.*;public class TestcaseInsertion implements Testcase private long startTime; private long timeTaken=0; public int execute(int len) startTime = System.currentTimeMillis(); int input = IntegerArrG
39、enerator.generateInput(len); SortAlgorithm sa = new InsertSort(); Context context = new Context(sa); int intArray = context.sortIntArray(input); timeTaken = System.currentTimeMillis()-startTime; return intArray; public long getTimeTaken() return timeTaken;第三層為被測試軟件層(排序算法)public interface SortAlgorit
40、hm int sort(int nums);public class BubbleSort implements SortAlgorithm public int sort(int nums) for(int i = nums.length; -i >= 0;) for(int j = 0; j < i; j+) if(numsj > numsj + 1) /exchange numsj+1 with numsj int T = numsj; numsj = numsj + 1; numsj + 1 = T; return nums; public class HeapSor
41、t implements SortAlgorithm public int sort(int nums ) for(int i=nums.length; i>1; i-) buildBinaryHeapTree(nums, i - 1); swapLeadingNodeWithLastNode(nums, i - 1); return nums; public void buildBinaryHeapTree(int array, int arrayBound) int leftChild, rightChild, biggerChild, temp; int root = (array
42、Bound-1)/2; / Find the bigger child index for(int i=root; i>=0; i-) leftChild = (2*i)+1; rightChild = (2*i)+2; if(leftChild <= arrayBound) && (rightChild <= arrayBound) if(arrayrightChild >= arrayleftChild) biggerChild = rightChild; else biggerChild = leftChild; else if(rightChil
43、d > arrayBound) biggerChild = leftChild; else biggerChild = rightChild; /swap the integer contained in the bigger child index /with that in the current parent node if(arrayi < arraybiggerChild) temp = arrayi; arrayi = arraybiggerChild; arraybiggerChild = temp; return;public static void swapLea
44、dingNodeWithLastNode(int array, int arrayBound) int temp; temp = array0; array0 = arrayarrayBound; arrayarrayBound = temp; return;public class InsertSort implements SortAlgorithm public int sort(int nums) for (int i = 1; i < nums.length; i+) int j = i; int numToBeInserted = numsi; while (j > 0
45、) && (numsj-1 > numToBeInserted) ) numsj = numsj-1; j-; numsj = numToBeInserted; return nums; 實驗三 分布式軟件體系結(jié)構(gòu)風(fēng)格實驗?zāi)康模?)理解分布式軟件體系結(jié)構(gòu)風(fēng)格的原理(2)掌握分布式軟件體系結(jié)構(gòu)風(fēng)格的實例(3)常見分布式軟件體系結(jié)構(gòu)風(fēng)格的編程實現(xiàn)實驗內(nèi)容C/S體系結(jié)構(gòu)風(fēng)格客戶機發(fā)送數(shù)據(jù)到服務(wù)器,服務(wù)器將收到的數(shù)據(jù)返回給客戶機,直到接收到字符串“end”為止,最后關(guān)閉連接。(1)服務(wù)器端程序tcpServer.javaimport java.io.*;import .*;pub
46、lic class tcpServer public static final int PORT=8888; public static void main(String args) throws IOException /建立ServerSocket ServerSocket s=new ServerSocket(PORT); System.out.println("ServerSocket:"+s); try /*程序阻塞,等待連接。即直到有一個客戶請求到達,程序方能繼續(xù)執(zhí)行*/ Socket ss=s.accept(); System.out.println(&quo
47、t;Socket accept:"+ss); try /連接成功,建立相應(yīng)的I/O數(shù)據(jù)流 DataInputStream dis=new DataInputStream(ss.getInputStream(); DataOutputStream dos=new DataOutputStream(ss.getOutputStream(); /在循環(huán)中,與客戶機通信 while(true) String str=dis.readUTF();/從客戶機中讀數(shù)據(jù) if(str.equals("end")break;/當(dāng)讀到end時,程序終止 System.out.println(str); dos.writeUTF("Echoing:"+str);/向客戶機中寫數(shù)據(jù) dos.close(); dis.close(); finally ss.close(); finally s.close(); 服務(wù)器端運行結(jié)果為: ServerSocket:ServerSocketaddr=0.0.0.0/0.0.0.0,port=0,localport=8888Socket accept:Sock
溫馨提示
- 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2026學(xué)年陜西省西安市新城區(qū)九年級(上)期末數(shù)學(xué)試卷(含答案)
- 【寒假復(fù)習(xí)】北師大版五年級數(shù)學(xué)上冊應(yīng)用題(含答案)
- 化工企業(yè)培訓(xùn)課件教學(xué)
- 12月轉(zhuǎn)債月報:轉(zhuǎn)債|跨年行情如何配置
- (一模)南通市2026屆高三學(xué)業(yè)質(zhì)量監(jiān)測語文試卷(含標(biāo)準(zhǔn)答案)
- 2026山東臨沂市市直部分事業(yè)單位招聘綜合類崗位21人參考考試題庫及答案解析
- 2026福建福州市馬尾區(qū)行政服務(wù)中心管委會第一批招聘編外人員1人筆試參考題庫及答案解析
- 元旦活動策劃方案地產(chǎn)(3篇)
- 2026貴州遵義融媒傳媒(集團)有限公司招聘19人備考考試試題及答案解析
- 讀詩錄音活動策劃方案(3篇)
- 設(shè)備管理人員19年述職
- 2025年黑龍江農(nóng)墾職業(yè)學(xué)院單招職業(yè)傾向性測試題庫附答案
- 《外科手術(shù)學(xué)基礎(chǔ)》課件
- 拖欠工程款上訪信范文
- 語文-安徽省皖南八校2025屆高三上學(xué)期12月第二次大聯(lián)考試題和答案
- 制造業(yè)工業(yè)自動化生產(chǎn)線方案
- 《傳播學(xué)概論(第四版)》全套教學(xué)課件
- (正式版)JB∕T 7052-2024 六氟化硫高壓電氣設(shè)備用橡膠密封件 技術(shù)規(guī)范
- 單位車輛委托處理協(xié)議書
- 2024工傷免責(zé)承諾書
- 企業(yè)人才發(fā)展方案
評論
0/150
提交評論