Question

In: Computer Science

Implement a Composite Design Pattern for the code below that creates a family tree MAIN: public...

Implement a Composite Design Pattern for the code below that creates a family tree

MAIN:

public class Main {
public static void main(String[] args) {
/* Let's create a family tree (for instance like one used on genealogy sites).
For the sake of simplicity, assume an individual can have at most two children.
If an individual has 1-2 children, they are considered a "tree". If an individual
does not have children, they are considered a "person".
With that in mind, let's populate a family tree with some data. */

Person p1 = new Person(1);
Person p2 = new Person(2);
Person p3 = new Person(3);
Person p4 = new Person(4);

Tree t1 = new Tree(p1, 1);
Tree t2 = new Tree(p2, p3, 2);
Tree t3 = new Tree(t1, p4, 3);
Tree t4 = new Tree(t3, t2, 4);
  
t4.print();
}
}

PERSON:


public class Person {
String name;
  
public Person(int num) {
name = "person" + num;
}
  
public void print() {
System.out.println(name);
}
}

TREE:


public class Tree {
private String name;
  
private Tree tree1;
private Tree tree2;
  
private Person person1;
private Person person2;

public Tree(Person p1, int num) {
tree1 = null;
tree2 = null;
person1 = p1;
person2 = null;
name = "tree" + num;
}

public Tree(Tree t1, int num) {
tree1 = t1;
tree2 = null;
person1 = null;
person2 = null;
name = "tree" + num;
}

public Tree(Tree t1, Tree t2, int num){
tree1 = t1;
tree2 = t2;
person1 = null;
person2 = null;
name = "tree" + num;
}
  
public Tree(Tree t1, Person p2, int num){
tree1 = t1;
tree2 = null;
person1 = null;
person2 = p2;
name = "tree" + num;
}
  
public Tree(Person p1, Person p2, int num){
tree1 = null;
tree2 = null;
person1 = p1;
person2 = p2;
name = "tree" + num;
}
  
public void print() {
System.out.println(name);
if (tree1 != null) {
tree1.print();
}
if (tree2 != null) {
tree2.print();
}
if (person1 != null) {
person1.print();
}
if (person2 != null) {
person2.print();
}
}
}

Solutions

Expert Solution

For composite Design Pattern Icreated On Interface SuperPrint and declared one method inside it that is print()

Then implements Person class from SuperPrint Interface and override the print() to Person class.

Same way Tree calss also implements from the SuperPrint interface and override the print().

In composite design pattern basically we are Creating one Interface and declaring all common methods inside in it. If any Class wants to Implement the common methods then that class should implements from the Interface and can override the common methods. In this way mutliple class can implements from the same Inerface to write implementation logic.

Below is the code

SuperPrint.java

public interface SuperPrint {
  
   void print();

}

Person.java

public class Person implements SuperPrint{

   String name;

   public Person(int num) {
       name = "person" + num;
   }

   // Interface implemented method

   public void print() {
       System.out.println(name);      
   }
}

Tree.java

public class Tree implements SuperPrint{

   private String name;

   private Tree tree1;
   private Tree tree2;

   private Person person1;
   private Person person2;

   public Tree(Person p1, int num) {
       tree1 = null;
       tree2 = null;
       person1 = p1;
       person2 = null;
       name = "tree" + num;
   }

   public Tree(Tree t1, int num) {
       tree1 = t1;
       tree2 = null;
       person1 = null;
       person2 = null;
       name = "tree" + num;
   }

   public Tree(Tree t1, Tree t2, int num) {
       tree1 = t1;
       tree2 = t2;
       person1 = null;
       person2 = null;
       name = "tree" + num;
   }

   public Tree(Tree t1, Person p2, int num) {
       tree1 = t1;
       tree2 = null;
       person1 = null;
       person2 = p2;
       name = "tree" + num;
   }

   public Tree(Person p1, Person p2, int num) {
       tree1 = null;
       tree2 = null;
       person1 = p1;
       person2 = p2;
       name = "tree" + num;
   }

   // Interface implemented method
  
   public void print() {
       System.out.println(name);
       if (tree1 != null) {
           tree1.print();
       }
       if (tree2 != null) {
           tree2.print();
       }
       if (person1 != null) {
           person1.print();
       }
       if (person2 != null) {
           person2.print();
       }
   }

}

In the following way also we can write the Main class after implementing Person and Tree class from SuperPrint Interface.

