Advertisement
irmantas_radavicius

Untitled

Apr 2nd, 2022
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class Box {
  5.     protected int x;
  6.     protected int y;
  7.     protected int z;
  8.    
  9.     public Box(int d){
  10.         this(d, d, d);
  11.     }
  12.    
  13.     public Box(int a, int b, int c){
  14.         this.x = a;
  15.         this.y = b;
  16.         this.z = c;
  17.     }
  18.    
  19.     public String toString(){
  20.         return "(" + this.x + ", " + this.y + ", " + this.z + ")";
  21.     }  
  22. }
  23.  
  24. class SmallBox extends Box {       
  25.     public SmallBox(){
  26.         super(1,1,1);      
  27.     }
  28.    
  29.     public String toString(){
  30.         return "Small " + super.toString();
  31.     }
  32. }
  33. class LargeBox extends Box {       
  34.     public LargeBox(){
  35.         super(10,10,10);       
  36.     }  
  37.    
  38.     public String toString(){
  39.         return "Large " + super.toString();
  40.     }
  41. }
  42.  
  43.  
  44. public class Sandbox {  
  45.    
  46.     public static void main(String args[]) {       
  47.         try {
  48.             ArrayList<Box> boxes = new ArrayList<>();
  49.             for(int i = 0; i < 10; ++i){
  50.                 if (Math.random() < 0.5){
  51.                     boxes.add(new SmallBox());
  52.                 } else {
  53.                     boxes.add(new LargeBox());
  54.                 }
  55.             }
  56.            
  57.             for(int i = 0; i < boxes.size(); ++i){
  58.                 System.out.println(boxes.get(i).toString());
  59.             }
  60.            
  61.            
  62.         } catch(Exception e){
  63.             e.printStackTrace();           
  64.             System.out.println("Unexpected error, sorry!");
  65.         }          
  66.     }  
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement