Menu
Question Index
...

What will be the output of the following program?

import java.awt.*;
import java.util.*;
import java.util.List;
public class EnclosedArea {
    public static void main(String[] args) {
        int[] line1 = {30, 1000, 20, 1006, 40, 1014, 20, 1016};
        int[] line2 = {20, 1000, 40, 1002, 20, 1004, 10, 1006, 30, 1008, 40, 1010, 20, 1012, 50, 1014};
        int[] area = new EnclosedArea().getArea(line1, line2, new Point(23, 1007), new Point(32, 1010));
        for (int i : area)
            System.out.print(i + " ");
    }
    public int[] getArea(int[] line1, int[] line2, Point start, Point end) {
        List<Point> points = new ArrayList<Point>();
        points.add(start);
        for (int i = 0; i < line1.length; i += 2) {
            if (line1[i + 1] >= start.y && line1[i + 1] < end.y)
                points.add(new Point(line1[i], line1[i + 1]));
            if (line1[i + 1] > end.y)
                break;
        }
        points.add(end);
        for (int i = line2.length - 1; i >= 0; i -= 2) {
            if (line2[i] <= end.y && line2[i] > start.y)
                points.add(new Point(line2[i - 1], line2[i]));
            if (line2[i] < start.y)
                break;
        }
        return convertToArray(points);
    }
    private int[] convertToArray(List<Point> points) {
        int[] result = new int[points.size() * 2];
        int i = 0;
        for (Point point : points) {
            result[i++] = point.x;
            result[i++] = point.y;
        }
        return result;
    }
}


23 1007 40 1010 30 1008 20 2012
23 1007 32 1010 40 1010 30 1008
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Other Advanced Topics In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App