Question
package edu.wit.cs.comp1050; import java.util.Scanner; import edu.wit.cs.comp1050.TriangleObject; //TODO: document this class public class PA4d { /** * Reads triangle sides from the keyboard and * outputs
package edu.wit.cs.comp1050;
import java.util.Scanner;
import edu.wit.cs.comp1050.TriangleObject;
//TODO: document this class
public class PA4d {
/**
* Reads triangle sides from the keyboard and
* outputs various properties of that triangle to console
*
* @param args command-line arguments, ignored
*/
public static void main(String[] args) {
// TODO: write your code here
}
}
check with j unit
package edu.wit.cs.comp1050.tests;
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.security.Permission;
import org.junit.Assert;
import edu.wit.cs.comp1050.PA4d; import edu.wit.cs.comp1050.TriangleObject; import junit.framework.TestCase;
public class PA4dTestCase extends TestCase {
@SuppressWarnings("serial") private static class ExitException extends SecurityException {}
private static class NoExitSecurityManager extends SecurityManager { @Override public void checkPermission(Permission perm) {}
@Override public void checkPermission(Permission perm, Object context) {}
@Override public void checkExit(int status) { super.checkExit(status); throw new ExitException(); } }
@Override protected void setUp() throws Exception { super.setUp(); System.setSecurityManager(new NoExitSecurityManager()); }
@Override protected void tearDown() throws Exception { System.setSecurityManager(null); super.tearDown(); }
private void _test(String f1, String f2, String f3, String t) { final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
final String input = String.format("%s%n%s%n%s%n", f1, f2, f3);
final String expected = TestSuite.stringOutput(new String[] { "Enter three sides: ", "Enter the color: ", "Enter a boolean value for filled: ", t + "%n" }, new Object[] {});
System.setIn(new ByteArrayInputStream(input.getBytes())); System.setOut(new PrintStream(outContent)); try { PA4d.main(new String[] { "foo" }); } catch (ExitException e) {}
assertEquals(expected, outContent.toString());
System.setIn(null); System.setOut(null); }
private void _testSide1(double side , double expected) { double getSide=-1.; TriangleObject tri = null; try { tri = new TriangleObject(); tri.setSide1(side); getSide = tri.getSide1(); } catch (ExitException e) {}
assertEquals(expected, getSide); }
private void _testSide2(double side , double expected) { double getSide=-1.; TriangleObject tri = null; try { tri = new TriangleObject(); tri.setSide2(side); getSide = tri.getSide2(); } catch (ExitException e) {}
assertEquals(expected, getSide); }
private void _testSide3(double side , double expected) { double getSide=-1.; TriangleObject tri = null; try { tri = new TriangleObject(); tri.setSide3(side); getSide = tri.getSide3(); } catch (ExitException e) {}
assertEquals(expected, getSide); }
private void _testPerimeter(double side1, double side2, double side3 , double expected) { double getPerimeter=-1.; TriangleObject tri = null; try { tri = new TriangleObject(side1, side2, side3); getPerimeter = tri.getPerimeter(); } catch (ExitException e) {}
assertEquals(expected, getPerimeter); }
private void _testArea(double side1, double side2, double side3 , double expected) { double getArea=-1.; TriangleObject tri = null; try { tri = new TriangleObject(side1, side2, side3); getArea = tri.getArea(); } catch (ExitException e) {}
assertEquals(expected, getArea); }
private void _testSide1() { _testSide1(1.0, 1.0); _testSide1(2.0, 2.0); _testSide1(3.14, 3.14); _testSide1(-1.0, 1.0); _testSide1(0.0, 1.0); }
private void _testSide2() { _testSide2(1.0, 1.0); _testSide2(2.0, 2.0); _testSide2(3.14, 3.14); _testSide2(-1.0, 1.0); _testSide2(0.0, 1.0); }
private void _testSide3() { _testSide3(1.0, 1.0); _testSide3(2.0, 2.0); _testSide3(3.14, 3.14); _testSide3(-1.0, 1.0); _testSide3(0.0, 1.0); }
public void testSide() { _testSide1(); _testSide2(); _testSide3();
}
public void testGetPerimeter() { _testPerimeter(1.0, 1.0, 1.0, 3.0); _testPerimeter(2.1, 3.2, 4.1, 9.4); _testPerimeter(2., 0., 4., 7.); }
public void testGetArea() { _testArea(1.0, 1.0, 1.0, 0.4330127018922193); _testArea(2.1, 3.2, 4.1, 3.3163232653045163); _testArea(1.0, 0.0, -11.0, 0.4330127018922193); }
public void testIsFilled() { boolean isFilled = false; TriangleObject tri = null; try { tri = new TriangleObject(); isFilled = tri.isFilled(); } catch (ExitException e) {}
boolean expected = true; assertEquals(expected, isFilled); }
public void testColor() { String color = "white"; TriangleObject tri = null; try { tri = new TriangleObject(); color = tri.getColor(); } catch (ExitException e) {}
String expected = "red"; assertEquals(expected, color); } public void testProgram() { double a1, p1, s1, s2, s3;
a1=3.32; p1=9.4; s1=2.1; s2=3.2; s3=4.1; String r = String.format("The area is %.2f%nThe perimeter is %.2f%nTriangle: side1 = %.2f, side2 = %.2f, side3 = %.2f%n", a1,p1,s1, s2, s3); _test("2.1 3.2 4.1", "blue", "true", r); a1=0.43; p1=3.; s1=1.; s2=1.; s3=1.; r = String.format("The area is %.2f%nThe perimeter is %.2f%nTriangle: side1 = %.2f, side2 = %.2f, side3 = %.2f%n", a1,p1,s1, s2, s3); _test("1.0 1.0 1.0", "green", "false", r); a1=0.50; p1=3.5; s1=1.; s2=1.5; s3=1.; r = String.format("The area is %.2f%nThe perimeter is %.2f%nTriangle: side1 = %.2f, side2 = %.2f, side3 = %.2f%n", a1,p1,s1, s2, s3); _test("-2.0 1.5 1.0", "yellow", "false", r); } }
If you choose, you can complete this shorter assignment instead of Problem b. However, notice that the max points that you can achieve is reduced. Design a class named TriangleObject that extends GeometricObject, which is provided in the project source. The TriangleObject class contains: . Three double data fields named side1, side2, and side3 each with a default value of 1.o to denote three sides of the triangle . Two constructors: o Ano-arg constructor that creates a default triangle and sets the default color and filled to "red" and True, respectively. o A constructor that creates a triangle with the specified side1, side2, and side3 and uses a single line to set the default color and ffled to "red" and True, respectively. . The accessor methods for all three data fields (getter and setter methods). o Only update the side if the new value is greater than o. . Amethod named getArea0 that returns the area of this triangle. . Amethod named getPerimeterO that returns the perimeter of this triangle. . Amethod named toString0 that returns a string description for the triangle. One formula for computing the area of a triangle is: s(sidel side2 +side3)V2 area = V5(5-side1)(s-side2Xs-side3) Override the toString0 method to return: return "Triangle: side! %.af, sidea = %.af. Side3 %.af". _... Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not. Expected results Enter three sides: 2.1 3.2 4.1 Enter the color: blue Enter a boolean value for filled: true The area is 3.32 The perimeter is 9.40 Triangle: side! = 2.10 side2 = 3.20 side3 4.10 //rounded to 2 decimal places //rounded to 2 decimal places If you choose, you can complete this shorter assignment instead of Problem b. However, notice that the max points that you can achieve is reduced. Design a class named TriangleObject that extends GeometricObject, which is provided in the project source. The TriangleObject class contains: . Three double data fields named side1, side2, and side3 each with a default value of 1.o to denote three sides of the triangle . Two constructors: o Ano-arg constructor that creates a default triangle and sets the default color and filled to "red" and True, respectively. o A constructor that creates a triangle with the specified side1, side2, and side3 and uses a single line to set the default color and ffled to "red" and True, respectively. . The accessor methods for all three data fields (getter and setter methods). o Only update the side if the new value is greater than o. . Amethod named getArea0 that returns the area of this triangle. . Amethod named getPerimeterO that returns the perimeter of this triangle. . Amethod named toString0 that returns a string description for the triangle. One formula for computing the area of a triangle is: s(sidel side2 +side3)V2 area = V5(5-side1)(s-side2Xs-side3) Override the toString0 method to return: return "Triangle: side! %.af, sidea = %.af. Side3 %.af". _... Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not. Expected results Enter three sides: 2.1 3.2 4.1 Enter the color: blue Enter a boolean value for filled: true The area is 3.32 The perimeter is 9.40 Triangle: side! = 2.10 side2 = 3.20 side3 4.10 //rounded to 2 decimal places //rounded to 2 decimal places
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started