Question

Design a Triangle class that extends the attached Shape class. Run the attached TestTriangle.java to test...

Design a Triangle class that extends the attached Shape class. Run the attached TestTriangle.java to test your Triangle class. You may not change attached Shape.java and TestTriangle.java. Submit chapter13.Triangle.java and TestTriangle.png.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.util.*;

abstract class Shape
{
abstract double perimeterTriangle();
abstract double areaTriangle();
}

class Triangle extends Shape
{
double side1,side2,side3,area,perimeter,s;

Triangle(double side1,double side2,double side3)
{
this.side1=side1;
this.side2=side2;
this.side3=side3;
}


double perimeterTriangle()
{
perimeter=side1+side2+side3;
return perimeter;
}

double areaTriangle()
{
s=perimeter/2;
area=Math.sqrt(s*(s-side1) * (s-side2) * (s-side3));
return area;
}
}

class TestTriangle
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("\nEnter 3 sides of Triangle: ");

double s1=sc.nextDouble();
double s2=sc.nextDouble();
double s3=sc.nextDouble();

Triangle tri=new Triangle(s1,s2,s3);
System.out.printf("\nPerimeter of Triangle: %.2f\n",tri.perimeterTriangle());
System.out.printf("\nArea of Triangle: %.2f\n",tri.areaTriangle());
System.out.print("\n\n");
}
}

Output:

Windows Powershell PS C:\Users\user\Desktop> javac TestTriangle.java PS C:\Users\user Desktop> java TestTriangle Enter 3 side

Add a comment
Know the answer?
Add Answer to:
Design a Triangle class that extends the attached Shape class. Run the attached TestTriangle.java to test...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class...

    (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class contains: - Three float data fields named side1, side2, and side3 to denote the three sides of the triangle. - A constructor that creates a triangle with the specified side1, side2, and side3 with default values 1.0. - The accessor methods for all three data fields. - A method named getArea() that returns the area of this triangle. - A method named getPerimeter() that...

  • Java programming 1. (Triangle class) Design a new Triangle class that extends the abstract GeometricObject class....

    Java programming 1. (Triangle class) Design a new Triangle class that extends the abstract GeometricObject class. Draw the UML diagram for the classes Triangle and GeometricObject and then implement the Triangle class. 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....

  • Introduction to Java:Design a class named Triangle that extends GeometricObject.

    Please include comments in the program .The program must be able to be compiled.Design a class named Triangle that extends GeometricObject. This class contains:* Three double data fields named side1, side2, and side3 with default values 1.0 to denote the three sides of a 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...

  • Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...

    Design a class named Triangle that extends GeometricObject class. 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 with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...

  • Given that the class Circle extends the abstract class Shape, which of the following lines would...

    Given that the class Circle extends the abstract class Shape, which of the following lines would instantiate object of these classes? Shape s = new Shape(); Circle c = new Shape(); Shape s = new Circle(); None of the answers

  • Design an Essay class that extends the GradedActivity class presented in the chapter 10 of textbook....

    Design an Essay class that extends the GradedActivity class presented in the chapter 10 of textbook. The Essay class should determine the grade a student receives for an essay. The student’s essay score can be up to 100 and is determined in the following manner:  Grammar: 30 points  Spelling: 20 points  Correct length: 20 points  Content: 30 points Demonstrate the class in a simple program. Example Run run: Term paper: Grammar points: 25.0 Spelling points: 18.0...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • OCF (Orthodox Canonical Form) Using Visual Studio, design, implement, and test an object class for a...

    OCF (Orthodox Canonical Form) Using Visual Studio, design, implement, and test an object class for a Cartesian 3-dimensional vector. Your class MUST be declared in a header (h) file, and methods implemented in a C++ (.cpp) file The origin of your vector by default is the point, (0, 0, 0) You must implement the OCF. You may implement as many preferred ctors as makes sense. You must be able to calculate and return the scalar magnitude of your vector in...

  • JAVA coding The Sorted List ADT Implement the SortedList class. The SortedList class extends the ...

    JAVA coding The Sorted List ADT Implement the SortedList class. The SortedList class extends the AbstractList class. Both can be seen here. Your assignment is to implement (recursively) all of the abstract methods of the AbstractList class. They are: insert (recursive) iterator remove (recursive) retrieve search (recursive) You must also implement an Iterator inner class for the SortedList class. You must submit a modified SortedList.java file with your source code. Do not submit and do not modify the List.java or...

  • First, design and implement a class Triangle, which includes the attributes -- sideA, sideB, and sideC;...

    First, design and implement a class Triangle, which includes the attributes -- sideA, sideB, and sideC; behaviors: regular constructor, mutators, assessors, and area(). Second, design user-defined Exception to handle the following two execution error: negative edge value the sum of two sides' length is small than that of the third side. In Java and Test please

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT