public class MyUtil{
public static String percentage2fivegrade(int grade){
//如果成绩小于60,转成“不及格”
if (grade < 60 && grade >=0)
return "不及格";
//如果成绩在60与70之间,转成“及格”
else if (grade < 70 && grade >=0)
return "及格";
//如果成绩在70与80之间,转成“中等”
else if (grade < 80 && grade >=0)
return "中等";
//如果成绩在80与90之间,转成“良好”
else if (grade < 90 && grade >=0)
return "良好";
//如果成绩在90与100之间,转成“优秀”
else if (grade <= 100 && grade >=0)
return "优秀";
//其他,转成“错误”
else
return "错误";
}
}
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));
}
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 void testException() {
assertEquals("错误", MyUtil.percentage2fivegrade(-5));
assertEquals("错误", MyUtil.percentage2fivegrade(105));
}
}
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("tring"));
System.out.println("buffer = " + buffer.toString());
System.out.println(buffer.length());
}
}
public class StringBufferDemoTest extends TestCase {
StringBuffer a = new StringBuffer("StringBuffer");
StringBuffer b = new StringBuffer("StringBufferStringBuffer");
StringBuffer c = new StringBuffer("StringBufferStringBufferStringBuffer");
public void testchatAt() {
assertEquals('S',a.charAt(0));
assertEquals('f',b.charAt(20));
assertEquals('B',c.charAt(30));
}
public void testcapacity() {
assertEquals(28,a.capacity());
assertEquals(40,b.capacity());
assertEquals(52,c.capacity());
}
public void testindexOf() {
assertEquals(0,a.indexOf("Str"));
assertEquals(3,b.indexOf("ing"));
assertEquals(8,c.indexOf("ffe"));
}
public void testlength() {
assertEquals(12,a.length());
assertEquals(24,b.length());
assertEquals(36,c.length());
}
}
- 3 让系统支持Float类,并在MyDoc类中添加测试代码表明添加正确
abstract class Data {
abstract public void DisplayValue();
}
class Float extends Data {
float value;
Float() {
value=100;
}
public void DisplayValue(){
System.out.println (value);
}
}
// Pattern Classes
abstract class Factory {
abstract public Data CreateDataObject();
}
class FloatFactory extends Factory {
public Data CreateDataObject(){
return new Float();
}
}
//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 FloatFactory());
d.DisplayData();
}
}
public class Complex {
private double RealPart;
private double ImagePart;
public Complex(double R,double I){
RealPart = R;
ImagePart = I;
}
double getRealPart(){
return RealPart;
}
double getImagePart(){
return ImagePart;
}
void setRealPart(double R){
RealPart=R;
}
void setImagePart(double I){
ImagePart=I;
}
Complex ComplexAdd(Complex a){
return new Complex(a.getRealPart()+this.getRealPart(),a.getImagePart()+this.getImagePart());
}
Complex ComplexSub(Complex a){
return new Complex(this.getRealPart()-a.getRealPart(),this.getImagePart()-a.getImagePart());
}
Complex ComplexMulti(Complex a){
return new Complex(this.getRealPart()*a.getRealPart()-this.getImagePart()*a.getImagePart(),this.getRealPart()*a.getImagePart()+this.getImagePart()*a.getRealPart());
}
Complex ComplexDiv(Complex a){
return new Complex((this.getRealPart()*a.getRealPart()+this.getImagePart()*a.getImagePart())/(a.getRealPart()*a.getRealPart()-a.getImagePart()*a.getImagePart()),(this.getImagePart()*a.getRealPart()-this.getRealPart()*a.getImagePart())/(a.getRealPart()*a.getRealPart()-a.getImagePart()*a.getImagePart()));
}
@Override
public String toString(){
String str="";
str+=this.getRealPart();
if(this.getImagePart()>=0) str+="+";
str+=this.getImagePart();
str+="i";
return str;
}
}
import junit.framework.TestCase;
/**
* Created by luzijie20155222 on 17-4-21.
*/
public class ComplexTest extends TestCase {
Complex a=new Complex(1,2);
Complex b=new Complex(2,1);
Complex c=new Complex(0,-1);
Complex d=new Complex(-2,-1);
public void testGetRealPart() throws Exception {
assertEquals(1.0,a.getRealPart());
assertEquals(2.0,b.getRealPart());
assertEquals(0.0,c.getRealPart());
assertEquals(-2.0,d.getRealPart());
}
public void testGetImagePart() throws Exception {
assertEquals(2.0,a.getImagePart());
assertEquals(1.0,b.getImagePart());
assertEquals(-1.0,c.getImagePart());
assertEquals(-1.0,d.getImagePart());
}
public void testComplexAdd() throws Exception {
assertEquals("3.0+3.0i",a.ComplexAdd(b).toString());
assertEquals("2.0+0.0i",b.ComplexAdd(c).toString());
assertEquals("-2.0-2.0i",c.ComplexAdd(d).toString());
assertEquals("0.0+0.0i",b.ComplexAdd(d).toString());
}
public void testComplexSub() throws Exception {
assertEquals("-1.0+1.0i",a.ComplexSub(b).toString());
assertEquals("2.0+2.0i",b.ComplexSub(c).toString());
assertEquals("2.0+0.0i",c.ComplexSub(d).toString());
assertEquals("4.0+2.0i",b.ComplexSub(d).toString());
}
public void testComplexMulti() throws Exception {
assertEquals("0.0+5.0i",a.ComplexMulti(b).toString());
assertEquals("1.0-2.0i",b.ComplexMulti(c).toString());
assertEquals("-1.0+2.0i",c.ComplexMulti(d).toString());
assertEquals("-3.0-4.0i",b.ComplexMulti(d).toString());
}
public void testComplexDiv() throws Exception {
}
}
- 5 使用StarUML对实验二中的代码进行建模,发类图的截图,加上学号水印。