Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6798747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:46:02+00:00 2026-05-26T18:46:02+00:00

Given an arraylist of rectangles, my task is to find the smallest rectangle enclosing

  • 0

Given an arraylist of rectangles, my task is to find the smallest rectangle enclosing all the other rectangles.

import java.awt.Rectangle;
import java.util.ArrayList;

public class Homework {
public static void main(String[] args) {
    ArrayList<Rectangle> test = new ArrayList<Rectangle>();
    test.add(new Rectangle(10, 20, 30, 40));
    test.add(new Rectangle(20, 10, 30, 40));
    test.add(new Rectangle(10, 20, 40, 50));
    test.add(new Rectangle(20, 10, 50, 30));
    Rectangle enc = enclosing(test);
    System.out.println(enc);
    System.out.println("Expected: java.awt.Rectangle[x=10,y=10,width=60,height=60]");
}

public static Rectangle enclosing(ArrayList<Rectangle> rects) {
    // Your work here
}
}

What I have so far:

public static Rectangle enclosing(ArrayList<Rectangle> rects) {
    double topLeftX = Integer.MAX_VALUE;
    double topLeftY = Integer.MAX_VALUE;
    double bottomRightX = Integer.MIN_VALUE;
    double bottomRightY = Integer.MIN_VALUE;

    for (Rectangle r : rects) {
        if (r.getX() < topLeftX)
            topLeftX = r.getX();

        if (r.getY() < topLeftY)
            topLeftY = r.getY();

        if ((r.getX() + r.getWidth()) > bottomRightX)
            bottomRightX = (r.getX() + r.getWidth());

        if ((r.getY() + r.getHeight()) > bottomRightY)
            bottomRightY = (r.getY() + r.getHeight());
    }
    Rectangle.Double enc = new Rectangle.Double(topLeftX, topLeftY, bottomRightX - topLeftX, bottomRightY - topLeftY);

    return enc;
}

I get an “incompatible types” error for my return line. I’m not sure what goes there to make the output match the tester block at the top.

Thanks in advance! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:46:03+00:00Added an answer on May 26, 2026 at 6:46 pm

    Change

    Rectangle.Double enc = new Rectangle.Double(topLeftX, topLeftY, bottomRightX - topLeftX, bottomRightY - topLeftY);
    

    to

    Rectangle enc = new Rectangle((int) topLeftX, (int) topLeftY, (int) (bottomRightX - topLeftX), (int) (bottomRightY - topLeftY));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java, i have an arrayList of ip address. how do i find the
Coming from Java and using the ArrayList class has me all frustrated when trying
Given a latitude and longitude, what is the easiest way to find the name
I have page gets given an ArrayList<Document> where each document has a property called
I know this is a basic question, but I can't find other StackOverflow posts
Hello Im trying to get all possible combinations with repetitions of given char array.
ArrayList<Rectangle> list = new ArrayList<Rectangle>(); for (int i=0; i < 10; i++) { list.add(new
In my code I default to using ArrayList for all Lists, HashMap for all
How to replace element if exists in an ArrayList at a given index?
I have a java method that should check through an ArrayList and check if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.