Java program to calcuate the maximium edge of triangle, Our problem statement here is, to find the longest side or edge of a triangle. For this, our required input will be, the lengths of three sides or edges of a triangle. Our expected output is, one of these three edges that is the longest of all.
Initially, to read the three edges at runtime, we can make use of the Scanner class in Java. This class reads any input from the console screen at runtime. To make use of this class, we first create an object instantiating the class and then call the necessary method to read a primitive datatype value. Here, our edges are of integer type so, we invoke the nextInt() method as shown below:
1 2 3 4 5 6 7 8 9 |
Scanner sc=new Scanner(System.in); System.out.println("Enter the 3 edges of a tringle)"); e1=sc.nextInt(); e2=sc.nextInt(); e3=sc.nextInt(); |
After reading the three edges, we need to start comparing it with each other to find the largest among them.
First we see if, edge1 (e1) is greater than edge2(e2). If yes then, we check whether edge1 (e1) is greater than edge3 (e3) too and if yes then, edge1 (e1) is the longest and we store this in a variable (max). If egde1 (e1) is not greater than edge3 (e3) then, edge3 (e3) is the longest and will be stored in the variable (max).
if(e1>=e2) {
if(e1>=e3)
max=e1;
else
max=e3;
}
Now, if edge1 (e1) is not greater than edge2 (e2) then, we check whether edge2 (e2) is greater than edge3 (e3). If yes then, edge2 (e2) is the longest and is stored in the max variable and else, edge3 (e3) is the greatest and is stored in the max variable.
else if(e2>=e3) {
max=e2;
}
else
max=e3;
Our desired output i.e., the longest edge among the three is stored in this max variable and can be displayed on the console screen by making use of System.out.println() method.
System.out.println(“The maximum edge of a trinagle of 3 edges (“+e1+” “+e2+” “+e3+”) is :”+max);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.Scanner; class MaxEdge { public static void main(String arg[]) { int e1,e2,e3,max; Scanner sc=new Scanner(System.in); System.out.println("Enter the 3 edges of a tringle)"); e1=sc.nextInt(); e2=sc.nextInt(); e3=sc.nextInt(); if(e1>=e2) { if(e1>=e3) max=e1; else max=e3; } else if(e2>=e3) { max=e2; } else max=e3; System.out.println("The maximum edge of a trinagle of 3 edges ("+e1+" "+e2+" "+e3+") is :"+max); } } |
Output:
Here, e1=4, e2=3 and e3=5,
e1>=e2 i.e., 4>=3 yes so,
e1>=e3 i.e., 4>=5 no so, max=e3=5.
1 2 3 4 5 6 |
java MaxEdge Enter the 3 edges of a tringle) 4 3 5 The maximum edge of a trinagle of 3 edges (4 3 5) is :5 |
Static Method – Edge of Triangle Java
This method also has the exact same logic and set of statements as discussed above but the difference is that, the main logic with comparison of three edges is placed within a separate static method (findMaxEdge) which takes three edges as arguments and returns the longest edge (m) to the main function.
We do this so that, we can make this part of the code reusable. If the same logic were to be used elsewhere in the code later then, instead of rewriting the same set of statements we can just make a method call and pass the arguments.
Not just that, it also enhances the readability of the code and makes it clearer. Also, for a static method, we do not need to create an object despite being outside main method. This is because, static method belongs to the class and not the instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.Scanner; class MaxEdge { public static void main(String arg[]) { int n1,n2,n3,max; Scanner sc=new Scanner(System.in); System.out.println("Enter the 3 edges of a tringle)"); n1=sc.nextInt(); n2=sc.nextInt(); n3=sc.nextInt(); max=findMaxEdge(n1,n2,n3); System.out.println("The maximum edge of a trinagle of 3 edges ("+n1+" "+n2+" "+n3+") is :"+max); } static int findMaxEdge(int s1,int s2,int s3) { int m; if(s1>=s2) { if(s1>=s3) m=s1; else m=s3; } else if(s2>=s3) { m=s2; } else m=s3; return m; } } |
Output:
s1=20, s2=20 and s3=20.
s1>=s2 i.e., 20>=20 yes so,
s1>=s3 i.e., 20>=20 yes then, m=e1=20.
1 2 3 4 5 6 |
java MaxEdge Enter the 3 edges of a tringle) 20 20 20 The maximum edge of a trinagle of 3 edges (20 20 20) is :20 |
Check : Transpose Matrix In Java