Advertisement
irmantas_radavicius

Untitled

Apr 2nd, 2022
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 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 super.toString();
  31.     }
  32. }
  33. class LargeBox extends Box {       
  34.     public LargeBox(){
  35.         super(1,1,1);      
  36.     }  
  37.    
  38.     public void beProud(){
  39.         System.out.println("Hello, I am a large box! Yoohooo :)");
  40.     }
  41.    
  42.     public String toString(){
  43.         return super.toString();
  44.     }
  45. }
  46.  
  47.  
  48. public class Sandbox {  
  49.    
  50.     public static void main(String args[]) {       
  51.         try {
  52.             ArrayList<Box> boxes = new ArrayList<>();
  53.             for(int i = 0; i < 10; ++i){
  54.                 if (Math.random() < 0.5){
  55.                     boxes.add(new SmallBox());
  56.                 } else {
  57.                     boxes.add(new LargeBox());
  58.                 }
  59.             }
  60.            
  61.             for(int i = 0; i < boxes.size(); ++i){
  62.                 System.out.println(boxes.get(i).toString() + " ");
  63.                 if (boxes.get(i) instanceof LargeBox){ 
  64.                     LargeBox lb = (LargeBox)boxes.get(i);              
  65.                     lb.beProud();                  
  66.                 }              
  67.             }
  68.            
  69.            
  70.         } catch(Exception e){
  71.             e.printStackTrace();           
  72.             System.out.println("Unexpected error, sorry!");
  73.         }          
  74.     }  
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement