JavaRush /Java Blog /Random EN /Coffee break #170. We are writing a Java program to check...

Coffee break #170. We are writing a Java program to check for a right triangle. Creating your own sorting algorithm

Published in the Random EN group

Java program to check right triangle

Source: Mayankvikash This article demonstrates the development of a Java program to test a triangle to see if it has a right angle. A right triangle is a triangle with one of its interior angles measuring 90 degrees. There are several properties of a right triangle; one of them is that the square of the hypotenuse is equal to the sum of the square of the legs (perpendicular and base of the triangle). This is called the Pythagorean theorem. The hypotenuse is the longest side of a triangle. Coffee break #170.  We are writing a Java program to check for a right triangle.  Create your own sorting algorithm - 1

Program logic

Since the square of the hypotenuse is equal to the sum of the squares of the other two sides, we need to calculate this sum. And if it is equal to the square of the hypotenuse, then the triangle is right-angled. Screenshot of the program: Coffee break #170.  We are writing a Java program to check for a right triangle.  Creating your own sorting algorithm - 2Now let's write the program. Here is its basic structure:
import java.util*;
public class RightAngledTriangle{
  public static void main(String args[]){
    Scanner in = new Scanner(System.in);
    // code
  }
}
Declaration of variables:
int h, p, b;
User prompt for input:
System.out.println("Enter the Hypotenuse");
h = in.nextInt();
System.out.println("Enter the Perpendicular");
p = in.nextInt();
System.out.println("Enter the Base");
b = in.nextInt();
A simple way to check whether the square of the hypotenuse is equal to the sum of the squares of the perpendicular and the base is to use if-else . If-else condition :
if (h*h==(p*p)+(b*b)){
    System.out.println("Right Angled Triangle");
}
else{
    System.out.println("Not a right angled Traingle");
}

Code

import java.util.*;
public class RightAngledTriangle {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int h, p, b;
        System.out.println("Enter the Hypotenuse");
        h = in.nextInt();
        System.out.println("Enter the Perpendicular");
        p = in.nextInt();
        System.out.println("Enter the Base");
        b = in.nextInt();

        if (h*h==(p*p)+(b*b)){
            System.out.println("Right Angled Triangle");
        }
        else{
            System.out.println("Not a right angled Traingle");
        }

    }
}

Conclusion

Non-right triangle: Coffee break #170.  We are writing a Java program to check for a right triangle.  Creating your own sorting algorithm - 3Right triangle: Coffee break #170.  We are writing a Java program to check for a right triangle.  Creating your own sorting algorithm - 4

Creating your own sorting algorithm

Source: Medium With this tutorial, you'll learn how to sort not only in ascending or descending order, but also based on your own parameters. Coffee break #170.  We are writing a Java program to check for a right triangle.  Creating your own sorting algorithm - 5Usually in Java or any other programming language we sort in ascending or descending order. But can we sort based on our own parameter? To find out, let's first sort in the usual way - in ascending or descending order.
//Например, у нас есть массив
Integer array[]={9,4,8,5,7,6,1,2,3};

//Сортируем по возрастанию
Arrays.sort(array);

//печатаем отсортированный массив
System.out.println(Arrays.toString(array));

//Сортируем по убыванию
Arrays.sort(array, Collections.reverseOrder());

//печатаем отсортированный массив
System.out.println(Arrays.toString(array));
Exit:
[1,2,3,4,5,6,7,8,9] [9,8,7,6,5,4,3,2,1]
Arrays.sort(array) is a collections method in Java to sort an array. If it is used without a second parameter, then the sorting occurs in ascending order. In the second parameter we can define Collections.reverseOrder() to sort in descending order. But we need to create our own sorting algorithm in the second parameter. How to do it? Suppose we need to order the numbers in an array in such a way that when we combine them we get a number that is the largest of all the numbers, with the condition that only the position of the numbers can be changed, and not the digits in that number. For example: {1,2,3,4}=>{4321} {12,34}=>{3412} {30,3}=>{330} {8,89}=>{898} There is a class in Java Comparator in which we must overload the comparison function. The way it works is that if two elements appear in the order {item1, item2} then they will be accepted by the comparator function like this:
public int compare(item 1, item2){

}
If the function outputs a positive value, the sort function will swap the elements. That is, the output in the collection will be {item2,item1} . If the function outputs a negative or zero result, then the order will not change, the output will remain the same: {item1,item2} . This logic applies recursively to all members of the array. So, going back to the original question, we'll define our logic in the comparator class by overloading the compare() function like this:
Collections.sort(A,new Comparator<Integer>(){

public int compare(Integer a,Integer b){
String a1=Integer.toString(a)+Integer.toString(b);
String a2=Integer.toString(b)+Integer.toString(a);
int l =a1.length();
for(int i=0;i<l;i++){

if(a1.charAt(i)>a2.charAt(i)){
return -1;
}

else if(a1.charAt(i)<a2.charAt(i)){
return 1;

}

else continue;

}

return 0;

}
Here we have combined two numbers and checked which one is greater. This logic is recursively applied to all other numbers in the array to produce the desired result. To summarize, use the Comparator class to overload the compare() function . This will help you create your own sorting logic and return a positive value when changing the order of appearance in the array, and a negative value or 0 when maintaining the order.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION