Ask Question

1 Write a java program which calculates the area of either a Triangle or a Rectangle. Use Scanner to take an input field to tell you if the area to be calculated is for a Triangle or is for a Rectangle. Depending on that input - the program must call either the Triangle method or the Rectangle method. Each of those methods must receive 2 input fields from the user - one for base and one for height in order to calculate the area and then to print that area and the height and base from inside the method.

+3
Answers (1)
  1. 26 April, 10:27
    0
    A Java program which calculates the area of either a Triangle or a Rectangle. Use Scanner to take an input field to tell you if the area to be calculated is for a Triangle or is for a Rectangle.

    Explanation:

    Use Scanner to take an input field to tell you if the area to be calculated is for a Triangle or is for a Rectangle.

    Depending on that input - the program must call either the Triangle method or the Rectangle method.

    import java. util. Scanner;

    class AreaOfRectangle {

    public static void main (String[] args)

    {

    Scanner scanner = new Scanner (System. in);

    System. out. println ("Enter the length of Rectangle:");

    double length = scanner. nextDouble ();

    System. out. println ("Enter the width of Rectangle:");

    double width = scanner. nextDouble ();

    //Area = length*width;

    double area = length*width;

    System. out. println ("Area of Rectangle is:"+area);

    }

    }

    class AreaOfTriangle

    {

    public static void main (String args[])

    {

    Scanner s = new Scanner (System. in);

    System. out. println ("Enter the width of the Triangle:");

    double b = s. nextDouble ();

    System. out. println ("Enter the height of the Triangle:");

    double h = s. nextDouble ();

    //Area = (width*height) / 2

    double area = (b*h) / 2;

    System. out. println ("Area of Triangle is: " + area);

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “1 Write a java program which calculates the area of either a Triangle or a Rectangle. Use Scanner to take an input field to tell you if the ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers