Question

Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3:...

Welcome to the Triangles program

Enter length 1: 3

Enter length 2: 5

Enter length 3: 5

Enter the base: 5

Enter the height: 8

---------------------

The triangle is Isosceles since it has two equal length sides.
Isosceles triangle also has two equal angles

The area is: 20

The permimeter is: 13

Continue? (y/n): y

Enter length 1: 10

Enter length 2: 10

Enter length 3: 10

Enter the base: 10

Enter the height: 7

---------------------

The triangle is Equilateral since it has equal length on all three sides.
All Equilateral triangle are equiangular triangles.

The area is: 35

The permimeter is: 30

Continue? (y/n): y

Enter length 1: 10

Enter length 2: 3

Enter length 3: 9

Enter the base: 10

Enter the height: 7

---------------------

The triangle is Scalene with all no equal side.
Scalene triangles have no equal angles.

The area is: 35

The permimeter is: 22

Continue? (y/n): n

Goodbye.

------------------------------------------------

Requirements:

  • Use a main class for accepting inputs and validating inputs
    • Create an object of type class triangle
    • Prompt the user to enter the three sides of a triangle.
    • Prompt the user for the base and the height
    • Accept decimal values
    • Validate input using try-catch and also nested-if statements as needed
    • If the user enters invalid data, the application displays an appropriate error message and prompts the user again until the user enters valid data.
      • Use a method for evaluating what type of triangle it is (call the method via the object, NO STATIC METHOD)
      • Use a method for calculating the area = (base * height)/2 (call the method via the object, NO STATIC METHOD)
      • Use a method for calculating the perimeter = (length1 + length2 + length3) (call the method via the object, NO STATIC METHOD)
  • After displaying all the triangle information, the application prompts the user to continue.
  • When the user chooses not to continue, the application displays a goodbye message

DBPM Template

**DBPM TEMPLATE **

package triangles;
import java.util.Scanner;
public class Triangles
{
private double area;
private double permimeter;

//CREATE A DEFAULT CONSTRUCTOR TO INITIALIZE area and permimeter

//CREATE THE TWO SETTER METHODS FOR area and permimeter

//CREATE THE TWO GETTER METHODS FOR area and permimeter

public void triangleType(double side1, double side2, double side3)
{
if (side1 == side2 && side1 == side3) {
System.out.println("The triangle is Equilateral since it has equal length on all three sides.");
System.out.println("All Equilateral triangle are equiangular triangles.");

} else {
if (side1 == side2 && side1 != side3 || side1 != side2 && side2 == side3 || side1 == side3 && side1 != side2) {
System.out.println("The triangle is Isosceles since it has two equal length sides.");
System.out.println("Isosceles triangle also has two equal angles");

} else {
if (side1 != side2 && side1 != side3) {
System.out.println("The triangle is Scalene with all no equal side.");
System.out.println("Scalene triangles have no equal angles.");

}
}
}
}
//WRITE THE AREA METHOD WITH TWO DOUBLE PARAMETERS WITH A VOID RETURN TYPE
{
// CALCULATE THE AREA (this.area)
System.out.println(this.area);
}

//WRITE THE PERMIMETER METHOD WITH THREE DOUBLE PARAMETERS WITH A VOID RETURN TYPE
{
// CALCULATE THE PERMIMETER (this.permimeter)
System.out.println(this.permimeter);
}

public static void main(String[] args)
{
System.out.println("Welcome to the triangles program");
Scanner key = new Scanner(System.in);
int x = 1;
String y = "y";

//CREATE A TRIANGLES OBJECT CALLED triangleObj

do {
try {
while ("y".equalsIgnoreCase(y)) {
x = 1;
System.out.print("Enter length 1: ");
double side1 = key.nextDouble();

System.out.print("Enter length 2: ");
double side2 = key.nextDouble();

System.out.print("Enter length 3: ");
double side3 = key.nextDouble();

System.out.println(" ");

System.out.print("Enter the Base: ");
double base = key.nextDouble();

System.out.print("Enter the Height: ");
double height = key.nextDouble();

System.out.println(" ");
System.out.println("---------------------");
System.out.println(" ");
triangleObj.triangleType(side1, side2, side3);
triangleObj.area(base, height);
triangleObj.permimeter(side1, side2, side3);
x = 2;

System.out.print("Continue? Y/N: ");
y = key.nextLine();
y = key.nextLine();
}
}
//CREATE THE CATCH CLAUSE WITH Exception e
{
System.out.println("Please enter a valid number.");
key.next();
}
} while (x == 1);
}
}

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

Note: Done accordingly. Please comment for any further help. Please give thumbs up.

Code:

import java.util.Scanner;

