Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

4: Pythagorean Theorem [10 marks Recall Pythagoras\'s Theorem: Pythagoras: In a

ID: 3873352 • Letter: 4

Question

4: Pythagorean Theorem [10 marks Recall Pythagoras's Theorem: Pythagoras: In a right angle triangle, the square of the hypotenuse is equal to the sum of the squares of the two other sides. If a, b and c are the lengths of the sides in a right angle triangle, with c being the hypotenuse (see diagram below), then c2 = a2 + b2. 90° c2 = a2 + b2 It is easy to check if three integer lengths (of a right angle triangle) satisfy this relationship. When the lengths are not integers though, we cannot just ask if cb, because the numbers are no longer exact and checking if two approximate real numbers are equal to each other is difficult. Instead of checking for an exact equality, we

Explanation / Answer

Create a file name Pythagoras.java and paste this code into it! filename must be Pythagoras.java

Pythagoras.java:

import java.util.Scanner;
import java.util.*;
public class Pythagoras {

public static Boolean isPythagorasInt(int a, int b, int c)
{
Boolean flag = false;
if(c*c == a*a + b*b)
{
flag = true;
}
return flag;
}
  
public static Boolean isPythagorasDouble( double a, double b, double c ,double epsilon)
{
Boolean flag = false;
if(Math.abs( a*a + b*b - c*c) < epsilon)
{
flag = true;
}
return flag;   
}
  
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
while(true)
{
System.out.println("Enter int, float or quit: ");
String tmp = scanner.nextLine();
if(tmp.compareTo("int") == 0)
{
System.out.println("Enter triple");
String str_nums = scanner.nextLine();
String[] nums = str_nums.split(",");
int a = Integer.parseInt(nums[0]);
int b = Integer.parseInt(nums[1]);
int c = Integer.parseInt(nums[2]);
System.out.println(isPythagorasInt(a,b,c));

}
else if(tmp.compareTo("float") == 0)
{
System.out.println("Enter triple");
String str_nums = scanner.nextLine();
String[] nums = str_nums.split(",");
double a = Double.parseDouble(nums[0]);
double b = Double.parseDouble(nums[1]);
double c = Double.parseDouble(nums[2]);
double epsilon = Double.parseDouble(nums[3]);
System.out.println(isPythagorasDouble(a,b,c,epsilon));
  
}
else if(tmp.compareTo("quit") == 0)
{
System.out.println("Good Bye!");
break;
}
else
{
System.out.println("Invalid Input!");
}
}
  
}
  
}

Sample Output:

Enter int, float or quit:
int
Enter triple
3,4,5
true
Enter int, float or quit:
float
Enter triple
12.0,5,13.0,0.01
true
Enter int, float or quit:
float
Enter triple
12.0,5,14.0, 0.01
false
Enter int, float or quit:
quit
Good Bye!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote