Advertisement
pastaCode

Untitled

Nov 21st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. /**
  2.  * Author: Leon Beilis
  3.  * Desription: receive two Trapezoid base points and return Trapezoid Perimeter and area
  4.  * minus values support also..
  5.  */
  6.  
  7. import java.util.Scanner;
  8.  
  9. public class Trapezoid {
  10.     public static void main() {
  11.  
  12.         //first base
  13.         int firstBaseFirstPointX, firstBaseFirstPointY, firstBaseLength, firstBaseSecondPointX, firstBaseSecondPointY;
  14.  
  15.         //second base
  16.         int secondBaseFirstPointX, secondBaseFirstPointY, secondBaseLength, secondBaseSecondPointX, secondBaseSecondPointY;
  17.  
  18.         // trapezoid properties
  19.         int trapezoidHeight;
  20.         double trapezoidBaseSum, trapezoidPerimeterSum;
  21.  
  22.         //initilize scanner
  23.         Scanner scan = new Scanner(System.in);
  24.        
  25.         System.out.println("Please enter the first base point and its length to the next points on the x axis");
  26.         System.out.println("by this format: (x1,y1,length on the x axis)");
  27.         System.out.println("for example: \"1 1 5\"");
  28.         firstBaseFirstPointX = Math.abs(scan.nextInt()); // x of a
  29.         firstBaseFirstPointY = firstBaseSecondPointY = Math.abs(scan.nextInt()); // y of a and b
  30.         firstBaseLength = scan.nextInt(); // length on the x axis to the next point (b)
  31.         firstBaseSecondPointX = firstBaseFirstPointX + firstBaseLength; // x of b
  32.  
  33.         System.out.println("Please enter the second base point and its length to the next points on the x axis");
  34.         secondBaseFirstPointX = Math.abs(scan.nextInt()); // x of c
  35.         secondBaseFirstPointY = secondBaseSecondPointY = Math.abs(scan.nextInt()); // y of c and d
  36.         secondBaseLength = scan.nextInt(); // length on the x axis to the next point (d)
  37.         secondBaseSecondPointX = secondBaseFirstPointX + secondBaseLength; // x of d
  38.  
  39.         trapezoidHeight = Math.abs(firstBaseFirstPointY - secondBaseFirstPointY);
  40.         trapezoidBaseSum = ( (firstBaseLength + secondBaseLength) * trapezoidHeight ) / 2.0;
  41.  
  42.         System.out.println("The area of the trapezoid is " + trapezoidBaseSum );
  43.  
  44.         trapezoidPerimeterSum = firstBaseLength + Math.sqrt( Math.pow( firstBaseSecondPointX - secondBaseSecondPointX ,2) + Math.pow( firstBaseSecondPointY - secondBaseSecondPointY ,2) ) + secondBaseLength + Math.sqrt( Math.pow( secondBaseFirstPointX - firstBaseFirstPointX ,2) + Math.pow( secondBaseFirstPointY - firstBaseFirstPointY ,2) );
  45.         System.out.println("The perimeter of the trapezoid is " + trapezoidPerimeterSum);
  46.  
  47.         /**
  48.          * Debug zone
  49.          */
  50.         // System.out.println("A point: (" + firstBaseFirstPointX + "," + firstBaseFirstPointY + ")");
  51.         // System.out.println("B point: (" + firstBaseSecondPointX + "," + firstBaseSecondPointY + ")");
  52.         // System.out.println("Length between A to B is: " + firstBaseLength);
  53.         // System.out.println("C point: (" + secondBaseFirstPointX + "," + secondBaseFirstPointY + ")");
  54.         // System.out.println("D point: (" + secondBaseSecondPointX + "," + secondBaseSecondPointY + ")");
  55.         // System.out.println("Length between C to D is: " + secondBaseLength);
  56.         // System.out.println("trapezoidHeight is: " + trapezoidHeight);
  57.         // System.out.println("Base sum is: " + trapezoidBaseSum);
  58.  
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement