Advertisement
Guest User

Java test

a guest
Dec 17th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. // file Node.java
  2. public class Node {
  3.     protected final int value;
  4.  
  5.     public Node(int value) {
  6.         this.value = value;
  7.     }
  8. }
  9.  
  10. // file InnerNode.java
  11. public class InnerNode extends Node {
  12.     private final List<Node> children;
  13.  
  14.     public InnerNode() {
  15.     }
  16.    
  17.     public void add(Node node) {
  18.         children.add(node);
  19.     }
  20.    
  21.     public void print() {
  22.         print(0);
  23.     }
  24.    
  25.     private void print(int padding) {
  26.         for (int i = 0; i < children.size(); i++) {
  27.             Node child = children.get(i);
  28.             printPadding(padding);
  29.             System.out.println(child.value);
  30.            
  31.             child.print(padding + 1);
  32.         }
  33.     }
  34.    
  35.     private static void printPadding(int padding) {
  36.         for (int i = 0; i < padding; i++) {
  37.             System.out.print(" ");
  38.         }
  39.     }
  40.    
  41. }
  42.  
  43. // file Main.java
  44. public class Main {
  45.     public static void main(String[] args) {
  46.         Node left = new Node(42);
  47.         Node right = new Node(99);
  48.        
  49.         InnerNode inner = new InnerNode();
  50.         inner.add(left);
  51.         inner.add(right);
  52.        
  53.         inner.print();
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement