第一部分
实验代码
public class MyUtil{
public static String chengji(int grade){
if ((grade < 0))
return "错误";
else if (grade < 60)
return "不及格";
else if (grade < 70)
return "及格";
else if (grade < 80)
return "中等";
else if (grade < 90)
return "良好";
else if (grade <= 100)
return "优秀";
else
return "错误";
}
}
测试代码
public class MyUtiltext {
public static void main(String[] args) {
zhengchang();
yichang();
bianjie();
}
public static void zhengchang(){
if(MyUtil.chengji(55) != "不及格")
System.out.println("test failed 1!");
else if(MyUtil.chengji(65) != "及格")
System.out.println("test failed 2!");
else if(MyUtil.chengji(75) != "中等")
System.out.println("test failed 3!");
else if(MyUtil.chengji(85) != "良好")
System.out.println("test failed 4!");
else if(MyUtil.chengji(95) != "优秀")
System.out.println("test failed 5!");
else
System.out.println("test passed!");
}
public static void yichang(){
if(MyUtil.chengji(-10) != "错误")
System.out.println("test failed 6!");
else if(MyUtil.chengji(115) != "错误")
System.out.println("test failed 7!");
else
System.out.println("test passed!");
}
public static void bianjie(){
if(MyUtil.chengji(0) != "不及格")
System.out.println("test failed 8!");
else if(MyUtil.chengji(60) != "及格")
System.out.println("test failed 9!");
else if(MyUtil.chengji(70) != "中等")
System.out.println("test failed 10!");
else if(MyUtil.chengji(80) != "良好")
System.out.println("test failed 11!");
else if(MyUtil.chengji(90) != "优秀")
System.out.println("test failed 12!");
else if(MyUtil.chengji(100) != "优秀")
System.out.println("test failed 13!");
else
System.out.println("test passed!");
}
}
实验代码截图
第二部分
实验代码
import java.lang.StringBuffer;
public class StringBufferone {
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());
}
}
测试代码
import junit.framework.TestCase;
import org.junit.Test;
public class StringBufferoneTest extends TestCase {
StringBuffer a = new StringBuffer("StringBuffer");
StringBuffer b = new StringBuffer("StringBufferStringBuffer");
StringBuffer c = new StringBuffer("StringBufferStringBufferStringBuffer");
@Test
public void testcharAt() throws Exception{
assertEquals('S',a.charAt(0));
assertEquals('g',b.charAt(5));
assertEquals('S',c.charAt(12));
}
public void testcapacity() throws Exception{
assertEquals(28,a.capacity());
assertEquals(40,b.capacity());
assertEquals(52,c.capacity());
}
public void testindexOf() throws Exception{
assertEquals(a.indexOf("Str"),0);
assertEquals(6,b.indexOf("Buf"));
assertEquals(9,c.indexOf("fer"));
}
}
第三部分
实验代码
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=100;
}
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 Integer();
}
}
class Document {
Data pd;
Document(Factory pf){
pd = pf.CreateDataObject();
}
public void DisplayData(){
pd.DisplayValue();
}
}
public class MyDoc {
static Document d;
static Document c;
public static void main(String[] args) {
d = new Document(new IntFactory());
c = new Document(new ShortFactory());
d.DisplayData();
c.DisplayData();
}
}
第四部分
实验代码
public class Complex {
int RealPart;
int ImagePart;
public Complex(){}
public Complex(int R,int I){
RealPart = R;
ImagePart = I;
}
public int getRealPart(){
return RealPart;
}
public int getImagePart(){
return ImagePart;
}
public String toString(){
String s = new String();
if(ImagePart>0)
s = getRealPart()+"+"+getImagePart()+"i";
if(ImagePart==0)
s = getRealPart()+"";
if(ImagePart<0)
s = getRealPart()+""+getImagePart()+"i";
if(RealPart==0)
s = getImagePart()+"i";
if(ImagePart==0&&RealPart==0)
s = "0";
return s;
}
public Complex ComplexAdd(Complex a){
Complex d = new Complex();
d.RealPart = RealPart + a.RealPart;
d.ImagePart = ImagePart + a.ImagePart;
return d;
}
public Complex ComplexSub(Complex a){
Complex d = new Complex();
d.RealPart = RealPart - a.RealPart;
d.ImagePart = ImagePart - a.ImagePart;
return d;
}
public Complex ComplexMulti(Complex a){
Complex d = new Complex();
d.RealPart = RealPart*a.RealPart - ImagePart*a.ImagePart;
d.ImagePart = RealPart*a.ImagePart + ImagePart*a.RealPart;
return d;
}
public Complex ComplexDiv(Complex a) {
Complex d = new Complex();
d.RealPart = (RealPart * a.RealPart + ImagePart * a.ImagePart) / (a.RealPart * a.RealPart + a.ImagePart * a.ImagePart);
d.ImagePart = (ImagePart * a.RealPart - RealPart * a.ImagePart) / (a.RealPart * a.RealPart + a.ImagePart * a.ImagePart);
return d;
}
}
测试代码
import junit.framework.TestCase;
import org.junit.Test;
public class ComplexTest extends TestCase {
Complex a = new Complex(1,2);
Complex b = new Complex(-1,-2);
Complex c = new Complex(0,0);
@Test
public void testGetRealPart() {
assertEquals(1,a.getRealPart());
assertEquals(-1,b.getRealPart());
assertEquals(0,c.getRealPart());
}
@Test
public void testGetImagePart() {
assertEquals(2,a.getImagePart());
assertEquals(-2,b.getImagePart());
assertEquals(0,c.getImagePart());
}
@Test
public void testToString() {
assertEquals("1+2i",a.toString());
assertEquals("-1-2i",b.toString());
assertEquals("0",c.toString());
}
@Test
public void testComplexAdd() {
String q = a.ComplexAdd(b).toString();
String w = b.ComplexAdd(c).toString();
String e = c.ComplexAdd(a).toString();
assertEquals("0",q);
assertEquals("-1-2i",w);
assertEquals("1+2i",e);
}
@Test
public void testComplexSub() {
String r = a.ComplexSub(b).toString();
String t = b.ComplexSub(c).toString();
String y = c.ComplexSub(a).toString();
assertEquals("2+4i",r);
assertEquals("-1-2i",t);
assertEquals("-1-2i",y);
}
@Test
public void testComplexMulti() {
String u = a.ComplexMulti(b).toString();
String i = b.ComplexMulti(c).toString();
String o = c.ComplexMulti(a).toString();
assertEquals("3-4i",u);
assertEquals("0",i);
assertEquals("0",o);
}
@Test
public void testComplexDiv() {
String r = a.ComplexDiv(b).toString();
String y = c.ComplexDiv(a).toString();
assertEquals("-1",r);
assertEquals("0",y);
}
}