public class Triangles {
private double area;
private double perimeter;

// CREATE A DEFAULT CONSTRUCTOR TO INITIALIZE area and permimeter
Triangles() {
this.area = 0;
this.perimeter = 0;
}
// CREATE THE TWO SETTER METHODS FOR area and permimeter
void setArea(double area) {
this.area = area;
}

void setPerimeter(double perimeter) {
this.perimeter = perimeter;
}
// CREATE THE TWO GETTER METHODS FOR area and permimeter
double getPerimeter() {
return this.perimeter;
}

double getArea() {
return this.area;
}

public void triangleType(double side1, double side2, double side3) {
if (side1 == side2 && side1 == side3) {
System.out.println("The triangle is Equilateral since it has equal length on all three sides.");
System.out.println("All Equilateral triangle are equiangular triangles.");

} else {
if (side1 == side2 && side1 != side3 || side1 != side2 && side2 == side3 ||
side1 == side3 && side1 != side2) {
System.out.println("The triangle is Isosceles since it has two equal length sides.");
System.out.println("Isosceles triangle also has two equal angles");

} else {
if (side1 != side2 && side1 != side3) {
System.out.println("The triangle is Scalene with all no equal side.");
System.out.println("Scalene triangles have no equal angles.");
}
}
}
}

// WRITE THE AREA METHOD WITH TWO DOUBLE PARAMETERS WITH A VOID RETURN TYPE
void area(double base, double height) {
this.area = (1 / 2.0) * base * height;
}

// WRITE THE PERMIMETER METHOD WITH THREE DOUBLE PARAMETERS WITH A VOID
// RETURN TYPE
void perimeter(double side1, double side2, double side3) {
this.perimeter = side1 + side2 + side3;
}

public static void main(String[] args) {
System.out.println("Welcome to the triangles program");
Scanner key = new Scanner(System.in);
int x = 1;
String y = "y";

//CREATE A TRIANGLES OBJECT CALLED triangleObj
while ("Y".equalsIgnoreCase(y)) {
Triangles triangleObj = new Triangles();
try {
System.out.print("Enter length 1: ");
double side1 = key.nextDouble();

System.out.print("Enter length 2: ");
double side2 = key.nextDouble();

System.out.print("Enter length 3: ");
double side3 = key.nextDouble();

System.out.println(" ");

System.out.print("Enter the Base: ");
double base = key.nextDouble();

System.out.print("Enter the Height: ");
double height = key.nextDouble();

System.out.println(" ");
System.out.println("---------------------");
System.out.println(" ");
triangleObj.triangleType(side1, side2, side3);
triangleObj.area(base, height);
triangleObj.perimeter(side1, side2, side3);
System.out.println("The area is: "+triangleObj.area);
System.out.println("The perimeter is: "+triangleObj.perimeter);
  
System.out.print("Continue? Y/N: ");
key.nextLine();//to clear buffer
y = key.nextLine();
}
//CREATE THE CATCH CLAUSE WITH Exception e
catch (Exception e) {
System.out.println("Please enter a valid number.");
key.nextLine();//to clear buffer
System.out.print("Continue? Y/N: ");
y = key.nextLine();
}
}
System.out.println("Goodbye.");
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3:...
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
  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • Why is my program returning 0 for the area of a triangle? public class GeometricObjects {...

    Why is my program returning 0 for the area of a triangle? public class GeometricObjects {    private String color = " white ";     private boolean filled;     private java.util.Date dateCreated;     public GeometricObjects() {         dateCreated = new java.util.Date();     }     public GeometricObjects(String color, boolean filled) {         dateCreated = new java.util.Date();         this.color = color;         this.filled = filled;     }     public String getColor() {         return color;     }     public void setColor(String color)...

  • Your math professor asked you to determine whether triangles are equilateral, isosceles, or scalene triangles. You...

    Your math professor asked you to determine whether triangles are equilateral, isosceles, or scalene triangles. You need to write a MATLAB program that accepts the side lengths for sides A, B, and C of a triangle and compares the side lengths in order to determine if the triangle is equilateral, isosceles, or scalene. Each triangle will only be classified as a single type based on the definitions below: 1. An equilateral triangle is a triangle in which all three sides...

  • JAVA question Interface and Abstract Class (20 pts): There are three types of triangles in terms...

    JAVA question Interface and Abstract Class (20 pts): There are three types of triangles in terms of how many sides are equal: • Equilateral Isosceles Scalene There are three types of triangles in terms of the degrees of interior angles: • Right · Acute . Obtuse Right Isosceles triangle is a right triangle, as well as an isosceles triangle. Triangle 590° Right triangle Isosceles triangle Right isosceles triangle Create a public interface Triangle, add the following method signatures: • double...

  • JAVA Code Requried Copy the file java included below. This program will compile, but, when you...

    JAVA Code Requried Copy the file java included below. This program will compile, but, when you run it, it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Below the main method, but in the Geometry class, create a static method called printMenu that has no parameter list and does not return a value. It will...

  • (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...

  • (Need to complete the methods and pass a Junit test i can email them to you.)...

    (Need to complete the methods and pass a Junit test i can email them to you.) public class Triangle implements Comparable {    /**    * Stores the number of objects instantiated from the {@code Triangle} class    */    private static int numTriangles;    /**    * The shortest side of the triangle    */    private final double a;    /**    * The side of medium length of the triangle    */    private final double b;...

  • Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task...

    Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task Create the following fields and methods for a Triangle class that extends the provided GeometricShapes class public class GeometricShapes { private String color = "red"; private boolean filled; private java.util.Date dateCreated;    public GeometricShapes() { dateCreated = new java.util.Date(); }    public GeometricShapes(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; }    public void setColor (String color)...

  • Project Objectives: To develop ability to write void and value returning methods and to call them...

    Project Objectives: To develop ability to write void and value returning methods and to call them -- Introduction: Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct order. This also allows for efficiencies, since the method can...

  • Design a general class GeometricObject can be used to model all geometric objects. This class contains...

    Design a general class GeometricObject can be used to model all geometric objects. This class contains the properties color and filled and their appropriate get and set methods. Assume that this class also contains toString() methods. The toString() method returns a string representation of the object. Define the Triangle, Circle, and Rectangle classes that extend the GeometricObject class. The Triangle class inherits all accessible data fields and methods from the GeometricObject class.In addition, it has three double data fields named...

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