Experiment Report of Java object-oriented programming
Unit Test
(1) Three kinds of code
伪代码
It is an algorithm description language, with clear structure, simple code, good readability and similar natural language, do not rigidly adhere to the specific implementation
百分制转五分制: 如果成绩小于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 "错误"; }}
Write the product code, we also have to write the test code, to prove that their code is not a problem.
Import Org.junit.test;import Junit.framework.testcase;public class Myutiltest extends TestCase {@Test public void t Estnormal () {assertequals ("failed", Myutil.percentage2fivegrade (55)); Assertequals ("Pass", Myutil.percentage2fivegrade (65)); Assertequals ("Medium", Myutil.percentage2fivegrade (75)); Assertequals ("Good", Myutil.percentage2fivegrade (85)); Assertequals ("excellent", Myutil.percentage2fivegrade (95)); } @Test public void Testexceptions () {assertequals ("error", Myutil.percentage2fivegrade (105)); Assertequals ("Error", Myutil.percentage2fivegrade (-55)); } @Test public void Testboundary () {assertequals ("failed", Myutil.percentage2fivegrade (0)); Assertequals ("Pass", Myutil.percentage2fivegrade (60)); Assertequals ("Medium", Myutil.percentage2fivegrade (70)); Assertequals ("Good", Myutil.percentage2fivegrade (80)); Assertequals ("excellent", Myutil.percentage2fivegrade (90)); Assertequals ("excellent", MYUTIL.PERCENTAGE2FIVEgrade (100)); }}
Experiment Two
TDD (test driven devlopment, testing-driven development)
The general steps for TDD are as follows:
Clear the current functionality to be completed and record it as a test list
Quick completion of writing test cases for this feature
Test code compilation does not pass (no product code)
Write the Product Code
Test Pass
Refactor the code and ensure the test passes (refactoring the next lab session)
Cycle through the development of all functions
Study StringBuffer in the way of TDD
Based on the code given by the teacher, the application of JUnit is performed.
import junit.framework.TestCase;import org.junit.Test;public class ComplexTest extends TestCase { Complex a = new Complex( 0,3); Complex b= new Complex( -1,-1); @Test public void testComplexAdd () throws Exception { assertEquals( "-1.0+2.0i",a.ComplexAdd(b).toString()); } @Test public void testComplexSub () throws Exception{ assertEquals( "1.0+4.0i",a.ComplexSub(b).toString()); } @Test public void testComplexMulti () throws Exception { assertEquals( "3.0 -3.0i",a.ComplexMulti(b).toString()); } @Test public void testComplexDiv () throws Exception { assertEquals( "-1.5 -1.5i",a.ComplexDiv(b).toString()); } }
Experiment Three
用自己的学号%6进行取余运算,根据结果进行代码扩充:0:让系统支持Byte类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的,加上学号水印1:让系统支持Short类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的,加上学号水印2:让系统支持Boolean类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的,加上学号水印3:让系统支持Long类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的,加上学号水印4:让系统支持Float类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的,加上学号水印5:让系统支持Double类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的,加上学号水印
I am the 5th question:
The experimental code allows the system to support the double class and add the test code to the MyDoc class to indicate that the correct
Abstract class Data {abstract public void displayvalue ();} Class Integer extends Data {int value; Integer () {value = 100; } @Override public void Displayvalue () {System.out.println (value); }}class Double extends Data {double value; Double () {value = 82.8; } @Override 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 Doublefactory extends Factory {public Data createdataobject () {return new Doub Le (); }} class Document {Data pd; Document (Factory pf) {PD = PF. CreateDataObject (); } public void Displaydata () {PD. Displayvalue (); }} public class MyDoc {static DocumEnt d, E; public static void Main (string[] args) {d = new Document (new Doublefactory ()); E = new Document (new Intfactory ()); E.displaydata (); D.displaydata (); } }
Experiment Four
Submit: Unit test code and run success and code on the cloud Codes link, to add the number watermark
Task: Develop a plural class complex in TDD mode.
public class Complex {private Double R; private double I; Public Complex (Double R, double i) {THIS.R = R; THIS.I = i; } public static Double Getrealpart (double R) {return r; } public static double Getimagepart (double i) {return i; } public Complex Complexadd (Complex c) {return new Complex (R + C.R, i + CI); } public Complex Complexsub (Complex c) {return new Complex (R-C.R, I-C.I); } public Complex Complexmulti (Complex c) {return new Complex (R * c.r-i * C.I., R * C.I. + i * C.R); } public Complex Complexdiv (Complex c) {return new Complex ((R * C.I. + i * C.R)/(CI-C.I. + C.R * c.r), (i * CI + R * c.r)/(CI * C.I. + C.R * c.r)); The public String toString () {string s = ""; if (i > 0) s = r + "+" + i + "I"; if (i = = 0) s = r + ""; if (I < 0) s = r + "" + i + "I"; return s; }}
import junit.framework.TestCase;import org.junit.Test;public class ComplexTest extends TestCase { Complex a = new Complex( 0,3); Complex b= new Complex( -1,-1); @Test public void testComplexAdd () throws Exception { assertEquals( "-1.0+2.0i",a.ComplexAdd(b).toString()); } @Test public void testComplexSub () throws Exception{ assertEquals( "1.0+4.0i",a.ComplexSub(b).toString()); } @Test public void testComplexMulti () throws Exception { assertEquals( "3.0 -3.0i",a.ComplexMulti(b).toString()); } @Test public void testComplexDiv () throws Exception { assertEquals( "-1.5 -1.5i",a.ComplexDiv(b).toString()); } }
Experiment Five
Using STARUML to model the code in experiment two
Experiment Summary and experience
The experiment I felt the difficulty of learning Java, because the use of Mac computers, some of the steps and the teacher gave the tutorial is not the same, through their own tutorial step by step to complete the experiment, feeling very fulfilling. In this experiment I learned about three different 伪代码
产品代码
测试代码
kinds of differences and learned to use Juint test code in idea.
20165229 Experiment Report of Java object-oriented programming