Experimental content
1. Initial mastery of unit testing and TDD
2. Understanding and mastering the object-oriented three elements: encapsulation, inheritance, polymorphism
3. Initial mastery of UML modeling
4. Familiarity with S.O.L.I.D principles
5. Understanding Design Patterns
Experimental requirements
1. Students who do not have a Linux base are advised to start with the Linux basics (new version) Vim Editor course
2. Complete the experiment, write the experiment Report, the experiment report is published in the blog site blog, note that the experimental report is focused on the results of the operation, problems encountered (tool search, installation, use, program editing, commissioning, operation, etc.), solutions (empty methods such as "Check Network", "Ask classmates", "reading" 0 points) as well as analysis (what can be learned from it, what gains, lessons, etc.). The report can refer to the guidance of Fan Fei Dragon Teacher
3. Plagiarism is strictly forbidden, and the results of the experiment of the perpetrator are zero, and other punitive measures are added.
4. Please first in the laboratory building in the ~/code directory with their own school number to create a directory, code and UML diagram to put in this directory, no study number will be required to redo, and then follow the following steps to practice.
Experiment Step (i) three codes
Pseudo code:
Percentile five-point system:
If the score is less than 60, turn into "fail"
If the score is between 60 and 70, turn into "pass"
If the score is between 70 and 80, turn into "medium"
If the score is between 80 and 90, turn into "good"
If the score is between 90 and 100, turn into "good"
Other, turn into "error"
Product Code:
PublicClassmyutil{PublicStatic StringPercentage2fivegrade(int grade) {If the score is less than 0, turn to "error"if (Grade <0))Return"Error";If the score is less than 60, turn into "fail"Elseif (Grade <30LReturn"Failed";If the score is between 60 and 70, turn into "pass"Elseif (Grade <90gReturn "pass"; //if the score is between 70 and 80, turn to "medium" else if ( Grade < 80) return "medium"; //if the score is between 80 and 90, turn into "good" else if (Grade < 90) return "good"; //if the score between 90 and 100, turn into" excellent "else if (Grade <= Span class= "Hljs-number" >100) return "excellent"; //If the score is greater than 100, turn to" error "else return " error "; }}
Test code:
PublicClassmyutiltest {PublicStaticvoidMainString[] (args) {if (Myutil.percentage2fivegrade (55)! ="Failed") System.Out.println ("Test failed!");ElseIfMyutil.percentage2fivegrade (65)! ="Pass") System.Out.println ("Test failed!");ElseIfMyutil.percentage2fivegrade (75)! = "medium") System. out.println ( "test failed!"); else if ( myutil.percentage2fivegrade (85)! = "good") System. out.println ( "test failed!"); else if ( myutil.percentage2fivegrade (95)! = "excellent") System. out.println ( "test failed!"); else System. out.println ( "test passed!");}
Run:
(ii) Study of learning StringBuffer in the form of TDD
The coding rhythm of TDD is: Add test code, JUnit appears red bar, modify product code->junit appear green bar, task completed. Based on TDD, we do not have an over-design situation, and the requirements are expressed through test cases, and our product code is only allowed to pass the test. According to the requirements of the tutorial I completed the myutiltest, and the teacher put forward the stringbuffer I did not want to come out, this week to spare time I will ask the students to make the answer.
Experiment Code:
PublicClassStringbufferdemo {StringBuffer buffer =New StringBuffer ();PublicStringbufferdemo (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 buf) {return Buffer.indexof (BUF);}}
For this experiment, design the test class code:
PublicClassStringbufferdemotestExtendsTestCase {StringBuffer A =New StringBuffer ("StringBuffer");Test 12 characters (<=16) StringBuffer B =New StringBuffer ("Stringbufferstringbuffer");Test 24 characters (>16&&<=34) StringBuffer C =New StringBuffer ("Stringbufferstringbufferstringbuffer");Test 36 characters (>=34)@TestPublicvoidTestcharat()Throws exception{Assertequals (' S ', A.charat (0)); Assertequals (' G ', A.charat (5)); Assertequals (' R ', A.charat (11));}@TestPublicvoidTestcapacity()Throws exception{Assertequals (28,a.capacity ()); Assertequals (40,b.capacity ()); Assertequals (52,c.capacity ());}@Testpublic void testlength () throws exception{assertequals (12,a.length ( )); Assertequals (24,b.length ()); Assertequals (36,c.length ());} @Test public void testindexof () throws exception{assertequals (0,a.indexof (" Str ")); Assertequals (5,a.indexof ( "GBu"); Assertequals (10,a.indexof (
Experimental results:
Three: The MyDoc class is extended to support the Boolean class, a preliminary understanding of design patterns
Let the system support the short class, and add the test code in the MyDoc class to indicate that adding the correct
Run:
Four, the development of a plural class complex in TDD, the requirements are as follows:
Define properties and generate Getter,setter
Double Realpart;
Double Imagepart;
Defining constructors
Public Complex ()
Public Complex (double r,double I)
Override Object
public boolean equals (Object obj)
Public String toString ()
Define public methods: subtraction
Complex Complexadd (Complex a)
Complex complexsub (Complex a)
Complex Complexmulti (Complex a)
Complex Complexdiv (Complex a)
Experiment Code:
public class Mycomplex {
Private double m;
private double N;
Public double Getm (double m)
{
return m;
}
Public double Getn (double n)
{
return n;
}
public void Setm (double m)
{
This.m=m;
}
public void Setn (double n)
{
This.n=n; }
Public Mycomplex (double m, double N) {
THIS.M = m;
THIS.N = n;
}
Public Mycomplex Add (Mycomplex c) {
return new Mycomplex (M + c.m, n + C.N);
}
Public Mycomplex minus (Mycomplex c) {
return new Mycomplex (M-C.M, N-C.N);
}
Public Mycomplex Multiply (Mycomplex c) {
return new Mycomplex (M * c.m-n * C.N, M * C.N + n * c.m);
}
Public Mycomplex Complexdiv (Mycomplex c)
{
return new Mycomplex ((MC.M+NC.N)/((C.MC.M) (C.NC.N)), (NC.M-MC.N)/((C.MC.M) (C.NC.N)));
}
Public String toString () {
String str = new string ();
if (this.n>=0)
{
str=m+ "+" +n+ "I";
}
else str=m+ "-" + (-N) + "I";
return str;
}
public boolean equals (Mycomplex c)
{
if (THIS.M==C.M&&THIS.N==C.N)
return true;
Else
return false;
}
}
Test Class Code:
Import Junit.framework.TestCase;Import Org.junit.Test;PublicClassMycomplextestExtendsTestCase {Mycomplex A =New Mycomplex (1,2); Mycomplex B =New Mycomplex (1,-4); Mycomplex C =New Mycomplex (19,0); Mycomplex d =New Mycomplex (0,-3); Mycomplex e =New Mycomplex (1,0); Mycomplex f =New Mycomplex (1,0);@TestPublicvoidTestadd() {Assertequals ("2.0-2.0i", A.add (b). ToString ()); Assertequals ("1.0-1.0i", A.add (d). toString ());@TestPublicvoidTestminus() {Assertequals ("0.0+6.0i", A.minus (b). ToString ()); Assertequals (" -18.0+2.0i", A.minus (c). ToString ());@TestPublicvoidTestmultiply () {assertequals ( "9.0-2.0i", a.multiply (b). ToString ()); Assertequals ( "19.0+38.0i", A.multiply (c). ToString ());} @Test public void testcomplexdiv () { Assertequals ( " -0.4375+0.375i", A.complexdiv (b). ToString ()); Assertequals (" 1.1875+4.75i ", C.complexdiv (b). ToString ()); @Test public void testequals () {assertEquals ( Span class= "Hljs-keyword" >false,a.equals (b)); Assertequals (true,e.equals (f))}}
Run:
(v) using STARUML to model the code in Experiment two, the
20145207 Experiment Report of Java object-oriented programming