Menu
Topics Index
...
`

Collections1768


If you need explanation Read this topic

If you need Answer Take test on this topic

Collections1768
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;
    }
}

A. 23 1007 40 1010 30 1008 20 2012
B. 23 1007 32 1010 40 1010 30 1008
C. Some other output
D. Compilation Error or Runtime Error
Topic: Other Advanced Topics In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans b

Posted by Manasa Jyothi Sree    2014-06-09 13:07:33


C sm other output.such as 20 1007 32 1010 only bcz v r consdrng Enclosed area with points 23,1007 and 32,1010 and strng in a ArrayList and returning d points in arrayformat.

Posted by Shabaaz Shaikz    2014-06-09 13:21:25


ans b. start point s 1st added and check the line 1 array y value greatr thn strt and less thn end.so no points r takn from line 1.
end point added and chck line2 array y value searchd les thn end point and grtr than equal strt point from tat 2 values of x y takd.
values r 40,1010 30,1008

Posted by Maheshwari Natarajan    2014-06-09 13:27:11


Congratulations Maheshwari Natarajan. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-06-10 11:38:07


© meritcampus 2019

All Rights Reserved.

Open In App