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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:36:47+00:00 2026-05-28T13:36:47+00:00

there is a class named as composite, in which, it stores a list. I

  • 0

there is a class named as “composite”, in which, it stores a list. I want to write a method to calculate the result of the list. where should I wirte the calculation method? Based on the OO methodology, do I need to create an interface of calculation, and make it be implemented by the composite class? If yes, can you explain why?
Thanks.

package composite;

public abstract class Component {

private int level;

public abstract void add(Component component);

public abstract void remove(Component component);

public abstract void eachChild();

   public int getLevel() {
      return level;    }

   public void setLevel(int level) {
      this.level = level;    } }



package composite;

import java.util.ArrayList;

public class Composite extends Component implements ILevelCalculator
{

ArrayList<Component> componentList = new ArrayList<Component>();

@Override public void add(Component component) {   
componentList.add(component); }

@Override public void remove(Component component) {   
componentList.remove(component); }

@Override public void eachChild() {   
System.out.println(this.getLevel() + " excuted...");    for
(Component component : componentList) {
      component.eachChild();    } }

@Override public int getLevel() {    int levelResult;    levelResult
= getTheBiggestNo(componentList);    return levelResult; }

private int getTheBiggestNo(ArrayList<Component> componentList) {   
int theBiggestNo = 0;    if (componentList.isEmpty()) {
      System.out.println("The list is empty!");    } else {
      theBiggestNo = getCompareResult(componentList);    }    return theBiggestNo; }

private int getCompareResult(ArrayList<Component> componentList) {  
int result = 0;    for (Component biggest : componentList) {
      if (biggest.getLevel() > result) {
         result = biggest.getLevel();
      }    }    return result; } }


package composite;

public interface ILevelCalculator { public int getLevel(); }


package composite;

public class Leaf extends Component {

   @Override    public void add(Component component) {
      throw new UnsupportedOperationException("Not supported yet.");    }

   @Override    public void remove(Component component) {
      throw new UnsupportedOperationException("Not supported yet.");    }

   @Override    public void eachChild() {
      System.out.println("Leaf's name:"+this.getLevel());    }

}



package composite;

public class Main {

public static void main(String[] args) {    Composite questionnaire
= new Composite();    for(int i=0;i<4;i++){
      Composite dimension = new Composite();    for(int j=0;j<5;j++){
      Leaf question = new Leaf();
      question.setLevel(i);
      dimension.add(question);    }
      questionnaire.add(dimension);
      System.out.println("The dimension < "+i+" > result is: "+questionnaire.getLevel());    }

   System.out.println("The final result is:
"+questionnaire.getLevel()); } }
  • 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-28T13:36:49+00:00Added an answer on May 28, 2026 at 1:36 pm

    It almost sounds like you have your OO a bit mixed up. If I had a class called composite it could implement an interface which had a method called calculate. Then by requirement of the interface your class needs a calculate method. Also, you tagged database-design for this. Was there something more to your question or was this just a generic OOP question?

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

Sidebar

Related Questions

I Create a class named DataClass there i have this method +(NSMutableArray*) returnList :(NSString
In my webpage, there's a div with a class named Test . How can
I don't understand the class TextCompositionEventArgs. There are members of type string named ControlText,SystemText,Text.
Is there a __CLASS__ macro in C++ which gives the class name similar to
There seems to only be 2nd class support for composite database keys in Java's
Is there a way to take a class name and convert it to a
is there any way to reference a class name from within the class declaration?
Is there a way in any browser to add/remove class names? For example, if
Is there a way in objective-c/Cocoa to alloc an object when the class name
Is there a class in the ASP.NET MVC framework that represents a controller name

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.