In the previous article, we have seen Java Program to Find All Possible Coordinates of Parallelogram
In this article we are going to see how to find the max area of a quadrilateral using Java programming language.
Java Program to Find Maximum Area of Quadrilateral
Before Jumping into the program directly let’s see how we can find the max area of a quadrilateral.
Explanation:
Formula to find max area of a quadrilateral = Sqrt of ((s-a)*(s-b)*(s-c)*(s-d)) Where 's' is the semi perimeter of the quadrilateral i.e. = (a+b+c+d)/2
Where,
- s represents semi perimeter of the quadrilateral.
- a, b, c, d represents 4 sides of quadrilateral.
Example:
a = 1
b = 2
c = 1
d = 2
Semi perimeter i.e. s = (a+b+c+d)/2
=> (1+2+1+2)/2
=> 6/2 = 3
Max area of a quadrilateral = Sqrt of ((s-a)*(s-b)*(s-c)*(s-d))
=> Sqrt of ((3-1)*(3-2)*(3-1)*(3-2))
=> Sqrt of (2*1*2*1)
=> Sqrt of (4) = 2
Let’s see different ways to find the max area of a quadrilateral.
Method-1: Java Program to Find Maximum Area of Quadrilateral By Using Static Value
Approach:
- Declare four integer variables say “a”, “b”, “c”, “d” and assign the values to it, which holds the values for the sides of a quadrilateral.
- Find the max area of quadrilateral using the formula
Sqrt of ((s-a)*(s-b)*(s-c)*(s-d))
- Print the result.
Program:
import java.io.*; class Main { public static void main(String[] args) { //values for the 4 sides are declared int a = 2; int b = 3; int c = 2; int d = 3; //find semi perimeter int s = (a+b+c+d)/2; //find max area double Maxarea = Math.sqrt((s-a)*(s-b)*(s-c)*(s-d)); System.out.println("The max area of quadrilateral is: " + Maxarea); } }
Output: The max area of quadrilateral is: 6.0
Method-2: Java Program to Find Maximum Area of Quadrilateral By Using User Input Value
Approach:
- Declare four integer variables say “a”, “b”, “c”, “d” and take the values as user input, which holds the values for the sides of a quadrilateral.
- Find the max area of quadrilateral using the formula
Sqrt of ((s-a)*(s-b)*(s-c)*(s-d))
- Print the result.
Program:
import java.util.*; class Main { public static void main(String[] args) { //Scanner class object created Scanner sc = new Scanner(System.in); System.out.println("Enter the value of side “a” of the quadrilateral:"); int a = sc.nextInt(); System.out.println("Enter the value of side “b” of the quadrilateral:"); int b = sc.nextInt(); System.out.println("Enter the value of side “c” of the quadrilateral:"); int c = sc.nextInt(); System.out.println("Enter the value of side “d” of the quadrilateral:"); int d = sc.nextInt(); //find semi perimeter int s = (a+b+c+d)/2; //find max area double Maxarea = Math.sqrt((s-a)*(s-b)*(s-c)*(s-d)); System.out.println("The max area of quadrilateral is: " + Maxarea); } }
Output: Enter the value of side “a” of the quadrilateral: 2 Enter the value of side “b” of the quadrilateral: 3 Enter the value of side “c” of the quadrilateral: 2 Enter the value of side “d” of the quadrilateral: 3 The max area of quadrilateral is: 6.0
Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.
Related Java Articles: