KnowledgeBoat Logo

Computer Applications

Define a class Triplet with the following specifications:
Class name — Triplet
Data Members — int a, int b, int c
Member Methods:
void getdata() — to accept three numbers
void findprint() — to check and display whether the numbers are Pythagorean Triplets or not.

Java

Java Classes

70 Likes

Answer

import java.util.Scanner;

public class Triplet
{
    private int a;
    private int b;
    private int c;
    
    public void getdata() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a: ");
        a = in.nextInt();
        System.out.print("Enter b: ");
        b = in.nextInt();
        System.out.print("Enter c: ");
        c = in.nextInt();
    }
    
    public void findprint() {
        if ((Math.pow(a, 2) + Math.pow(b, 2)) == Math.pow(c, 2)
                || (Math.pow(b, 2) + Math.pow(c, 2)) == Math.pow(a, 2)
                || (Math.pow(a, 2) + Math.pow(c, 2)) == Math.pow(b, 2))
            System.out.print("Numbers are Pythagorean Triplets");
        else
            System.out.print("Numbers are not Pythagorean Triplets");
    }
    
    public static void main(String args[]) {
        Triplet obj = new Triplet();
        obj.getdata();
        obj.findprint();
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a class program with the following specifications: Class Name — Triplet Data Members — int a, b, c Member Methods: void getdata() — to accept three numbers void findprint() — to check and display whether the numbers are Pythagorean Triplets or not.BlueJ output of Write a class program with the following specifications: Class Name — Triplet Data Members — int a, b, c Member Methods: void getdata() — to accept three numbers void findprint() — to check and display whether the numbers are Pythagorean Triplets or not.

Answered By

29 Likes


Related Questions