Part 1 You need to simply first experience running a JUnit test using existing c
ID: 3757540 • Letter: P
Question
Part 1
You need to simply first experience running a JUnit test using existing code. In a new project called Lab 2, you will re-use the same Rectangle class from Lab 1 and pair it with a newly posted RectangleTest class.
1. Under the CULearn section called Lab Software, please read the PowerPoint presentation on Setting up a JUnit Test in Netbeans.
2. Open Netbeans and create a new project called Lab2.
• Do NOT create a main class.
3. Forget about testing for a minute and add Rectangle to your new project, same as in Lab 1
• Create a New Java Class
• Make sure the name of the class is Rectangle
• Make sure the name of the package is Shapes
• Copy-paste the posted Rectangle code into the empty class
• Make sure everything compiles.
4. Follow the guidance in the PowerPoint presentation to create a JUnit class called RectangleTest.
5. Copy-paste the posted code for RectangleTest.java into your empty RectangleTest.
6. Run RectangleTest. Hopefully you see the GREEN bar indicating that all tests pass successfully.
Part 2
You now need to experience test-driven development. Test-driven means that the test code is written before – or at least, the same time – as the actual source code. I have provided two JUnit test classes called TriangleTest and CircleTest.
1. Add these two test files to the same project, to the same place as RectangleTest
2. You will have compiler errors simply because you have not yet written the needed code, Triangle and Circle.
• Pick one class, either is fine. I will pick Triangle, for example.
3. Using the UML below, write the public interface only for Triangle. Your goal is to write enough so that the test code compiles even though the test runs fail.
• Systematically, translate each line of the UML diagram into Java code.
• For each method, write the public call signature but leave the private implementation empty i. i.e. Simply write { }
4. Only after the code compiles, incrementally write the implementations of the Triangle methods
• Write the code for one method. Study Rectangle to figure out how to implement these methods.
• Run the JUnit test. One less test should fail.
• Repeat, until one-by-one all the tests pass.
5. Repeat for the other class.
Circle Triangle
-radius:double -base:double
+Circle(radius:double) -height:double
+getRadius():double +Triangle(base:double, height:double)
+toString():String +getHeight():double
+perimeter():double +getBase():double
+area():int +perimeter():double
+toString():String +area():int
Tip: Finding the perimeter of a triangle is technically not possible if you only know the base and the height. For our solution, see: https://math.stackexchange.com/questions/80397/can-we-find-theperimeter-of-a-triangle-given-only-its-base-and-height
Part 3
Your final task is a small exercise in array programming.
1. Within your Netbeans project, add a new file called Lab2c.java
2. This client class will contain only the main() method
public static void main(String args[]) { }
3. Within the main() method, write code to accomplish the following task:
• Construct an array of 10 Circle objects (called circles), each one individually initialised to a random radius.
• Construct two arrays of 10 doubles (called areas and perimeters) , filling their values with the area and perimeter of the corresponding circle, in the circles array.
• Note: It is possible to do the next task without these arrays, but by following these instructions you are gaining practice in arrays-of-primitives
• For each Circle object, print out its radius, area, and perimeter
4. Run your program. Its output should resemble the following
run:
Circle 0(32.0) : Area = 3216.98816, Perimeter = 201.06176.
Circle 1(78.0) : Area = 19113.433559999998, Perimeter = 490.08804.
Circle 2(48.0) : Area = 7238.22336, Perimeter = 301.59263999999996.
Circle 3(12.0) : Area = 452.38896, Perimeter = 75.39815999999999.
Circle 4(63.0) : Area = 12468.97071, Perimeter = 395.84033999999997.
Circle 5(41.0) : Area = 5281.01279, Perimeter = 257.61037999999996.
Circle 6(29.0) : Area = 2642.07719, Perimeter = 182.21222.
Circle 7(84.0) : Area = 22167.05904, Perimeter = 527.78712.
Circle 8(41.0) : Area = 5281.01279, Perimeter = 257.61037999999996.
Circle 9(84.0) : Area = 22167.05904, Perimeter = 527.78712.
BUILD SUCCESSFUL (total time: 0 seconds)
Explanation / Answer
Circle.java
package Shapes;
public class Circle {
private double radius;
public Circle(double radius){
if(radius < 0)
throw new IllegalArgumentException("Radius must be larger then 0");
this.radius = radius;
}
public double getRadius(){
return radius;
}
public double perimeter(){
return 2*Math.PI*radius;
}
public double area(){
return Math.PI*Math.pow(radius,2);
}
public String toString(){
return "r="+radius;
}
}
CircleTest.java
package Shapes;
import junit.framework.Assert;
import org.junit.Test;
import static org.junit.Assert.*;
import Shapes.Circle;
public class CircleTest {
public CircleTest() {
}
// TODO add test methods here.
// The methods must be annotated with annotation @Test. For example:
//
@Test
public void testConstructor() {
Circle c = new Circle(5.0);
assertEquals(5.0, c.getRadius(), 0.001);
}
@Test
public void testConstructorNegative() {
try {
Circle r = new Circle(-5.0);
fail();
} catch (IllegalArgumentException e) { }
}
@Test
public void testPerimeter() {
Circle c = new Circle (6.0);
assertEquals( 2*6.0*3.14159, c.perimeter(), 0.001);
}
@Test
public void testArea() {
Circle c = new Circle(6.0);
assertEquals( 6.0*6.0*3.14159, c.area(), 0.001);
}
@Test
public void testToString() {
Circle c = new Circle(5.0);
assertEquals ("r=5.0", c.toString());
}
}
Lab2c.java
package Shapes;
import java.util.Date;
import java.util.Random;
public class Lab2c {
public static void main(String args[]){
Circle[] circles = new Circle[10];
double[] areas = new double[10];
double[] perimeters = new double[10];
Random random = new Random(new Date().hashCode());
for(int i = 0; i < 9;i++){
double seed = random.nextDouble()*100;
circles[i] = new Circle(seed);
areas[i] = circles[i].area();
perimeters[i] = circles[i].perimeter();
System.out.println("Circle "+i+"("+seed+") : Area = "+areas[i]+", Perimeter = "+perimeters[i]+".");
}
}
}
Rectangle.java
package Shapes;
public class Rectangle {
private double length;
private double width;
public Rectangle(double length, double width){
if (length <= 0.0 || width <= 0.0)
throw new IllegalArgumentException ("Shape's dimensions must be positive values");
this.length = length;
this.width = width;
}
public double getLength() { return this.length; }
public double getWidth() { return this.width; }
public double perimeter() { return (2*this.length) + (2* this.width); }
public double area() { return this.length * this.width; }
@Override
public boolean equals(Object other) {
if (other == this) return true;
if (other == null) return false;
if (getClass() != other.getClass()) return false;
Rectangle r = (Rectangle)other;
return ((Math.abs(length - r.length)< 0.001) &&
(Math.abs(width-r.width)< 0.001));
}
@Override
public String toString() {
return "l=" + this.length + " x w=" + this.width;
}
}
RectangleTest.java
package Shapes;
import junit.framework.Assert;
import org.junit.Test;
import static org.junit.Assert.*;
import Shapes.Rectangle;
public class RectangleTest {
public RectangleTest() {
}
// TODO add test methods here.
// The methods must be annotated with annotation @Test. For example:
//
@Test
public void testConstructor() {
Rectangle r = new Rectangle(5.0,6.0);
assertEquals(5.0, r.getLength(), 0.001);
assertEquals(6.0, r.getWidth(), 0.001);
}
@Test
public void testConstructorNegativeLength() {
try {
Rectangle r = new Rectangle(-5,6);
fail();
} catch (IllegalArgumentException e) { }
}
@Test
public void testConstructorNegativeWidth() {
try {
Rectangle r = new Rectangle(5,0);
fail();
} catch (IllegalArgumentException e) { }
}
@Test
public void testPerimeter() {
Rectangle r = new Rectangle(5.0 ,6.0);
assertEquals( 22.0, r.perimeter(), 0.001);
}
@Test
public void testArea() {
Rectangle r = new Rectangle(5.0 ,6.0);
assertEquals( 30.0, r.area(), 0.001);
}
@Test
public void testToString() {
Rectangle r = new Rectangle(5.0, 6.0);
assertEquals ("l=5.0 x w=6.0", r.toString());
}
}
Triangle.java
package Shapes;
public class Triangle {
private double base;
private double height;
public Triangle(double base, double height){
if(base < 1 || height < 1)
throw new IllegalArgumentException("One or more arguments are less than 0");
this.base = base;
this.height = height;
}
public double getHeight() {
return height;
}
public double getBase() {
return base;
}
public double perimeter(){
return base+Math.sqrt(Math.pow(base,2)+4*Math.pow(height,2));
}
public int area(){
return (int)(base*height)/2;
}
public String toString(){
return "b="+base+" x h="+height;
}
}
TriangleTest.java
package Shapes;
import junit.framework.Assert;
import org.junit.Test;
import static org.junit.Assert.*;
import Shapes.Triangle;
public class TriangleTest {
public TriangleTest() {
}
// TODO add test methods here.
// The methods must be annotated with annotation @Test. For example:
//
@Test
public void testConstructor() {
Triangle r = new Triangle(5.0,6.0);
assertEquals(5.0, r.getBase(), 0.001);
assertEquals(6.0, r.getHeight(), 0.001);
}
@Test
public void testConstructorNegativeBase() {
try {
Triangle r = new Triangle(-5,6);
fail();
} catch (IllegalArgumentException e) { }
}
@Test
public void testConstructorNegativeHeight() {
try {
Triangle r = new Triangle(5,0);
fail();
} catch (IllegalArgumentException e) { }
}
@Test
public void testPerimeter() {
Triangle r = new Triangle(5.0 ,6.0);
double expected = (6.0*6.0*4.0) + (5.0*5.0);
expected = Math.sqrt(expected) + 5.0;
assertEquals( expected, r.perimeter(), 0.001);
}
@Test
public void testArea() {
Triangle r = new Triangle(5.0 ,6.0);
assertEquals(0.5*5.0*6, r.area(), 0.001);
}
@Test
public void testToString() {
Triangle r = new Triangle(5.0, 6.0);
assertEquals ("b=5.0 x h=6.0", r.toString());
}
}