标签:modules index work RKE str 一个 version idea 博客
我们先写伪代码
,伪代码
从意图层面来解决问题;
有了伪代码
我们用特定编程语言翻译一下,就是可用的产品代码
;写完产品代码
,接下来写测试代码
证明自己的代码没有问题。Java编程时,程序员对类实现的测试叫单元测试
Setting->Plugins->Marketplace
搜索Junit
下载JUnitGenerator V2.0
,下载后重启IDEA即可找到下载好的JunitFile->Project Settings->Modules->Dependencies->‘+‘
在你的安装IDEA的路径中找到lib文件夹导入图中的两个包后,点击OK
即可正常使用百分制转五分制: 如果成绩小于60,转成“不及格” 如果成绩在60与70之间,转成“及格” 如果成绩在70与80之间,转成“中等” 如果成绩在80与90之间,转成“良好” 如果成绩在90与100之间,转成“优秀” 其他,转成“错误”
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 "错误";???
}
}
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));???
}
}
public class StringBufferDemo {??
StringBuffer buffer = new StringBuffer();???
public void 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 s) {???????
return buffer.indexOf(s);???
}
}
import org.junit.Test;
public class StringBufferDemoTest extends TestCase {???
StringBuffer a = new
StringBuffer("StringBuffer");???
StringBuffer b = new
StringBuffer("StringBufferStringBuffer");???
StringBuffer c = new StringBuffer("StringBuffer12345StringBuffer12345");???
@Test???
public void testcharAt() throws Exception {???????
assertEquals('S',a.charAt(0));???????
assertEquals('u',b.charAt(7));???????
assertEquals('g',c.charAt(22));???
}???
@Test???
public void testcapacity() throws? Exception {???????
assertEquals(28,a.capacity());???????
assertEquals(40,b.capacity());???????
assertEquals(50,c.capacity());???
}???
@Test???
public void testlength() throws? Exception {???????
assertEquals(12,a.length());???????
assertEquals(24,b.length());???????
assertEquals(34,c.length());???
}???
@Test???
public void testindexOf() throws Exception {???????
assertEquals(6,a.indexOf("Buf"));???????
assertEquals(11,b.indexOf("rSt"));???????
assertEquals(15,c.indexOf("45St"));???
}
}
对capacity()方法的理解:
capacity()用于返回当前字符串的最大容量,初始值为16,即无论字符串中是否有内容,都会被分配容量,当有字符被写入是,容量自动增加,例如当输入StringBuffer
时会增加12个大小的空间,调用capacity()会返回当前字符串最大容量28
结果截图
让系统支持Short类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印。
S.O.L.I.D原则
我的学号为9,按照要求题目应为3,即让系统支持Long类,代码如下:
public class MyDoc {???
static Document D;???
static Document E;???
public static void main(String[] args) {???????
D = new Document(new IntFactory());???????
D.DisplayData();???????
E = new Document(new LongFactory());???????
E.DisplayData();???
}
}
abstract class Data {???
abstract public void DisplayValue();
}
class Integer extends Data {???
int value;???
Integer() {???????
value=100;???
}???
public void DisplayValue(){???????
System.out.println (value);???
}
}
class Long extends Data {???
long value;???
Long() {???????
value = 1000000000;???
}???
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 LongFactory extends Factory {???
public Data CreateDataObject(){???????
return new Long();???
}
}
class Document {???
Data pd;???
Document(Factory pf){???????
pd = pf.CreateDataObject();???
}???
public void DisplayData(){???????
pd.DisplayValue();???
}
}
// 定义属性并生成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;???
double ImagePart;???
public Complex() {}???
public Complex(double R,double I) {???????
RealPart = R;???????
ImagePart = I;???
}???
public double getRealPart() {???????
return RealPart;???
}???
public double getImagePart() {???????
return ImagePart;???
}???
public boolean equals(Object obj) {???????
if(this == obj)???????????
return true;???????
if(obj == null)???????????
return false;???????
if(getClass() != obj.getClass())???????????
return false;???????
if(!(obj instanceof Complex))???????????
return false;???????
else {???????????
Complex complex = (Complex) obj;???????????
if (complex.ImagePart == ((Complex) obj).ImagePart)???????????????
return true;???????????
if (complex.RealPart == ((Complex) obj).RealPart)???????????????
return true;???????
}???????
return false;???
}???
public? String toString() {???????
String str = "";???????
if (RealPart == 0&&ImagePart == 0)???????????
str = "0.0";???????
else if (RealPart == 0&&ImagePart != 0)???????????
str = ImagePart + "" + "i";???????
else if (RealPart != 0&&ImagePart == 0)???????????
str = RealPart + "";???????
else if (RealPart !=0&&ImagePart > 0)???????????
str = RealPart + "" + "+" + ImagePart + "i";???????
else if (RealPart !=0&&ImagePart < 0)???????????
str = RealPart + "" + ImagePart + "i";???????
return str;???
}???
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) {???????
return new Complex(RealPart*a.getRealPart() - ImagePart*a.getImagePart(),RealPart*a.getImagePart() +
ImagePart*a.getRealPart());???
}???
public Complex ComplexDiv(Complex a) {???????
if(a.getRealPart() == 0&&a.getImagePart() == 0) {???????????
System.out.println("除数不能为0");???????????
return new Complex();???????
}???????
else ??????????
return new Complex((RealPart*a.getRealPart() -
ImagePart*a.getImagePart())/(a.getRealPart()*a.getRealPart()-a.getImagePart()*a.getImagePart()),(RealPart*a.getImagePart() + ImagePart*a.getRealPart())/(a.getRealPart()*a.getRealPart()-a.getImagePart()*a.getImagePart()));???
}
}
import junit.framework.TestCase;
import org.junit.Test;
public class ComplexTest extends TestCase {???
Complex a = new Complex(2.0,5.0);???
Complex b = new Complex(1.0,-4.0);???
Complex c = new Complex(-3.0,2.0);???
Complex d = new Complex(-4.0,-3.0);???
Complex e = new Complex(0.0,0.0);???
@Test???
public void testgetRealPart() throws Exception {???????
assertEquals(2.0,a.getRealPart());???????
assertEquals(1.0,b.getRealPart());???????
assertEquals(-3.0,c.getRealPart());???????
ssertEquals(-4.0,d.getRealPart());???????
assertEquals(0.0,e.getRealPart());???
}???
@Test???
public void testgetImagePart() throws Exception {???????
assertEquals(5.0,a.getImagePart());???????
assertEquals(-4.0,b.getImagePart());???????
assertEquals(2.0,c.getImagePart());???????
assertEquals(-3.0,d.getImagePart());???????
assertEquals(0.0,e.getImagePart());???
}???
@Test???
public void testequals() throws Exception {???????
assertEquals(true,a.toString().equals(a.toString()));???????
assertEquals(false,b.toString().equals(c.toString()));???????
assertEquals(false,d.toString().equals(e.toString()));???
}???
@Test???
public void testtoString() throws Exception {???????
assertEquals("2.0+5.0i",a.toString());???????
assertEquals("1.0-4.0i",b.toString());???????
assertEquals("-3.0+2.0i",c.toString());???????
assertEquals("-4.0-3.0i",d.toString());???????
assertEquals("0.0",e.toString());???
}???
@Test???
public void testComplexAdd() throws Exception {???????
assertEquals("3.0+1.0i",a.ComplexAdd(b).toString());???????
assertEquals("-7.0-1.0i",c.ComplexAdd(d).toString());???????
assertEquals("-4.0-3.0i",d.ComplexAdd(e).toString());???
}???
@Test???
public void testComplexSub() throws Exception {???????
assertEquals("1.0+9.0i",a.ComplexSub(b).toString());???????
assertEquals("1.0+5.0i",c.ComplexSub(d).toString());???????
assertEquals("-3.0+2.0i",c.ComplexSub(e).toString());???
}???
public void testComplexMulti() throws Exception {???????
assertEquals("22.0-3.0i",a.ComplexMulti(b).toString());???????
assertEquals("18.0+1.0i",c.ComplexMulti(d).toString());???????
assertEquals("0.0",b.ComplexMulti(e).toString());???
}???
public void testComplexDiv() throws Exception {???????
assertEquals("-1.4666666666666666+0.2i",a.ComplexDiv(b).toString());???????
assertEquals("2.5714285714285716+0.14285714285714285i",c.ComplexDiv(d).toString());???????
assertEquals("0.0",a.ComplexDiv(e).toString());???
}
}
选择画类图的题目为任务3
12345
添加至了第八行中,而这一行代码在执行时被注释掉不会被执行,但第五行的字符串和之前相比并没有发生改变,因此在运行时不会出现给出的结果,应该将代码12345
添加至第五行的字符串末尾才能出现事例中的运行结果https://gitee.com/wangzihong/20175209/tree/master
20175209 实验二《Java面向对象程序设计》实验报告
标签:modules index work RKE str 一个 version idea 博客
原文地址:https://www.cnblogs.com/wangzihong0213/p/10733361.html