11.1 (The Triangle class) Design a class named Triangle that extends
GeometricObject. The class contains:
■ Three double data fields named side1, side2, and side3 with default
values 1.0 to denote three sides of the triangle.
■ A no-arg constructor that creates a default triangle.
■ A constructor that creates a triangle with the specified side1, side2, and
side3.
■ The accessor methods for all three data fields.
■ A method named getArea() that returns the area of this triangle.
■ A method named getPerimeter() that returns the perimeter of this triangle.
■ A method named toString() that returns a string description for the triangle.
For the formula to compute the area of a triangle, see Programming Exercise 2.19.
The toString() method is implemented as follows:
return "Triangle: side1 = " + side1 + " side2 = " + side2 +
" side3 = " + side3;
Draw the UML diagrams for the classes Triangle and GeometricObject and
implement the classes. 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.
Sections 11.5–11.14
GeometricObject. The class contains:
■ Three double data fields named side1, side2, and side3 with default
values 1.0 to denote three sides of the triangle.
■ A no-arg constructor that creates a default triangle.
■ A constructor that creates a triangle with the specified side1, side2, and
side3.
■ The accessor methods for all three data fields.
■ A method named getArea() that returns the area of this triangle.
■ A method named getPerimeter() that returns the perimeter of this triangle.
■ A method named toString() that returns a string description for the triangle.
For the formula to compute the area of a triangle, see Programming Exercise 2.19.
The toString() method is implemented as follows:
return "Triangle: side1 = " + side1 + " side2 = " + side2 +
" side3 = " + side3;
Draw the UML diagrams for the classes Triangle and GeometricObject and
implement the classes. 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.
Sections 11.5–11.14
public class Triangle extends GeometricObject { private double side1; private double side2; private double side3; public Triangle(double side1, double side2, double side3) throws IllegalTriangleException { this.side1 = side1; this.side2 = side2; this.side3 = side3; isValidTriangle(); } public Triangle() { this.side1 = 1; this.side2 = 1; this.side3 = 1; } @Override public double getArea() { double s = (side1 + side2 + side3) / 2.0; return Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5); } @Override public double getPerimeter() { return side1 + side2 + side3; } @Override public String toString() { return "Triangle{" + "side1=" + side1 + ", side2=" + side2 + ", side3=" + side3 + '}'; } public static boolean isTriangle(double side1, double side2, double side3) { return ((side1 + side2 > side3) && (side1 + side3 > side2) && (side3 + side2 > side1)); } public double getSide1() { return side1; } public void setSide1(double side1) throws IllegalTriangleException { this.side1 = side1; isValidTriangle(); } public double getSide2() { return side2; } public void setSide2(double side2) throws IllegalTriangleException{ this.side2 = side2; isValidTriangle(); } public double getSide3() { return side3; } public void setSide3(double side3) throws IllegalTriangleException { this.side3 = side3; isValidTriangle(); } private void isValidTriangle() throws IllegalTriangleException{ if (!isTriangle(side1, side2, side3)) { throw new IllegalTriangleException(side1, side2, side3); } } public class IllegalTriangleException extends IllegalArgumentException { private double s1; private double s2; private double s3; public IllegalTriangleException(double s1, double s2, double s3) { super("Not a real triangle:" + " side1 = " + s1 + " side2 = " + s2 + " side3 = " + s3); this.s1 = s1; this.s2 = s2; this.s3 = s3; } public double getS1() { return s1; } public double getS2() { return s2; } public double getS3() { return s3; } } }
import java.util.Scanner; public class Exercise_01 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter three sides of a triangle: "); double[] sides = new double[3]; for (int i = 0; i < sides.length; i++) sides[i] = input.nextDouble(); System.out.print("Enter a triangle color: "); String color = input.next(); System.out.print("Is the triangle filled? true/false: "); String isFilledString = input.next(); boolean isFilled = (isFilledString.equals("true")); Triangle t1 = null; try { t1 = new Triangle(sides[0], sides[1], sides[2]); t1.setColor(color); t1.setFilled(isFilled); System.out.println("Triangle 1:"); System.out.println("Area = " + t1.getArea()); System.out.println("Perimeter = " + t1.getPerimeter()); System.out.println("Color = " + t1.getColor()); System.out.println("Is filled? " + t1.isFilled()); } catch (IllegalTriangleException e) { e.printStackTrace(); } } }
No comments :
Post a Comment