Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class RectangleArea2D_03 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // 1. read input -> x1, y1 , x2, y2
- // 2. calculate width = y1 - y2
- // 3. calculate length = x1 - x2
- // 4. area = width * length
- // 5. perimeter = 2* (width + length)
- // 6. print -> 2
- double x1 = Double.parseDouble(scanner.nextLine());
- double y1 = Double.parseDouble(scanner.nextLine());
- double x2 = Double.parseDouble(scanner.nextLine());
- double y2 = Double.parseDouble(scanner.nextLine());
- double width = Math.abs(y1 - y2); // the number can only be positive - Math.abs
- double length= Math.abs(x1 - x2);
- double area = width * length;
- double perimeter = 2 * (width + length);
- System.out.printf("%.2f%n", area); // %n- go to new line after the printed line
- System.out.printf("%.2f", perimeter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement