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)...

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

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

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

  • (JAVA) Implement a Triangle class. Any triangle can be represented by its THREE sides. Therefore,...

    (JAVA) Implement a Triangle class. Any triangle can be represented by its THREE sides. Therefore, your class will have THREE private member variables → side1, side2 and side3. Use the double data type to represent the triangle sides. In addition, please provide public methods that perform the following FIVE tasks: ▪ An input method that obtains the appropriate values for the three sides from the user. While entering the values of the three sides, please remember the triangle property that...

  • PLEASE READ AND CODE IN BASIC C++ CODE. ALSO, PLEASE CODE USING THIS DO WHILE LOOP...

    PLEASE READ AND CODE IN BASIC C++ CODE. ALSO, PLEASE CODE USING THIS DO WHILE LOOP AND FORMAT: #include <iostream> using namespace std; int main() { //variables here    do { // program here             }while (true);    } Objectives To learn to code, compile and run a program containing SELECTION structures Assignment Write an interactive C++.program to have a user input the length of three sides of a triangle. Allow the user to enter the sides...

  • Program Requirements ·         The program prompts the user to enter a loan amount and an interest rate....

    Program Requirements ·         The program prompts the user to enter a loan amount and an interest rate. ·         The application calculates the interest amount and formats the loan amount, interest rate, and interest amount. Then, it displays the formatted results to the user. ·         The application prompts the user to continue. ·         The program stops prompting the user for values after taking 3 loan amounts. ·         The application calculates and displays the total loan and interest amount and ends the program Example Output Welcome to...

  • Modify the Triangle class (from previous code, will post under this) to throw an exception in...

    Modify the Triangle class (from previous code, will post under this) to throw an exception in the constructor and set routines if the triangle is not valid (i.e., does not satisfy the triangle inequality). Modify the main routine to prompt the user for the sides of the triangle and to catch the exception and re-prompt the user for valid triangle sides. In addition, for any valid triangle supply a method to compute and display the area of the triangle using...

  • // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public...

    // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public class String { public static void main(String[] args) { double Sside, Rlength, Rwidth, Tbase, Theight, Area, Tarea, Rarea; String input; Scanner keyboard = new Scanner(System.in); System.out.print("To Calculate the are of Square Enter 'Square', For Rectangle Enter 'Rectangle', For Triangle Enter 'Triangle'"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("SQUARE")) { System.out.println("Square Side"); Sside = keyboard.nextInt(); Tarea = Sside * Sside; System.out.println("Side = " + Tarea ); }...

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