zoukankan      html  css  js  c++  java
  • vi, Java, Ant, Junit自学报告

    vi, Java, Ant, Junit自学报告

    2017软件工程实训 15331023 陈康怡

    vi

    Vi是linux系统的标准文本编辑器,采用指令的方式进行操作,此处仅记录部分常用的指令。

    vi模式

    vi编辑器分为三种模式:

    (1)命令行模式 command mode

    命令行模式可控制光标移动,删除字符、字、行,复制剪切等操作。在命令行模式下按i进入插入模式,按:进入底行模式。

    (2)插入模式 insert mode

    在插入模式下,可以进行文字输入。在插入模式下,按Esc回到命令行模式。

    (3)底行模式 last lint mode

    主要进行保存退出的操作,也可以设置编辑环境。

    vi基本操作

    (1)打开文件

    在Terminal中使用$ vi filename 即可用vi打开文件,若文件不存在,vi会自己创建新文件。

    (2)退出vi

    在底行模式中,输入:

    :w filename ——将文本以文件名filename保存

    :wq ——保存并退出vi

    :q! ——不保存强制退出vi

    命令行模式常用操作

    (1)移动光标:

    使用键盘上的h,j,k,l来分别控制光标向左,下,上,右移动。

    (2)删除(可在操作前加数字表示删除多个):

    x ——删除光标后的一个字符

    X ——删除光标前的一个字符

    dd ——删除光标所在行

    (3)撤销:

    u ——撤销上一次操作

    底行模式常用操作

    (1)列出行号 —— :set nu

    (2)跳到某一行 —— :#

    (3)保存 —— :w

    Java

    本周主要学习了Java下简单GUI程序的编写。Java下GUI程序编写主要使用了java.awt以及jawax.swing的库。

    java的GUI程序的基本思路是以JFrame对象为基础,加入不同的控件如JButton,JTextField等控件实现不同的交互功能及布局,再用java代码实现功能逻辑。

    小任务:实现简单计算器

    这是我在这周实训中写的简单计算器中的一些代码片段。这里结合代码记录学习所得。完整代码见学习记录末尾。

    private JFrame myframe = new JFrame("Calculator");
    private JButton plusButton = new JButton("+");
    private JTextField equalText = new JTextField("=");
    

    这一段是声明计算器中的框架以及控件。首先JFrame myframe = new JFrame("Calculator");声明了一个JFrame对象。JFrame对象是Java GUI编程中的基础。它生成了一个程序窗体,窗体名称叫Calculator,之后你可以往JFrame中添加控件。接下来声明了JButton对象和JTextField对象,用于生成计算器上不同操作的按钮,以及输入操作数、输出结果的文本框对象。

    //省略了部分功能相似代码
    protected void generateFrame() {
    	//设置窗体大小
    	myframe.setSize(500,200);
    	
    	//设置窗体布局
    	myframe.setLayout(new GridLayout(0,5));
    	
    	//设置控件属性
    	equalText.setEditable(false);
    	equalText.setHorizontalAlignment(SwingConstants.CENTER);
    	
    	//将控件插入窗体
    	myframe.add(operand1);
    
    	//将窗体设置为可见,且关闭窗体时退出进程
    	myframe.setVisible(true);
    	myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    

    声明完以后,我们就可以在generateFrame函数中对窗体进行进一步的操作。代码的功能具体见注释。

    这里有意思的地方主要有设置窗体布局和设置控件属性。Java Swing提供了多种布局方法:

    (1)FlowLayout:将控件逐个添加到窗口中,从左往右一字排开,直至没有空间剩余才换到下一行。

    (2)BorderLayout:将界面分成5个区域,分别用BorderLayout的常量指定:

    1.-PAGE_START

    2.-PAGE_END

    3.-LINE_START

    4.-LINE_END

    5.-CENTER

    (3)BoxLayout:可以控制控件水平或竖直地插入窗体。

    (4)CardLayout:每次仅显示一个控件,可切换显示。

    (5)GridLayout:控件会自动排列成指定行列数,且每个控件大小相同。

    (6)GridBagLayout:最复杂、功能最强大的布局,可以实现不规则的网格状布局。

    详见:swing布局管理

    Swing还提供了一些常用的布局用的静态常量,例如居中可以用SwingContants.CENTER,除此之外还有BOTTOM, EAST, WEST等可供使用,相当方便。详见:SwingContants

    public void addListener() {
    		plusButton.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				operator.setText("+");
    			}			
    		});
    		equalButton.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				String op1 = operand1.getText();
    				String op2 = operand2.getText();
    				String opt = operator.getText();
    				String formula = op1+opt+op2;
    				try {
    					result.setText(""+ jse.eval(formula));
    				} catch (ScriptException e1) {
    					// TODO Auto-generated catch block
    					logger.warning(e1.toString());;
    				}
    			}			
    		});
    	}
    

    这段代码的作用是给各个按钮加上监听器,当按钮被点击时,触发对应的函数。此处等于号的运算,我引用了Javascript引擎中的eval函数,可以对字符串直接运算得出结果,相当方便。缺点是有安全隐患。

    ant

    ant是一种类似与C++下的makefile类似的编译机制,通过ant可以清晰方便地定义依赖关系,方便编译且提高效率。跟makefile相同,已经编译且没有改变的文件,ant不会重复编译。

    ant的语法

    ant采用XML语法,类似于web中的HTML语言的标签,利用Eclipse,我们可以轻易地得到模板,并在模板上进行修改。这里主要记录几个常用的标签。

    (1) <property name="xxx" value="yyy"/>
    此处声明了属性,即可在后文中使用${xxx}来代替yyy,尤用于文件路径,避免了文件路径变动导致build.xml中多处改动的情况。

    (2)<target name="secondstep" depends="firststep"></target>
    target标签表示ant执行的步骤,target执行顺序由depends属性决定,当该target的depends属性指向的target都已经执行完毕后,才会执行。

    (3)<javac srcdir="${src.path}" destdir = "${build.path}"></javac>
    javac标签用于编译java文件。

    (4)<java classname="test" classpath="${build.path}"></java>
    java标签用于执行class程序。

    (5)<junit printsummary="true"><test name="testTest"></test></junit>
    junit标签用于在ant中执行junit单元检测,test标签为测试的名称。

    更多ant标签及其属性详见:ant如何编译项目

    junit

    junit是java单元测试工具,其优势在于简化了单元测试的工作。这里记录一些在学习junit过程中学习的知识。

    元数据:记录数据的数据。junit中常用的元数据有:

    @Test:测试

    @Before:使用了该元数据的方法在每个测试方法执行之前都要执行一次。

    @After:使用了该元数据的方法在每个测试方法执行之后要执行一次。

    @Test(timeout=xxx):该元数据传入了一个时间(毫秒)给测试方法,如果测试方法在制定的时间之内没有运行完,则测试也失败。

    @ignore:该元数据标记的测试方法在测试中会被忽略。

    重要函数:assertEquals(aaa, bbb) ——junit会对assertEquals中的aaa和bbb进行判断,若不相等则会返回failure。这是重要的测试函数。


    以上就是第一周的学习记录。

    下附Java GUI部分Calculator的完整代码。


    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptException;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.logging.Logger;
    
    
    public final class Calculator {
    	private Calculator(){}
    	
    	public static void main (String args[]) {
    		CalFrame myFrame = new CalFrame();
    		myFrame.generateFrame();
    		myFrame.addListener();
    	}
    }
    
    class CalFrame {
    	CalFrame() {}
    	
    	private JFrame myframe = new JFrame("Calculator");
    	private static ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");
    	private static String strClassName = CalFrame.class.getName();
    	private static Logger logger = Logger.getLogger(strClassName);
    	private JButton plusButton = new JButton("+");
    	private JButton minusButton = new JButton("-");
    	private JButton multiplyButton = new JButton("*");
    	private JButton divideButton = new JButton("/");
    	private JButton equalButton = new JButton("=");
    	private JTextField equalText = new JTextField("=");
    	private JTextField operand1 = new JTextField("");
    	private JTextField operand2 = new JTextField("");
    	private JTextField operator = new JTextField("");
    	private JTextField result = new JTextField("");
    	protected void generateFrame() {
    		myframe.setSize(500,200);
    		//layout setting
    		myframe.setLayout(new GridLayout(0,5));
    		//other settings
    		equalText.setEditable(false);
    		operator.setEditable(false);
    		result.setEditable(false);
    		equalText.setHorizontalAlignment(SwingConstants.CENTER);
    		operand1.setHorizontalAlignment(SwingConstants.CENTER);
    		operand2.setHorizontalAlignment(SwingConstants.CENTER);
    		operator.setHorizontalAlignment(SwingConstants.CENTER);
    		result.setHorizontalAlignment(SwingConstants.CENTER);
    		//add listeners
    		
    		//add component
    		myframe.add(operand1);
    		myframe.add(operator);
    		myframe.add(operand2);
    		myframe.add(equalText);
    		myframe.add(result);
    		myframe.add(plusButton);
    		myframe.add(minusButton);
    		myframe.add(multiplyButton);
    		myframe.add(divideButton);
    		myframe.add(equalButton);
    		//visible setting
    		myframe.setVisible(true);
    		myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    	
    	public void addListener() {
    		plusButton.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				operator.setText("+");
    			}			
    		});
    		minusButton.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				operator.setText("-");
    			}			
    		});
    		multiplyButton.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				operator.setText("*");
    			}			
    		});
    		divideButton.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				operator.setText("/");
    			}			
    		});
    		equalButton.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				String op1 = operand1.getText();
    				String op2 = operand2.getText();
    				String opt = operator.getText();
    				String formula = op1+opt+op2;
    				try {
    					result.setText(""+ jse.eval(formula));
    				} catch (ScriptException e1) {
    					// TODO Auto-generated catch block
    					logger.warning(e1.toString());;
    				}
    			}			
    		});
    	}
    }
  • 相关阅读:
    8.2 TensorFlow实现KNN与TensorFlow中的损失函数,优化函数
    8.3 TensorFlow BP神经网络构建与超参数的选取
    8.3 TensorFlow BP神经网络构建与超参数的选取
    聊一聊深度学习的weight initialization
    聊一聊深度学习的weight initialization
    聊一聊深度学习的activation function
    聊一聊深度学习的activation function
    Hadoop-2.7.4 八节点分布式集群安装
    Hadoop-2.7.4 八节点分布式集群安装
    Vue学习笔记(一) 入门
  • 原文地址:https://www.cnblogs.com/JerryChan31/p/6582126.html
Copyright © 2011-2022 走看看