Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- class Box {
- protected int x;
- protected int y;
- protected int z;
- public Box(int d){
- this(d, d, d);
- }
- public Box(int a, int b, int c){
- this.x = a;
- this.y = b;
- this.z = c;
- }
- public String toString(){
- return "(" + this.x + ", " + this.y + ", " + this.z + ")";
- }
- }
- class SmallBox extends Box {
- public SmallBox(){
- super(1,1,1);
- }
- public String toString(){
- return "Small " + super.toString();
- }
- }
- class LargeBox extends Box {
- public LargeBox(){
- super(10,10,10);
- }
- public String toString(){
- return "Large " + super.toString();
- }
- }
- public class Sandbox {
- public static void main(String args[]) {
- try {
- ArrayList<Box> boxes = new ArrayList<>();
- for(int i = 0; i < 10; ++i){
- if (Math.random() < 0.5){
- boxes.add(new SmallBox());
- } else {
- boxes.add(new LargeBox());
- }
- }
- for(int i = 0; i < boxes.size(); ++i){
- System.out.println(boxes.get(i).toString());
- }
- } catch(Exception e){
- e.printStackTrace();
- System.out.println("Unexpected error, sorry!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement