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...
Implement a generic queue.  Use the code below for main(). main() { int i, x; float y;...
Implement a generic queue.  Use the code below for main(). main() { int i, x; float y; char z; Queue<int> A; Queue<float> B; Queue<char> C; ifstream in; in.open("int.txt"); for (i = 0; i < 100; i++){         in >> x;         A.enqueue(x);     } cout << A.dequeue() << endl;; for (i = 0; i < 12; i++)         A.enqueue(i); cout << A.dequeue() << endl; cout << A.dequeue() << endl; cout << "Dequeueing: "<< A.dequeue()<< endl; while (!A.isEmpty())      cout  << A.dequeue() << "  "; if (A.isEmpty())       cout <<...
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...
Can you give a java code example of using a singleton pattern and factory pattern design...
Can you give a java code example of using a singleton pattern and factory pattern design together?
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 -...
java 8.3: Histogram Design and implement an application that creates a histogram that allows you to...
java 8.3: Histogram 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....
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.
2. Implement a main method that does the following: Use Java (a) Creates a binary search...
2. Implement a main method that does the following: Use Java (a) Creates a binary search tree to hold sports scores as data for a sport of your own choice (b) Adds between 5 and 8 scores to the tree using standard tree operations Try insert (c) Choose one of the tree traversals, preorder or postorder, and use it to print out all elements of the tree (d) Delete one score from the tree using standard tree operations (suppose the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT