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 8929627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:42:55+00:00 2026-06-15T08:42:55+00:00

I have a Java class ( SquareIcon ) that implements the Icon interface. It

  • 0

I have a Java class (SquareIcon) that implements the Icon interface. It draws a square, of which you can choose the size and color when you create one. I now want to write a class using the Composite Pattern (CompositeIcon), which makes it possible for one to draw several different squares. I’ve read about the Composite Pattern, but I just can’t seem to make it work.

In the SquareIcon class, I have the following three methods:

  • getIconHeight
  • getIconWidht
  • paintIcon

I have to put all of these in the CompositeIcon class too, right? But how do I do this? I’ve been thinking of something along these lines, but I don’t know if this is right:

public int getIconWidth() {
  for (Icon i : icons) {
    i.getIconWidth();
  }
}

I also have no idea how to do this for the paintIcon method, since it takes 4 parameters, which means this won’t work.

  • 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-06-15T08:42:56+00:00Added an answer on June 15, 2026 at 8:42 am

    A Composite pattern treats leaf and parent classes the same.

    You have to start with a common interface:

    public interface Icon {
        void paint();
    }
    

    Leaf implements the interface:

    public class LeafIcon implements Icon {
        public void paint() { 
            // more here
        }
    }
    

    So does the Parent, which has a collection of Leaf children:

    public class ParentIcon implements Icon {
        private List<Icon> children = new ArrayList<Icon>();
        public void paint() {
            for (Icon child : children) {
                child.paint();
            }
        }
    }
    

    Your code should deal with collections of Icons. You can all the paint() method on leaves and parents.

    List<Icon> icons = new ArrayList<Icon>();
    for (Icon icon : icons) {
        icon.paint();
    }
    

    Any parent in the collection will recursively call its children all the way to leaf nodes in the tree.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java class that can extend either of two third-party classes, which
I have a Java class which is located in src/java that references classes from
I have a Java class which can be called from shell. (via java [command][options])
I have a java class that uses complex static fields which need special operations
I have a Java class MyPojo that I am interested in deserializing from JSON.
I have a Java class which handles both single valued and multi-valued data. Therefore,
If I have a java class which is package-private (declared with class, not public
I'm new to learning JSPs and Servlets. I have a Java class which calls
I have java class which has a variable of type char now I have
I have a java class which attempts to autologin to a website. The status

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.