Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Java Program to find Area Of Trapezoid

by suresh

Write Java Program to find Area Of Trapezoid and Median of a Trapezoid with example.

Java 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 = 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, this Java program will calculate the Area of a trapezoid and Median of a Trapezoid.

// 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);
	}
}
Java Program to find Area Of Trapezoid 1

Within this Java Program to find Area Of Trapezoid, the following statement will ask the user to enter base1, base, and height values. And 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 the 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);

The following Java 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 in this Java Program to find Area Of Trapezoid 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 the first example. Still, we will separate the logic and place it in a method.

// 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);
	}
}
Java Program to find Area Of Trapezoid 2

Java Program to find Area Of Trapezoid using Oops

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

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

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 Java Program to find Area Of Trapezoid, we will create an instance of the above-specified class and call the methods.

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);
	}
}
Java Program to find Area Of Trapezoid 3

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 TrapezoidArea function with three arguments of type double. Within the function, we calculated the Area of Trapezoid, and we are returning the Value.

Java Program to find Area Of Trapezoid Main Class Analysis:

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

AreaOfaTrapezoid atz = new AreaOfaTrapezoid();

Next, we are calling the AreaOfTrapezoid method. This method will calculate Area, Median, and print the output from the AreaOfATrapezoid Class itself.

atz.AreaofTrapezoid();

Next, we are calling the TrapezoidArea method. It is the second method with a double data type, and it will calculate Area and return a value. So, we are assigning the return value to the Area variable.

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

Lastly, the following Java System.out.format statement to print the Area of a Trapezoid, which is calculated by the TrapezoidArea method.

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

Placed Under: Java Programs

  • Java Hello World Program
  • Java Add Two Numbers Program
  • Java Compound Interest Program
  • Java Number Divisible by 5 & 11
  • Java Cube of a Number Program
  • Java Print Even Numbers 1 to N
  • Java GCD of Two Numbers
  • Java LCM of Two Numbers
  • Java Largest of Two Numbers
  • Java Largest of Three Numbers
  • Java Print Multiplication Table
  • Java Odd Numbers from 1 to N
  • Java Odd Even Program
  • Java find +Ve or -Ve number
  • Java Power of a Number Program
  • Java Calculate Profit or Loss
  • Java Print 1 to 100 without Loop
  • Java Quadratic Equation roots
  • Java Square of Number Program
  • Java Simple Interest Program
  • Java Sum of Even Numbers
  • Java Sum of Odd numbers
  • Java Sum of Even & Odd Number
  • Java find Total ,Average & Percentage of 5 Subjects
  • Java ASCII Value of a Character
  • Java ASCII Value of all Characters
  • Java String Comparison program
  • Java Area of Circle Program
  • Java Area Of Triangle Program
  • Java Area Of Trapezoid Program
  • Java Area of Equilateral Triangle
  • Java Area of Rectangle Program
  • Java Area of Right angle triangle
  • Java Volume & Surface of Sphere
  • Java Volume & Surface of Cone
  • Java Volume & Surface of Cuboid
  • Java Volume & Surface of Cube
  • Java Cylinder Volume & Surface
  • Java Math Library
  • Java Armstrong Number Program
  • Java Perfect Number Program
  • Java Palindrome Program
  • Java Count Digits in a Number
  • Java Calculate Electricity Bill
  • Java Find Factors of a Number
  • Java Factorial Program
  • Java Fibonacci Series program
  • Java find First Digit of a Number
  • Java find Last Digit of a Number
  • Java First & Last Digit of Number
  • Java Generic Root of Number
  • Java Natural Numbers in Reverse
  • Java Natural Numbers from 1 to N
  • Java Find Sum of Prime Numbers
  • Java Print Prime Numbers 1 to N
  • Java Prime Number Program
  • Java Reverse a Number Program
  • Java Strong Number Program
  • Java Sum of N Natural Numbers
  • Java Sum of Digits Program
  • Java Swap Two Numbers
  • Java Leap Year Program
  • Java Number of Days in a Month
  • Java Array
  • Java Array Arithmetic Operations
  • Java Program to Copy an Array
  • Java Count Array Duplicates
  • Java count even number in array
  • Java count odd numbers in Array
  • Java Count Even & Odds in Array
  • Java Count Negative Array Items
  • Java Count Positive Array Items
  • Java Count Array +Ve & -Ve num
  • Java Delete Array Duplicates
  • Java Large & Small Array Item
  • Java Largest Array Number
  • Java Print Array Elements
  • Java Print Positive Array Items
  • Java Print Negative Array Items
  • Java Smallest Array Number
  • Java Second Largest Array Item
  • Java Print Unique Array Items
  • Java +Ve & -Ve in separate array
  • Java Sort Array in Descending
  • Java Sort Array in Ascending
  • Java Sum of Array Elements
  • Java Sum of Array Even Numbers
  • Java Sum of Array Odd Numbers
  • Java Sum of Array Even and Odd
  • Java Swap Arrays without temp
  • Java Matrix Introduction
  • Java Print Matrix items
  • Java Check 2 Matrices are Equal
  • Java Matrix Arithmetic operation
  • Java Matrix Addition
  • Java Matrix Multiplication
  • Java Find Matrix Determinant
  • Java Identity Matrix
  • Java Interchange Matrix diagonal
  • Java Matrix Lower Triangle
  • Java Matrix Lower Triangle Sum
  • Java Matrix Upper Triangle
  • Java Matrix Upper Triangle Sum
  • Java Scalar Matrix Multiplication
  • Java Sparse Matrix
  • Java Sum of Matrix Diagonal
  • Java Sum of Matrix Opp Diagonal
  • Java Sum of each Matrix Row
  • Java Sum of each Matrix Column
  • Java Sum of Matrix row & column
  • Java Symmetric Matrix
  • Java Transpose Matrix
  • Java Rectangle Star Pattern
  • Java Square Star Pattern
  • Java Hollow Box Number Pattern
  • Java Box Number Pattern of 1, 0
  • Java Square Number Pattern
  • Java Print Floyd’s Triangle
  • Java Print String Characters
  • Java String replace 1st Char Occ
  • Java String replace last Char occ
  • Java String remove 1st Char Occ
  • Java string remove last Char Occ
  • Java String remove 1st, last char
  • Java String remove all char occur
  • Java String Find 1st Char Occur
  • Java String find all char occur
  • Java String Find Last Char Occur
  • Java String Print 1st & last Char
  • Java String Chars ASCII values
  • Java Program to Concat Strings
  • Java String Characters count
  • Java Count Total Char Occur
  • Java Max Occur String Character
  • Java Toggle String Characters
  • Java Frequency of each Char
  • Java Count Vowels & Consonants
  • Java Count alph digits, special
  • Java Palindrome String
  • Java Program to Reverse a String
  • Java Min Occur String character
  • Java Convert Lower to Upper
  • Java Convert Upper to Lower
  • Java Count total String words

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy