实验报告封面
实验内容及步骤
实验一、实现百分制成绩转成“优、良、中、及格、不及格”五级制成绩的功能
代码
public class MyUtil{
public static String percentage2fivegrade(int grade) {
if (grade < 0 || grade >100)
return "错误";
//如果成绩小于60,转成“不及格”
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 "优秀";
//其他,转成“错误”
else
return "错误";
}
}
测试代码
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 testExceptions () {
assertEquals("错误",MyUtil.percentage2fivegrade(105));
assertEquals("错误",MyUtil.percentage2fivegrade(-55));
}
@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));
}
}
实验截图
实验二、以TDD的方式研究学习StringBuffer
代码
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer buffer = new StringBuffer();
buffer.append('S');
buffer.append("tringBuffer");
//System.out.println(buffer.charAt(1));
System.out.println(buffer.capacity());
//System.out.println(buffer.indexOf("tring12345"));
//System.out.println("buffer = " + buffer.toString());
//System.out.println(buffer.length());
}
}
测试代码
import junit.framework.TestCase;
import org.junit.Test;
public class StringBufferDemoTest extends TestCase {
StringBuffer a = new StringBuffer("StringBuffer");
//测试12个字符
StringBuffer b = new StringBuffer("StringBufferStringBuffer");
//测试24个字符
StringBuffer c = new StringBuffer("StringBufferStringBufferStringBuffer");
//测试36个字符
StringBuffer d = new StringBuffer("StringBufferStringBufferStringBufferStr");
//测试39个字符
@Test
public void testcharAt() {
assertEquals('S',a.charAt(0));
assertEquals('e',b.charAt(10));
assertEquals('f',c.charAt(20));
assertEquals('B',d.charAt(30));
}
@Test
public void testcapacity() {
assertEquals(28,a.capacity());
assertEquals(40,b.capacity());
assertEquals(52,c.capacity());
assertEquals(55,d.capacity());
}
@Test
public void testindexOf() {
assertEquals(0,a.indexOf("Str"));
assertEquals(3,b.indexOf("ing"));
assertEquals(6,c.indexOf("Buffer"));
assertEquals(8,d.indexOf("ff"));
}
@Test
public void testlength() {
assertEquals(12,a.length());
assertEquals(24,b.length());
assertEquals(36,c.length());
assertEquals(39,d.length());
}
}
实验截图
实验三、对设计模式示例进行扩充,体会OCP原则和DIP原则的应用,初步理解设计模式
代码
// Server Classes
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 = 5337;
}
public void DisplayValue(){
System.out.println (value);
}
}
// Pattern Classes
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();
}
}
//Client classes
class Document {
Data pd;
Document(Factory pf){
pd = pf.CreateDataObject();
}
public void DisplayData(){
pd.DisplayValue();
}
}
//Test class
public class MyDoc {
static Document d;
public static void main(String[] args) {
d = new Document(new ShortFactory());
d.DisplayData();
}
}
实验四、以TDD的方法开发一个复数类Complex
代码
public class Complex {
public double getRealPart() {
return RealPart;
}
public double getImagePart() {
return ImagePart;
}
double RealPart;
double ImagePart;
public void setRealPart(double realPart) {
RealPart = realPart;
}
public void setImagePart(double imagePart) {
ImagePart = imagePart;
}
public Complex(){}
public Complex(double R,double I){
this.ImagePart = I;
this.RealPart = R;
}
@Override
public String toString() {
return "Complex{" +
"RealPart=" + RealPart +
", ImagePart=" + ImagePart +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Complex complex = (Complex) o;
return Double.compare(complex.getRealPart(), getRealPart()) == 0 &&
Double.compare(complex.getImagePart(), getImagePart()) == 0;
}
Complex ComplexAdd(Complex a) {
return new Complex(this.RealPart + a.RealPart,this.ImagePart + a.ImagePart);
}
Complex ComplexSub(Complex a){
return new Complex(this.RealPart - a.RealPart,this.ImagePart - a.ImagePart);
}
Complex ComplexMulti(Complex a){
return new Complex(this.RealPart*a.RealPart-this.ImagePart*a.ImagePart,
this.ImagePart*a.RealPart+this.RealPart*a.ImagePart);
}
Complex ComplexDiv(Complex a){
double scale = a.getRealPart()*a.getRealPart() + a.getImagePart()*a.getImagePart();
Complex b = new Complex(a.getRealPart() / scale, - a.getImagePart() / scale);
return this.ComplexMulti(b);
}
}
测试代码
import junit.framework.TestCase;
import org.junit.Test;
public class ComplexTest extends TestCase {
Complex a = new Complex(5.0, 6.0);
Complex b = new Complex(-3.0, 4.0);
@Test
public void testgetRealPart() {
assertEquals(5.0,a.getRealPart());
assertEquals(-3.0,b.getRealPart());
}
@Test
public void testgetImagePart() {
assertEquals(6.0,a.getImagePart());
assertEquals(4.0,b.getImagePart());
}
@Test
public void testtoString() {
assertEquals("Complex{RealPart=5.0, ImagePart=6.0}",a.toString());
assertEquals("Complex{RealPart=-3.0, ImagePart=4.0}",b.toString());
}
@Test
public void testequals() {
assertEquals(false,a.equals(b));
Complex c = new Complex(a.getRealPart(),a.getImagePart());
assertEquals(true,a.equals(c));
}
@Test
public void testComplexAdd() {
assertEquals(2.0,a.ComplexAdd(b).getRealPart());
assertEquals(10.0,a.ComplexAdd(b).getImagePart());
}
@Test
public void testComplexSub() {
assertEquals(8.0,a.ComplexSub(b).getRealPart());
assertEquals(2.0,a.ComplexSub(b).getImagePart());
}
@Test
public void testComplexMulti() {
assertEquals(-39.0,a.ComplexMulti(b).getRealPart());
assertEquals(2.0,a.ComplexMulti(b).getImagePart());
}
@Test
public void testComplexDiv() {
assertEquals(0.36,a.ComplexDiv(b).getRealPart());
assertEquals(-1.52,a.ComplexDiv(b).getImagePart());
}
}
实验截图
实验五、代码建模
实验PSP
步骤 | 耗时 | 百分比 |
---|---|---|
需求分析 | 20分钟 | 4.7% |
设计 | 20分钟 | 4.7% |
代码实现 | 200分钟 | 47% |
测试 | 80分钟 | 19% |
分析总结 | 100分钟 | 23.8% |
遇到的问题
在使用IDEA的过程中,我的Junit安装不了,后来问同学得知可以从电脑找到后再导入。但是我的APPdata文件夹找不到,后来发现是被系统隐藏了,在使用管理员权限显示后就好了。