Advertisement
SUni2020

RectangleArea2D_03

Feb 16th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class RectangleArea2D_03 {
  4.     public static void main(String[] args) {
  5.  
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         // 1. read input -> x1, y1 , x2, y2
  9.         // 2. calculate width = y1 - y2
  10.         // 3. calculate length = x1 - x2
  11.         // 4. area = width * length
  12.         // 5. perimeter = 2* (width + length)
  13.         // 6. print -> 2
  14.  
  15.         double x1 = Double.parseDouble(scanner.nextLine());
  16.         double y1 = Double.parseDouble(scanner.nextLine());
  17.         double x2 = Double.parseDouble(scanner.nextLine());
  18.         double y2 = Double.parseDouble(scanner.nextLine());
  19.  
  20.         double width = Math.abs(y1 - y2); //  the number can only be positive - Math.abs
  21.         double length= Math.abs(x1 - x2);
  22.         double area = width * length;
  23.         double perimeter = 2 * (width + length);
  24.  
  25.         System.out.printf("%.2f%n", area); // %n-  go to new line after the printed line
  26.         System.out.printf("%.2f", perimeter);
  27.  
  28.  
  29.  
  30.  
  31.  
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement