zoukankan      html  css  js  c++  java
  • 20165317JAVA实验二-面向对象程序设计

    JAVA实验二-面向对象程序设计

    提交点一

    参考Intellj IDEA 简易教程-单元测试完成单元测试的学习

    • 在IDEA中建立名为MyUtil5317的project,并在其src文件夹中创建名为MyUtil5317的public类,并输入如下代码。
    public class MyUtil5317{
        public static String percentage2fivegrade(int grade){
            if (grade < 60 && grade >= 0 )
                return "不及格";
            else if (grade < 70 && grade >= 0 )
                return "及格";
            else if (grade < 80 && grade >= 0 )
                return "中等";
            else if (grade < 90 && grade >= 0 )
                return "良好";
            else if (grade <= 100 && grade >= 0 )
                return "优秀";
            else
                return "错误";
        }
    }
    
    • 在项目根目录下建立名为test的子目录,并将其设定为test的源代码存放目录,在目录中建立测试源代码,并按照Intellj IDEA 简易教程-单元测试的要求进行操作,其中代码如下:
    import org.junit.Test;
    import junit.framework.TestCase;
    public class MyUtil5317Test extends TestCase {
        @Test
        public void testNormal() {
            assertEquals("不及格", MyUtil5317.percentage2fivegrade(55));
            assertEquals("及格", MyUtil5317.percentage2fivegrade(65));
            assertEquals("中等", MyUtil5317.percentage2fivegrade(75));
            assertEquals("良好", MyUtil5317.percentage2fivegrade(85));
            assertEquals("优秀", MyUtil5317.percentage2fivegrade(95));
        }
    
        @Test
        public void testException() {
            assertEquals("错误", MyUtil5317.percentage2fivegrade(-5));
            assertEquals("错误", MyUtil5317.percentage2fivegrade(105));
        }
    
        @Test
        public void testBoundary() {
            assertEquals("不及格", MyUtil5317.percentage2fivegrade(0));
            assertEquals("及格", MyUtil5317.percentage2fivegrade(60));
            assertEquals("中等", MyUtil5317.percentage2fivegrade(70));
            assertEquals("良好", MyUtil5317.percentage2fivegrade(80));
            assertEquals("优秀", MyUtil5317.percentage2fivegrade(90));
            assertEquals("优秀", MyUtil5317.percentage2fivegrade(100));
        }
    }
    
    • 结果截图如下

    提交点二

    参考积极主动敲代码,使用JUnit学习Java,以 TDD的方式研究学习StringBuffer。

    • 按照上述博客和第一提交点完成代码的编辑
    public class StringBufferDemo{
        public static void main(String [] args){
            StringBuffer buffer = new StringBuffer();
            buffer.append('S');
            buffer.append("tringBuffer12345");
            System.out.println(buffer.charAt(1));
            System.out.println(buffer.capacity());
            System.out.println(buffer.indexOf("tringBuffer12345"));
            System.out.println("buffer = " + buffer.toString());
            System.out.println(buffer.length());
        }
    }
    
    • 编辑测试用例如下
    import org.junit.Test;
    import junit.framework.TestCase;
    
    public class StringBufferDemotest extends TestCase {
        StringBuffer jh = new StringBuffer("nxy and jh");
        StringBuffer nxy = new StringBuffer("nxy and jh danthony nb");
        public void testcharat() {
            assertEquals('n', jh.charAt(0));
            assertEquals('n', nxy.charAt(0));
        }
        public void testcapacity() {
            assertEquals(26, jh.capacity());
            assertEquals(38, nxy.capacity());
        }
        public void testlength() {
            assertEquals(10, jh.length());
            assertEquals(22, nxy.length());
        }
        public void testindexof() {
            assertEquals(0, jh.indexOf("nxy"));
            assertEquals(20,nxy.indexOf("nb"));
        }
    }
    
    • 提交的结果截图

    提交点三

    • 实验题目:让系统支持Double类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
    • 实验代码
    abstract class Data {
        abstract public void DisplayValue();
    }
    class Integer extends  Data {
        int value;
        Integer() {
            value=100;
        }
        public void DisplayValue(){
            System.out.println (value);
        }
    }
    class Short extends Data {
        short value;
        Short() {
            value = 5317;
        }
        public void DisplayValue(){
            System.out.println (value);
        }
    }
    
    abstract class Factory {
        abstract public Data CreateDataObject();
    }
    class IntFactory extends Factory {
        public Data CreateDataObject(){
            return new Integer();
        }
    }
    class ShortFactory extends Factory {
        public Data CreateDataObject(){
            return new Short();
        }
    }
    
    class Document {
        Data pd;
        Document(Factory pf){
            pd = pf.CreateDataObject();
        }
        public void DisplayData(){
            pd.DisplayValue();
        }
    }
    
    public class MyDoc {
        static Document d;
        public static void main(String[] args) {
            d = new Document(new ShortFactory());
            d.DisplayData();
        }
    }
    
    • 结果运行截图

    提交点四

    • 任务:以TDD的方式开发一个复数类Complex
      • 定义属性并生成getter,setter
      • double RealPart;
      • double ImagePart
    • 实验代码
    public class Complex {
        double R;
        double I;
    
        Complex(double R, double I) {
            this.R = R;
            this.I = I;
        }
    
        public static double getRealPart(double R) {  
            return R;
        }
    
        public static double getImagePart(double I) { 
            return I;
        }
    
        public Complex ComplexAdd(Complex c) {
            return new Complex(R + c.R, I + c.I);
        }
    
        public Complex ComplexSub(Complex c) {
            return new Complex(R - c.R, I - c.I);
        }
    
        public Complex ComplexMulti(Complex c) {
            return new Complex(R * c.R - I * c.I, R * c.I + I * c.R);
        }
    
        public Complex ComplexDiv(Complex c) {
            return new Complex((R * c.I + I * c.R) / (c.I * c.I + c.R * c.R), (I * c.I + R * c.R) / (c.I * c.I + c.R * c.R));
        }
    
        public String toString() {
            String str = "";
            if (I > 0)
                str = R + "+" + I + "i";
            if (I == 0)
                str = R+"";
            if (I < 0)
                str = R + "" + I + "i";
            return str;
        }
        public boolean equals(Object obj){
            if (this == obj)
                return true;
            if(obj==null)
                return false;
            if(obj instanceof Complex){
                Complex p = (Complex) obj;
                if(p.R==this.R &&p.I==this.I )
                    return true;
            }
            return false;
        }
    }
    
    • 调试代码如下
    import junit.framework.TestCase;
    import org.junit.Test;
    
    public class ComplexTest extends TestCase {
        Complex c1 = new Complex(1.0,2.0);
        Complex c2 = new Complex(2.0,2.0);
        Complex c3 = new Complex(2.0,0.0);
        @Test
        public void testGetRealPart() throws Exception {
            assertEquals(-1.1, Complex.getRealPart(-1.1));
            assertEquals(6.0, Complex.getRealPart(6.0));
            assertEquals(0.0, Complex.getRealPart(0.0));
        }
    
        @Test
        public void testGetImagePart() throws Exception {
            assertEquals(-1.1, Complex.getImagePart(-1.1));
            assertEquals(6.0, Complex.getImagePart(6.0));
            assertEquals(0.0, Complex.getImagePart(0.0));
        }
    
        @Test
        public void testComplexAdd() throws Exception {
            assertEquals("3.0+4.0i", c1.ComplexAdd(c2).toString());
            assertEquals("3.0+2.0i", c1.ComplexAdd(c3).toString());
            assertEquals("4.0+2.0i", c2.ComplexAdd(c3).toString());
        }
    
        @Test
        public void testComplexSub() throws Exception {
            assertEquals("-1.0", c1.ComplexSub(c2).toString());
            assertEquals("-1.0+2.0i", c1.ComplexSub(c3).toString());
            assertEquals("0.0+2.0i", c2.ComplexSub(c3).toString());
        }
    
        @Test
        public void testComplexMulti() throws Exception {
            assertEquals("-2.0+6.0i", c1.ComplexMulti(c2).toString());
            assertEquals("2.0+4.0i", c1.ComplexMulti(c3).toString());
            assertEquals("4.0+4.0i", c2.ComplexMulti(c3).toString());
        }
    
        @Test
        public void testComplexComplexDiv() throws Exception {
            assertEquals("0.75+0.75i", c1.ComplexDiv(c2).toString());
            assertEquals("1.0+0.5i", c1.ComplexDiv(c3).toString());
            assertEquals("1.0+1.0i", c2.ComplexDiv(c3).toString());
        }
        @Test
        public void testEquals() throws Exception{
            assertEquals(false,c1.equals(c2));
            assertEquals(false,c1.equals(c3));
            assertEquals(false,c2.equals(c3));
            assertEquals(true,c1.equals(c1));
        }
    
    }
    
    • 运行结果如下

    提交点五

    • uml图如下

    码云链接:https://gitee.com/BESTI-IS-JAVA-2018/exp2

  • 相关阅读:
    sql语句 字段的赋值
    sql查询字段是否为空
    二分查找算法
    sql语句查看库里有没有这张表,有就删除
    字符串转换成元组
    python+selenium多窗口之间切换
    java代码实现highchart与数据库数据结合完整案例分析(二)---折线图
    java代码实现highchart与数据库数据结合完整案例分析(一)---饼状图
    java中集合格式及json格式的特点和转换
    substring()的用法和注意事项
  • 原文地址:https://www.cnblogs.com/ningxinyu/p/8893630.html
Copyright © 2011-2022 走看看