• 20155212 2017.05.17课堂代码实践总结


    20155212 2017.05.17课堂代码实践总结

    Junit

    在IDEA中对P145 MathTool.java 使用JUnit进行单元测试,测试用例不少于三个,要包含正常情况,边界情况。

    /**
     * Created by radish608 on 17-5-17.
     */
    import org.junit.Test;
    public class MathToolTest {
        @Test
        public void  MathToolTester(){
            assert MathTool.sum(1,2,3) == 6 : "error1";
            assert MathTool.sum(0) == 0 : "error2";
            assert MathTool.sum(1) == 1 : "error3";
            assert MathTool.sum(1,-1) == 0 : "error4";
            assert MathTool.sum(-1,-1) == -2 : "error5";
        }
    }
    
    • 运行截图

    构建对象

    1. 设计并实现一个Book类,定义成Book.java,Book包含书名,作者,出版社和出版日期,这些数据都要定义getter和setter。定义至少三个构造方法,接收并初始化这些数据。覆盖(Override)toString方法,返回良好的含有多行的书的描述信息。覆盖equals方法,书名,作者,出版社和出版日期完全一致才说明两本书是一样的。创建一个测试类Bookshelf, 其中的main方法创建并更新几个Book对象。Book至少包含三本本学期教材内容。
    2. 创建一个测试类Bookshelf,其中的main方法创建并更新几个Book对象。Book至少包含三本本学期教材内容。
    /**
     * Created by radish608 on 17-5-17.
     */
    class Book {
        String name, editor, publisher;
        int date;
    
        public Book(String name, String editor, String publisher, int date) {
            this.name = name;
            this.editor = editor;
            this.publisher = publisher;
            this.date = date;
        }
    
        public void setName() {
            this.name = name;
        }
    
        public void setEditor(String editor) {
            this.editor = editor;
        }
    
        public void setPublisher(String publisher) {
            this.publisher = publisher;
        }
    
        public void setDate(int date) {
            this.date = date;
        }
    
        public String getName() {
            return this.name;
        }
    
        public String getEditor() {
            return this.editor;
        }
    
        public String getPublisher() {
            return this.publisher;
        }
    
        public int getDate() {
            return this.date;
        }
    
        @Override
        public String toString() {
            return "Book20155212 [书名=" + name + ", 作者=" + editor + ", 出版社=" + publisher + ", 出版日期=" + date + "]";
        }
    
        @Override
        public boolean equals(Object obj) {
            if (!(obj instanceof Book)) {
                return false;
            }
            Book book = (Book) obj;
            if (!getName().equals(book.getName())) {
                return false;
            }
            if (!getEditor().equals(book.getEditor())) {
                return false;
            }
            if (!getPublisher().equals(book.getPublisher())) {
                return false;
            }
            if (!(getDate() == book.getDate())) {
                return false;
            }
            return true;
        }
    }
    
    
    public class Bookshelf {
        public static void main(String[] args) {
            Book book1 = new Book("Java学习笔记", "林信良", "清华大学出版社", 2016);
            Book book2 = new Book("密码学", "郑秀林", "金城出版社", 2016);
            Book book3 = new Book("数据结构与算法", "张乃孝", "高等教育出版社", 2015);
    
            System.out.println(book1.toString());
            System.out.println(book2.toString());
            System.out.println(book3.toString());
    
            System.out.println(book1.equals(book2));
        }
    }
    
    
    • 运行截图
  • 相关阅读:
    理性与感性
    JVM系列(之class文件)
    Java集合框架
    Java字符串连接的几种方式
    JVM系列(之ClassLoader)
    时之终结
    约束中的存在
    拿什么爱你?我的数学
    梦的表征、抽象的思维
    Spider with R
  • 原文地址:https://www.cnblogs.com/dky20155212/p/6866343.html
Copyright © 2020-2023  润新知