In this article we will show you, How to write Java Program to find Area Of Trapezoid and Median of a Trapezoid with example. Before we step into practical example, Let see the definitions and formulas behind the Median and Area of a Trapezoid

## Area of a Trapezoid

- If we know the height and two base lengths then we can calculate the Area of a Trapezoid using the formula: Area = (a + b)/2 * h. Here a and b are the two bases and h is the height of the Trapezoid
- We can calculate the median of a Trapezoid using the formula: Median = (a+b) / 2.
- If we know the Median and height then we can calculate the Area of a Trapezoid as: Area = median * height

## Java Program to find Area Of Trapezoid

This Java program allows the user to enter both sides and height of the Trapezoid. Using those values we will calculate the Area of a trapezoid and Median of a Trapezoid.

**JAVA CODE**

// Java Program to find Area Of Trapezoid package Area; import java.util.Scanner; public class AreaOfTrapezoid { private static Scanner sc; public static void main(String[] args) { double base1, base2, height, Area, Median; sc = new Scanner(System.in); System.out.println(" Please Enter First Base of a Trapezoid = "); base1 = sc.nextDouble(); System.out.println(" Please Enter Second Base of a Trapezoid = "); base2 = sc.nextDouble(); System.out.println(" Please Enter the Height of a Trapezoid = "); height = sc.nextDouble(); Area = 0.5 * (base1 + base2) * height; Median = 0.5 * (base1+ base2); System.out.format("\n The Area of a Trapezoid = %.2f\n",Area); System.out.format(" The Median of a trapezium = %.2f \n", Median); } }

**OUTPUT**

**ANALYSIS**

Following statement will ask the user to enter base1, base and height values and they will assign the user input values to respected variables. Such as first value will be assigned to base1, second value to base2 and third value to height

System.out.println(" Please Enter First Base of a Trapezoid = "); base1 = sc.nextDouble(); System.out.println(" Please Enter Second Base of a Trapezoid = "); base2 = sc.nextDouble(); System.out.println(" Please Enter the Height of a Trapezoid = "); height = sc.nextDouble();

Next, we are using the Mathematical Formula to calculate the area of trapezoid

Area = 0.5 * (base1 + base2) * height;

In the next line, We are calculating the Median of the trapezoid

Median = 0.5 * (base1+ base2);

Following System.out.format statements will help us to print the Perimeter and Area of a rectangle

System.out.format("\n The Area of a Trapezoid = %.2f\n",Area); System.out.format(" The Median of a trapezium = %.2f \n", Median);

User Entered Values are base1 = 8, base2 = 5 and height = 9

Area of a Trapezoid = 0.5 * (base1 + base2) * height;

Area of a Trapezoid = 0.5 * (8 + 5) * 9;

Area of a Trapezoid = 0.5 * 13 * 9;

Area of a Trapezoid = 58.5

Median of a Trapezoid = 0.5 * (base1+ base2);

Median of a Trapezoid = 0.5 * (8 + 5)

Median of a Trapezoid = 0.5 * 13

Median of a Trapezoid = 6.5

## Java Program to find Area Of Trapezoid using Functions

This Java program allows the user to enter both sides and height of the Trapezoid. In this example, we are going to use the logic that we specified in first example but we will separated the logic and place it in a method.

**JAVA CODE**

// Java Program to find Area Of Trapezoid using Functions package Area; import java.util.Scanner; public class AreaOfTrapezoidUsingMethod { private static Scanner sc; public static void main(String[] args) { double base1, base2, height; sc = new Scanner(System.in); System.out.println(" Please Enter First Base1, Base2 and Height of Trapezoid = "); base1 = sc.nextDouble(); base2 = sc.nextDouble(); height = sc.nextDouble(); AreaofRectangle(base1, base2, height); } public static void AreaofRectangle( double base1, double base2, double height ) { double Area, Median; Area = 0.5 * (base1 + base2) * height; Median = 0.5 * (base1 + base2); System.out.format("\n The Area of a Trapezoid = %.2f\n",Area); System.out.format(" The Median of a trapezium = %.2f \n", Median); } }

**OUTPUT**

## Java Program to find Area Of Trapezoid using Oops

In this Java program, we are dividing the code using the Object Oriented Programming. To do this, First we will create a class which holds a methods.

**TIP:** In general, You don’t have to write first method. We used this method to show the available options

**JAVA CODE**

package Area; public class AreaOfaTrapezoid { double Area, Median, a, b, h; public void AreaofTrapezoid( ) { Area = 0.5 * (a + b) * h; Median = 0.5 * (a + b); System.out.format("\n The Area of a Trapezoid = %.2f\n",Area); System.out.format(" The Median of a trapezium = %.2f \n", Median); } public double TrapezoidArea( double base1, double base2, double height ) { Area = 0.5 * (base1 + base2) * height; return Area; } }

Within the Main program, we will create an instance of the above specified class and call the methods.

**JAVA CODE**

package Area; import java.util.Scanner; public class AreaOfTrapezoidUsingClass { private static Scanner sc; public static void main(String[] args) { double base1, base2, height, Area; sc = new Scanner(System.in); System.out.println(" Please Enter First Base1, Base2 and Height of Trapezoid = "); base1 = sc.nextDouble(); base2 = sc.nextDouble(); height = sc.nextDouble(); AreaOfaTrapezoid atz = new AreaOfaTrapezoid(); atz.a = base1; atz.b = base2; atz.h = height; atz.AreaofTrapezoid(); Area = atz.TrapezoidArea(base1, base2, height); System.out.format("\n Second Method: The Area of a Trapezoid = %.2f\n", Area); } }

**OUTPUT**

**ANALYSIS**

**AreaOfATrapezoid Class Analysis:**

- First we declared a function
**AreaofTrapezoid**with zero arguments. Within the function, we are calculating the Area and Median of Trapezoid using respective mathematical formulas. Within the function, we used the**System.out.println**statement to print the output. - Next we declared a
**Trapezoid****Area**function with three arguments of type double. Within the function, calculating the Area of Trapezoid and we are returning the Value.

**TIP:** If you declare a method with void keyword then we can’t return any value. If you want to return any value then replace void with data type and add return keyword.

**Main Class Analysis:**

First we created an instance / created an Object of the **AreaOfATrapezoid** Class

AreaOfaTrapezoid atz = new AreaOfaTrapezoid();

Next, we are calling the **AreaOfTrapezoid** method. Note, this is the first method that we created with void keyword and this method will calculate Area, Median and print the output from the **AreaOfATrapezoid** Class itself.

atz.AreaofTrapezoid();

Next, we are calling the **Trapezoid****Area** method. This is the second method that we created with double data type and this method will calculate Area and return a value so, we are assigning the return value to Area variable.

Area = atz.TrapezoidArea(base1, base2, height);

Lastly we used following System.out.format statement to print the Area of a Trapezoid which is calculated by **Trapezoid****Area** method

System.out.format("\n Second Method: The Area of a Trapezoid = %.2f\n", Area);

Thank You for Visiting Our Blog