techhipbettruvabetnorabahisbahis forumutaraftarium24eduedueduedueduseduedueduedusedus Java Program : Maximum Edge of A Triangle | Java Programs
Latest :

Java Program : Maximum Edge of A Triangle | Java Programs

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:

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);

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.

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.

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.

 

Check : Transpose Matrix In Java

x

Check Also

Implement Heap Sort Java Algorithm – 2 Ways | Java Sorting

Implement Heap sort using Java – We will discuss the methods to Implement heapsort in Java. ...

atakoy escortatakoy escortonwinbakırköy escorthalkali escortstarzbetkucukcekmece escortbağcılar escortsightcarefatih escortantalya escortmaltepe escortdeneme bonusu veren sitelerescort kadıköyarnavutkoy escortbasaksehir escortistanbul, escortholiganbetromabetromabetromabetbetofficecasibombelek escortjojobet giriş토토사이트casibomholiganbetholiganbetpusulabetonwincasibommatbetvaycasinobetcioonwin girişpiabellacasino güncel giriş1xbet girişnakitbahiszbahisbahiscomotobetbetturkeybets10sheratonbetportobetcasibomaresbetgrandpashabetbetcioextrabetimajbetmavibetdamabetyeni deneme bonusu veren sitelergrandpashabetdeneme bonusu veren sitelergrandpashabetgrandpashabetvaycasinograndpashabetistanbul escortcasino siteleriDeneme Bonusu Veren SitelerDeneme Bonusu Veren SitelerAlanya EscortDeneme Bonusu Veren Sitelercasibom girişnakitbahisGrandpashabetCasibom girişbetasus güncel girişperabetsekabetholiganbet girişcasibomsekabet girişholiganbetjustin tvbetcio girişvaycasinobetciotaraftarium24selçuksportsultraslotbetebetultraslotultraslotbetpuan güncel girişgrandpashabetcasibom girişcasibomdeneme bonusu veren sitelergrandpashabetdeneme bonusu veren sitelerholiganbetbahsegelbahisslot sitelerideneme bonusu veren sitelerbahis sitelerideneme bonusutürk ifşaBetcioBetcioonwinkingroyalmobilbahiscasibomcasibom güncel girişbahiscom1xbettipobetdeneme bonusuvaycasinodeneme bonusudeneme bonusuholiganbetcasibomotobetsahabetgrandpashabetHoliganbet girişmatbet girişcasibomcasibomonwin