w3resource

Java: Reads the two adjoined sides and the diagonal of a parallelogram and check whether the parallelogram is a rectangle or a rhombus

Java Basic: Exercise-229 with Solution

Check Rectangle or Rhombus from Parallelogram Sides

Write a Java program that reads the two adjoining sides and the diagonal of a parallelogram. It will check whether the parallelogram is a rectangle or a rhombus.

According to Wikipedia-
parallelograms: In Euclidean geometry, a parallelogram is a simple (non-self-intersecting) quadrilateral with two pairs of parallel sides. The opposite or facing sides of a parallelogram are of equal length and the opposite angles of a parallelogram are of equal measure.
rectangles: In Euclidean plane geometry, a rectangle is a quadrilateral with four right angles. It can also be defined as an equiangular quadrilateral, since equiangular means that all of its angles are equal (360°/4 = 90°). It can also be defined as a parallelogram containing a right angle.
rhombus: In plane Euclidean geometry, a rhombus (plural rhombi or rhombuses) is a simple (non-self-intersecting) quadrilateral whose four sides all have the same length. Another name is equilateral quadrilateral, since equilateral means that all of its sides are equal in length. The rhombus is often called a diamond, after the diamonds suit in playing cards which resembles the projection of an octahedral diamond, or a lozenge, though the former sometimes refers specifically to a rhombus with a 60° angle (see Polyiamond), and the latter sometimes refers specifically to a rhombus with a 45° angle.

Input:

Two adjoined sides and the diagonal.
1 ≤ ai, bi, ci ≤ 1000, ai + bi > ci

Visual Presentation:

Java Basic Exercises: Reads the two adjoined sides and the diagonal of a parallelogram and check whether the parallelogram is a rectangle or a rhombus.

Sample Solution:

Java Code:

// Importing the Scanner class for user input
import java.util.*;

// Main class named "Main"
public class Main {
 	
    // Main method to execute the program
    public static void main(String[] args) {
        // Creating a Scanner object for user input
        Scanner sc = new Scanner(System.in);
        
        // Initializing variables to count occurrences
        int count_1 = 0, count_2 = 0;
        
        // Prompting the user to input two adjoined sides and the diagonal of a parallelogram (comma separated)
        System.out.println("Input two adjoined sides and the diagonal of a parallelogram (comma separated):");
        
        // Reading the input line and splitting it using commas
        String[] s = sc.nextLine().split(",");
        
        // Parsing the string values to integers
        int len1 = Integer.parseInt(s[0]);
        int len2 = Integer.parseInt(s[1]);
        int len3 = Integer.parseInt(s[2]);
        
        // Checking if the parallelogram is a rectangle based on the Pythagorean theorem
        if (len3 * len3 == len1 * len1 + len2 * len2)
            count_1++;
        
        // Checking if the parallelogram is a rhombus based on equal sides
        if (len1 == len2)
            count_2++;
        
        // Outputting the result based on the counts
        if (count_1 > 0)
            System.out.println("This is a rectangle.");
        if (count_2 > 0)
            System.out.println("This is a rhombus.");
    }
} 

Sample Output:

Input two adjoined sides and the diagonal of a parallelogram (comma separated):
8,8,8
This is a rhombus.

Flowchart:

Flowchart: Reads n digits (given) chosen from 0 to 9 and prints the number of combinations.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous:Write a Java program that reads n digits (given) chosen from 0 to 9 and prints the number of combinations where the sum of the digits equals to another given number (s). Do not use the same digits in a combination.
Next: Write a Java program to replace a string "Python" with "Java" and "Java" with "Python" in a given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/basic/java-basic-exercise-229.php