《软件设计与体系结构实验指导书2014(共41页).doc》由会员分享,可在线阅读,更多相关《软件设计与体系结构实验指导书2014(共41页).doc(41页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上专心-专注-专业实验一 经典软件体系结构风格(一)实验目的(1)理解管道-过滤器软件体系结构、面向对象软件体系结构的原理(2)掌握管道-过滤器软件体系结构、面向对象软件体系结构的实例(3)管道-过滤器软件体系结构、面向对象软件体系结构的编程实现实验内容1管道-过滤器软件体系结构(1)在dos提示符下输入下面的命令:dir | more使得当前目录列表在屏幕上逐屏显示。dir的输出的是整个目录列表,它不出现在屏幕上而是由于符号“|”的规定,成为下一个命令more的输入,more命令则将其输入一屏一屏地显示,成为命令行的输出。(2)Java I/O流中的管道流类Piped
2、InputStream和PipedOutputStream可以方便地实现管道-过滤器体系结构,这两个类的实例对象要通过connect方法连接。下面程序的功能是sender发送“Hello,receiver! Im sender”给receiver,然后receiver接受后显示出来并且在前面加上“the following is from sender”的信息。管道流内部在实现时还有大量的对同步数据的处理,管道输出流和管道输入流执行时不能互相阻塞,所以一般要开启独立线程分别执行,顺便复习了多线程操作。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 PipedInputStream(); public
5、 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(); 程序的执行结果: the following is from sender:Hello,receiver ! Im sender2数据
6、抽象和面向对象软件体系结构有一个已知的二维坐标系,在坐标系中定义了若干种规则的图形:圆、正方形、矩形和椭圆。使用Java语言进行面向对象的程序设计:(1)首先考虑数据封装性,(2)考虑继承性,(3)考虑抽象类。abstract class Graphprotected double x,y;/ x,y是规则图形的中心点坐标 public Graph(double x,double y)/ 构造函数初始化中心点坐标 this.x=x; this.y=y; protected void changeX(double x)/ 修改横坐标 this.x=x; protected void change
7、Y(double y)/ 修改纵坐标 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; public double area() r
8、eturn 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; class MyRectangle ext
9、ends 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; class MyEllipse extends
10、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 static void main (Stri
11、ng 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的面积是+r.area(); System.out.println(椭圆e的面积是+e.area(); 该程序
12、的运行结果为: 圆c的面积是28.2744正方形s的面积是16.0矩形r的面积是2.0椭圆e的面积是18.8496思考与提高1、管道-过滤器软件体系结构与批处理软件体系结构的区别和联系是什么?2、面向对象软件体系结构与主程序-子程序软件体系结构的区别和联系是什么?实验二 经典软件体系结构风格(二)实验目的(1)理解基于事件的隐式调用软件体系结构、层次软件体系结构的原理(2)掌握事件的隐式调用软件体系结构、层次软件体系结构的实例(3)事件的隐式调用软件体系结构、层次软件体系结构的编程实现实现内容1基于事件的隐式调用风格常用控制组件的事件按钮与动作事件(ActionEvent),参见下例。按钮与动
13、作事件运行结果import java.awt.*;import java.awt.event.*; /引入java.awt.event包处理事件class BtnLabelAction extends Frame implements ActionListener/声明窗口类(BtnLabelAction)并实现动作事件接口(ActionListener)Label prompt;Button btn;void CreateWindow() /自定义方法setTitle(MyButton);prompt = new Label(你好);/创建标签对象btn = new Button(操作);/
14、创建按钮对象setLayout(new FlowLayout();/布局设计,用于安排按钮、标签的位置add(prompt);/将标签放入容器add(btn);/将按钮放入容器btn.addActionListener(this);/将监听器(窗体对象本身)注册给按钮对象setSize(300,100);setVisible(true);public void actionPerformed(ActionEvent e)/接口ActionListener的事件处理方法if(e.getSource()=btn) /判断动作事件是否是由按钮btn引发的if(prompt.getText()=你好)
15、prompt.setText(再见);elseprompt.setText(你好); public class BtnTestpublic static void main (String args)BtnLabelAction bla=new BtnLabelAction();bla.CreateWindow(); 2层次软件体系结构基于层次软件体系结构的软件测试系统。第一层为用户图形界面层import java.awt.*;import java.util.*;import javax.swing.*;import java.awt.event.*;import com.sun.java.
16、swing.plaf.windows.*;public class TestingGUI extends JPanel private JTextArea txtTestInfo, txtTestcase; private JLabel lblTestcases; private JPanel buttonPanel; private JComboBox cmbTestcases; private static final String CASE_BUBBLE= TC1-Test Bubble Sort; private static final String CASE_HEAP= TC2-T
17、est Heap Sort; private static final String CASE_INSERTION= TC3-Test Insertion Sort; private static final String EXECUTE = Execute; private static final String EXIT = Exit; public TestingGUI() txtTestInfo=new JTextArea(Test output from source shown heren, 6, 20); txtTestInfo.setLineWrap(true); txtTes
18、tcase = new JTextArea(Testcase info and test validation shown heren, 4, 15); txtTestcase.setLineWrap(true); buildUpScrollGUI(); private void buildUpScrollGUI() setUpButtonPanel(); JScrollPane btnPane = new JScrollPane(buttonPanel); JScrollPane textPane = new JScrollPane(txtTestcase); textPane.setMin
19、imumSize(new Dimension(250, 150); JScrollPane testDataPane = new JScrollPane(txtTestInfo); JSplitPane upSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); upSplitPane.setLeftComponent(btnPane); upSplitPane.setRightComponent(testDataPane); JScrollPane downPane = new JScrollPane(textPane); Dimen
20、sion minimumSize = new Dimension(130, 100); btnPane.setMinimumSize(minimumSize); textPane.setMinimumSize(new Dimension(100, 100); upSplitPane.setDividerLocation(270); upSplitPane.setPreferredSize(new Dimension(500, 300); JSplitPane bigSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upSplitPane
21、, downPane); bigSplitPane.setDividerLocation(190); add(bigSplitPane); setSize(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); cmbTest
22、cases.addItem(CASE_INSERTION); /Create the open button JButton executeBtn = 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.a
23、ddActionListener(objButtonHandler); exitButton.addActionListener(objButtonHandler); buttonPanel = new JPanel(); GridBagLayout gridbag = new GridBagLayout(); buttonPanel.setLayout(gridbag); GridBagConstraints gbc = new GridBagConstraints(); buttonPanel.add(lblTestcases); buttonPanel.add(cmbTestcases)
24、; buttonPanel.add(executeBtn); buttonPanel.add(exitButton); gbc.insets.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.g
25、ridx = 1; gbc.gridy = 0; gridbag.setConstraints(cmbTestcases, gbc); 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 = GridBagC
26、onstraints.WEST; gbc.gridx = 1; gbc.gridy = 7; gridbag.setConstraints(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 getSelectedTestcas
27、e() return (String) 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.
28、getActionCommand().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(); outp
29、ut = test.execute(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 class BtnListener private st
30、atic void createAndShowGUI() JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame(Layered Architecture- Software Testing); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); TestingGUI newContentPane = new TestingGUI(); newContentPane.setOpaque(true); frame.setContentPane(newCon
31、tentPane); 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 isResultCorrect(int arr) for(int k=0;
32、 k 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 SortAlgorithm alg; / Constructor public Context(SortAlgorithm alg) this.alg = alg; p
33、ublic 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= 0;) for(int j = 0; j numsj + 1) /exchange numsj+1 with numsj int T = numsj; numsj = numsj + 1; numsj + 1 = T; return nums; public class HeapSort implements SortAlgorithm public int sort(int nums ) for(int i=nums.length; i1; i-) buildBinaryHeapTree(nums, i - 1); swapLeadingNodeWithLastNode(nums, i - 1); return nu
限制150内