Main.java

public class Main {

   public static void main(String[] args) {
       /* Let's create a family tree (for instance like one used on genealogy sites).
       For the sake of simplicity, assume an individual can have at most two children.
       If an individual has 1-2 children, they are considered a "tree". If an individual
       does not have children, they are considered a "person".
       With that in mind, let's populate a family tree with some data. */

       SuperPrint p1 = new Person(1);
       SuperPrint p2 = new Person(2);
       SuperPrint p3 = new Person(3);
       SuperPrint p4 = new Person(4);

       SuperPrint t1 = new Tree((Person)p1, 1);
       SuperPrint t2 = new Tree((Person)p2, (Person)p3, 2);
       SuperPrint t3 = new Tree((Tree)t1, (Person)p4, 3);
       SuperPrint t4 = new Tree((Tree)t3,(Tree) t2, 4);
      
       t4.print();
   }
}


Related Solutions

Implement a Factory Design Pattern for the code below: MAIN: import java.util.ArrayList; import java.util.List; import java.util.Random;...
Implement a Factory Design Pattern for the code below: MAIN: import java.util.ArrayList; import java.util.List; import java.util.Random; public class Main { public static void main(String[] args) { Character char1 = new Orc("Grumlin"); Character char2 = new Elf("Therae"); int damageDealt = char1.attackEnemy(); System.out.println(char1.name + " has attacked an enemy " + "and dealt " + damageDealt + " damage"); char1.hasCastSpellSkill = true; damageDealt = char1.attackEnemy(); System.out.println(char1.name + " has attacked an enemy " + "and dealt " + damageDealt + " damage");...
Design and implement a Java program that creates a GUI that will allow a customer to...
Design and implement a Java program that creates a GUI that will allow a customer to order pizza and other items from a Pizza Paarlor. The customer should be able to order a variety of items which are listed below. The GUI should allow the customer (viaJavaFX UI Controls - text areas, buttons, checkbox, radio button, etc.) to input the following information: Name of the customer First Name Last Name Phone number of the customer Type of food being order...
Just Implement Inorder, Preorder and Postorder of Tree Data Structure in given code below. Thank You....
Just Implement Inorder, Preorder and Postorder of Tree Data Structure in given code below. Thank You. #include<bits/stdc++.h> using namespace std; struct Node{     int data;     Node* left;     Node* right; }; Node* createNode(int value){     Node* newNode = new Node();     newNode->data = value;     newNode->left = NULL;     newNode->right = NULL;     return newNode; } Node* insert(Node *currentNode, int value){     if(currentNode==NULL){         currentNode = createNode(value);     }else if(value <= currentNode->data){         currentNode->left = insert(currentNode->left,value);     }else{        ...
Java - Design and implement an application that creates a histogram that allows you to visually...
Java - Design and implement an application that creates a histogram that allows you to visually inspect the frequency distribution of a set of values. The program should read in an arbitrary number of integers that are in the range 1 to 100 inclusive; then produce a chart similar to the one below that indicates how many input values fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk for each value entered. Sample...
8.3) Design and implement an application that creates a histogram that allows you to visually inspect...
8.3) Design and implement an application that creates a histogram that allows you to visually inspect the frequency distribution of a set of values. The program should read in an arbitrary number of integers that are in the range 1 to 100 inclusive; then produce a chart similar to the one below that indicates how many input values fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk for each value entered. 1 -...
Draw a state diagram of the string pattern recognizer, implement it according to the design sequence...
Draw a state diagram of the string pattern recognizer, implement it according to the design sequence of the FSM, and draw a schematic diagram.
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Dependency injection is a design pattern used to implement Inversion of Control (IoC). Explain Dependency Injection...
Dependency injection is a design pattern used to implement Inversion of Control (IoC). Explain Dependency Injection as a fundamental aspect of spring framework. Discuss different ways to implemented DI in J2EE.
subject pattern design (decorator) Description • Suppose you were asked to implement a drawing tool •...
subject pattern design (decorator) Description • Suppose you were asked to implement a drawing tool • This drawing tool contains the shape supper class which includes draw() and getDescription functionalities • And you have two concrete class; Circle and Rectangle • This tool enables us to draw circles and rectangles but we want to extend this tool to enable the user to decorate the shapes with • Fill-color • border-color • border-thickness • border-style; dashed , dotted Drawing Tool Exercise...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT