The example is simple, design a program, enter three integers, determine whether the three integers as the length of the three sides can form a triangle, if you can return the type of the triangle (normal triangles, isosceles triangle or equilateral triangle)
Implementation of the code (I write myself casually to do an example, the estimated code quality is poor.) ):
public class Judgetriangle
{
Common triangles
private static int normal_triangle = 0;
Isosceles Triangle
private static int isosceles_triangle = 1;
Equilateral triangle
private static int equilateral_triangel = 2;
Cannot form a triangle
private static int error_code =-1;
public int Getnormaltriangle ()
{
return normal_triangle;
}
public int Getisoscelestriangle ()
{
return isosceles_triangle;
}
public int Getequilateraltriangle ()
{
return Equilateral_triangel;
}
public int GetErrorCode ()
{
return error_code;
}
public int judgetype (int a,int b,int c)
{
if ((a<=0) | | | (b<=0) | | (c<=0))
{
return error_code;
}
if ((A+b<c | | Math.Abs (a) >c) | | (B+c<a | | Math.Abs (b-c) >a) | | (A+c<b | | Math.Abs (A-C) >b))
{
return error_code;
}
if (a==b | | b==c | | c==a)
{
if (a==b && b==c)
{
return Equilateral_triangel;
}
Else
{
return isosceles_triangle;
}
}
Else
{
return normal_triangle;
}
}
}
Unit Test Code:
Import static org.junit.assert.*;
Import Org.junit.After;
Import Org.junit.Before;
Import Org.junit.Test;
public class Judgetriangletest {
private static Judgetriangle Judgetriangle = new Judgetriangle ();
@Before
public void SetUp () throws Exception {
}
@Test
public void Testisnormaltriangle () throws Exception {
Assertequals (Judgetriangle.getnormaltriangle (), Judgetriangle.judgetype (3, 4, 5));
}
@Test
public void Testisisoscelestriangle () throws Exception {
Assertequals (Judgetriangle.getisoscelestriangle (), Judgetriangle.judgetype (2, 2, 3));
}
@Test
public void Testequilateraltriangle () throws Exception {
Assertequals (Judgetriangle.getequilateraltriangle (), Judgetriangle.judgetype (2, 2, 2));
}
@Test
public void Testnottriangle () throws Exception {
Assertequals (Judgetriangle.geterrorcode (), Judgetriangle.judgetype (10,-10, 10));
Assertequals (Judgetriangle.geterrorcode (), Judgetriangle.judgetype (10, 2, 2));
}
@After
public void TearDown () throws Exception {
}
}
These two links are easy to introduce for JUnit understood
Http://tech.sina.com.cn/s/2010-01-18/14081218926.shtml
http://leowzy.iteye.com/blog/793077
A simple example of JUnit doing unit testing