• 实验二 面向对象程序设计


    实验二面向对象程序设计《Java开发环境的熟悉》实验报告

    一、面向对象程序设计-1
    ①实验要求:
    1、参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST ;
    2、参考http://www.cnblogs.com/rocedu/p/6736847.html;
    3、提交最后三个测试用例都通过的截图,截图上要有画图加水印,输入自己的学号。
    ②实验步骤:
    根据参考,对每一步的程序进行编译运行,观察正常情况、错误情况与边界情况,并对其进行junit测试。
    ③实验代码:

    1、伪代码:
    百分制转五分制:
    如果成绩小于60,转成“不及格”
    如果成绩在60与70之间,转成“及格”
    如果成绩在70与80之间,转成“中等”
    如果成绩在80与90之间,转成“良好”
    如果成绩在90与100之间,转成“优秀”
    其他,转成“错误”
    2、产品代码:
    public class MyUtil{
    public static String percentage2fivegrade(int grade){
    //如果成绩小于0,转成“错误”
    if ((grade < 0))
    return "错误";
    //如果成绩小于60,转成“不及格”
    else if (grade < 60)
    return "不及格";
    //如果成绩在60与70之间,转成“及格”
    else if (grade < 70)
    return "及格";
    //如果成绩在70与80之间,转成“中等”
    else if (grade < 80)
    return "中等";
    //如果成绩在80与90之间,转成“良好”
    else if (grade < 90)
    return "良好";
    //如果成绩在90与100之间,转成“优秀”
    else if (grade <= 100)
    return "优秀";
    //如果成绩大于100,转成“错误”
    else
    return "错误";
    }
    }
    3、测试代码:
    import org.junit.Test;
    import junit.framework.TestCase;
    public class MyUtilTest extends TestCase {
    @Test
    public void testNormal() {
    assertEquals("不及格", MyUtil.percentage2fivegrade(55));
    assertEquals("及格", MyUtil.percentage2fivegrade(65));
    assertEquals("中等", MyUtil.percentage2fivegrade(75));
    assertEquals("良好", MyUtil.percentage2fivegrade(85));
    assertEquals("优秀", MyUtil.percentage2fivegrade(95));
    }
    @Test
    public void testException(){
    assertEquals("错误",MyUtil.percentage2fivegrade(-55));
    assertEquals("错误",MyUtil.percentage2fivegrade(105));
    }
    @Test
    public void testBoundary(){
    assertEquals("不及格", MyUtil.percentage2fivegrade(0));
    assertEquals("及格",MyUtil.percentage2fivegrade(60));
    assertEquals("中等",MyUtil.percentage2fivegrade(70));
    assertEquals("良好",MyUtil.percentage2fivegrade(80));
    assertEquals("优秀",MyUtil.percentage2fivegrade(90));
    assertEquals("优秀",MyUtil.percentage2fivegrade(100));
    }
    }
    4.运行截图

    二、面向对象程序设计-2
    ①实验要求:
    1、参考 积极主动敲代码,使用JUnit学习Java (http://www.cnblogs.com/rocedu/p/4837092.html);
    2、参考http://www.cnblogs.com/rocedu/p/6736847.html以 TDD的方式研究学习StringBuffer,提交你的单元测试用例和测试通过的截图,截图要加上学号水印。
    ②实验代码:
    1、产品代码:
    public class StringBufferDemo{
    StringBuffer buffer = new StringBuffer();
    public StringBufferDemo(StringBuffer buffer){
    this.buffer = buffer;
    }
    public Character charAt(int i){
    return buffer.charAt(i);
    }
    public int capacity(){
    return buffer.capacity();
    }
    public int length(){
    return buffer.length();
    }
    public int indexOf(String buf) {
    return buffer.indexOf(buf);
    }
    }
    2、测试代码:
    import junit.framework.TestCase;
    import org.junit.Test;
    // 20175308
    public class StringBufferDemoTest extends TestCase {
    StringBuffer a = new StringBuffer("StringBuffer");
    StringBuffer b = new StringBuffer("StringBufferStringBuffer");
    StringBuffer c = new StringBuffer("StringBuffer tested by 175229");
    @Test
    public void testCharAt() throws Exception{
    assertEquals('S',a.charAt(0));
    assertEquals('g',b.charAt(5));
    assertEquals('t',c.charAt(16));
    }
    @Test
    public void testcapacity() throws Exception{
    assertEquals(28,a.capacity());
    assertEquals(40,b.capacity());
    assertEquals(45,c.capacity());

    }
    @Test
    public void testlength() throws Exception{
        assertEquals(12,a.length());
        assertEquals(24,b.length());
        assertEquals(29,c.length());
    }
    @Test
    public void testindexOf(){
        assertEquals(6,a.indexOf("Buff"));
        assertEquals(3,b.indexOf("ing"));
        assertEquals(23,c.indexOf("1752"));
    }
    

    }
    3.运行截图

    三、面向对象程序设计-3
    ①实验要求:
    1、参考Java面向对象程序设计(http://www.cnblogs.com/rocedu/p/4472842.html);
    2、参考http://www.cnblogs.com/rocedu/p/6736847.html;
    3、对设计模式示例进行扩充,体会OCP原则和DIP原则的应用,初步理解设计模式
    用自己的学号%6进行取余运算,根据结果进行代码扩充:
    0: 让系统支持Byte类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
    1: 让系统支持Short类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
    2: 让系统支持Boolean类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
    3: 让系统支持Long类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
    4: 让系统支持Float类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
    5: 让系统支持Double类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
    ②实验步骤:
    根据参考编写java程序,由于我的学号为20175131,模6后为1,所以我需要扩充代码的要求为:让系统支持Short类,并在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=29;
    }
    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 p;
    Document(Factory pf){
    p = pf.CreateDataObject();
    }
    public void DisplayData(){
    p.DisplayValue();
    }
    }
    public class MyDoc {
    static Document a;
    static Document b;
    public static void main(String[] args) {
    a = new Document(new IntFactory());
    a.DisplayData();
    b=new Document(new ShortFactory());
    b.DisplayData();

    }                                
    

    }
    4.运行截图

    四、面向对象程序设计-4
    ①实验要求:
    1、提交:单元测试代码和运行成功截图及码云上代码链接,截图要加上学号水印;
    2、参考http://www.cnblogs.com/rocedu/p/6736847.html;
    3、任务:以TDD的方式开发一个复数类Complex,要求如下:
    // 定义属性并生成getter,setter
    double RealPart;
    double ImagePart;
    // 定义构造函数
    public Complex()
    public Complex(double R,double I)

    //Override Object
    public boolean equals(Object obj)
    public String toString()

    // 定义公有方法:加减乘除
    Complex ComplexAdd(Complex a)
    Complex ComplexSub(Complex a)
    Complex ComplexMulti(Complex a)
    Complex ComplexDiv(Complex a)
    ③实验代码:
    产品代码:
    public class Complex {
    double RealPart=0;
    double ImagePart=0;
    public Complex(){}
    public Complex(double RealPart,double ImagePart){
    this.RealPart=RealPart;
    this.ImagePart=ImagePart;

    }
    public double getRealPart(){
        return RealPart;
    }
    public double getImagePart(){
        return ImagePart;
    }
    public String toString(){
        String s = "";
        double r=RealPart;
        double i=ImagePart;
        if(r==0&&i==0){
            s="0";
        }
        else if(r==0&&i!=0){
            s=i+"i";
        }
        else if(r!=0&&i==0){
            s=r+"";
        }
        else if(r!=0&&i<0){
            s=r+""+i+"i";
        }
        else
        {
            s=r+"+"+i+"i";
        }
        return s;
    }
    public boolean equals(Object obj){
        if(this==obj){
            return true;
        }
        else return false;
    }
    
    public Complex ComplexAdd(Complex a){
        return new Complex(RealPart+a.getRealPart(),ImagePart+a.getImagePart());
    }
    public Complex ComplexSub(Complex a){
        return new Complex(RealPart-a.getRealPart(),ImagePart-a.getImagePart());
    }
    public Complex ComplexMulti(Complex a){
        double r=RealPart*a.getRealPart()-ImagePart*a.getImagePart();
        double i =ImagePart*a.getRealPart()+RealPart*a.getImagePart();
        return new Complex(r,i);
    }
    public Complex ComplexDiv(Complex a){
        double r=(RealPart * a.ImagePart + ImagePart * a.RealPart) / (a.ImagePart * a.ImagePart + a.RealPart * a.RealPart);
        double i=(ImagePart * a.ImagePart + RealPart * a.RealPart) / (a.RealPart * a.RealPart + a.RealPart * a.RealPart);
        return new Complex(r,i);
    }
    

    }
    2、测试代码:
    import org.junit.Test;
    import junit.framework.TestCase;
    public class ComplexTest extends TestCase {
    Complex a =new Complex(3.0,4.0);
    Complex b =new Complex( 2.0,-4.0);
    Complex c =new Complex(0.0,0.0);
    Complex d =new Complex(-3.0,0.0);
    Complex e =new Complex(-6.0,-1.0);
    @Test
    public void testgetRealPart()throws Exception{
    assertEquals(3.0,a.getRealPart());
    assertEquals(2.0,b.getRealPart());
    assertEquals(0.0,c.getRealPart());
    assertEquals(-3.0,d.getRealPart());
    assertEquals(-6.0,e.getRealPart());
    }
    @Test
    public void testgetImagePart()throws Exception{
    assertEquals(4.0,a.getImagePart());
    assertEquals(-4.0,b.getImagePart());
    assertEquals(0.0,c.getImagePart());
    assertEquals(0.0,d.getImagePart());
    assertEquals(-1.0,e.getImagePart());

    }
    @Test
    public void testtoString()throws Exception{
        assertEquals("3.0+4.0i",a.toString());
        assertEquals("2.0-4.0i",b.toString());
        assertEquals("0",c.toString());
        assertEquals("-3.0",d.toString());
        assertEquals("-6.0-1.0i",e.toString());
    }
    @Test
    public void testComplexAdd()throws Exception{
        assertEquals("5.0",a.ComplexAdd(b).toString());
        assertEquals("2.0-4.0i",b.ComplexAdd(c).toString());
        assertEquals("-1.0-4.0i",b.ComplexAdd(d).toString());
    }
    @Test
    public void testComplexSub()throws Exception{
        assertEquals("1.0+8.0i",a.ComplexSub(b).toString());
        assertEquals("-2.0+4.0i",c.ComplexSub(b).toString());
        assertEquals("3.0",c.ComplexSub(d).toString());
    }
    @Test
    public void testComplexMulti()throws Exception{
        assertEquals("22.0-4.0i",a.ComplexMulti(b).toString());
        assertEquals("0",b.ComplexMulti(c).toString());
        assertEquals("18.0+3.0i",d.ComplexMulti(e).toString());
    }
    @Test
    public void testComplexDiv()throws Exception{
        assertEquals("-0.2-1.25i",a.ComplexDiv(b).toString());
        assertEquals("0",c.ComplexDiv(b).toString());
    }
    @Test
    public void testequals()throws Exception{
        assertEquals(true,a.equals(a));
        assertEquals(false,a.equals(b));
    
    }
    

    }
    4.运行截图

    五、面向对象程序设计-5
    ①实验要求:
    1、使用WhiteStarUML对实验二中的代码进行建模,发类图的截图,加上学号水印;
    2、参考http://www.cnblogs.com/rocedu/p/6736847.html;
    3、类图中只少两个类。
    4.运行截图

    六.实验总结和感想
    刚开始做第一项和第二项的时候觉得这次的实验和上次的实验差不多,做到三才真正认真的开始做了,junit的问题困扰了我一下午的时间,花了很长的时间,在稍微运用junit测试代码后,才发现junit好用,一个成功的程序员只有合理的运用测试,才会让你的程序更加完美。用WhiteStarUML画程序的UML图,刚开始很盲目不知道如何画,参考了很多资料才慢慢学会

    七.学习资料

    https://www.cnblogs.com/rocedu/p/4837092.html
    https://blog.csdn.net/wokenshin/article/details/42592765

  • 相关阅读:
    Python学习-if条件语句
    Python学习-变量
    认识Python
    win7分盘
    mysql环境变量配置
    mysql的下载及配置
    c# excel xls保存
    js 在线引用
    js layer.js
    vue day3 bootstrap 联动下拉
  • 原文地址:https://www.cnblogs.com/WZL-DM/p/10732853.html
Copyright © 2020-2023  润新知