Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Random;
- //I declare that this is my own work and that I have not passed the program
- //to anyone else and would be
- //willing to accept any penalty given to me if found to be doing so.
- enum ShapeColor {
- Blue, Yellow, Red, Green, White
- }
- interface Shape {
- public double area();
- }
- interface Resizable {
- public void resize();
- }
- abstract class TwoD implements Shape {
- protected ShapeColor sc;
- protected int a;
- protected int b;
- protected int c;
- protected int d;
- public TwoD() {
- }
- public TwoD(ShapeColor sc, int a) {
- this.sc = sc;
- this.a = a;
- }
- public TwoD(ShapeColor sc, int a, int b) {
- this.sc = sc;
- this.a = a;
- this.b = b;
- }
- public TwoD(ShapeColor sc, int a, int b, int c) {
- this.sc = sc;
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public TwoD(ShapeColor sc, int a, int b, int c, int d) {
- this.sc = sc;
- this.a = a;
- this.b = b;
- this.c = c;
- this.d = d;
- }
- public TwoD(TwoD td) {
- this.sc = td.sc;
- this.a = td.getA();
- this.b = td.getB();
- this.c = td.getC();
- this.d = td.getD();
- }
- public int getA() {
- return a;
- }
- public int getB() {
- return b;
- }
- public int getC() {
- return c;
- }
- public int getD() {
- return d;
- }
- public ShapeColor getShapeColor() {
- return sc;
- }
- public void set(ShapeColor sc, int a, int b, int c, int d) {
- this.sc = sc;
- this.a = a;
- this.b = b;
- this.c = c;
- this.d = d;
- }
- public String toString() {
- return String.format("(%s", sc, a, b, c, d);
- }
- }
- class Circle extends TwoD {
- public Circle() {
- super();
- }
- public Circle(ShapeColor sc, int radius) {
- this.sc = sc;
- this.a = radius;
- }
- public Circle(Circle c) {
- this.sc = c.getShapeColor();
- this.a = getA();
- }
- @Override
- public double area() {
- // TODO Auto-generated method stub
- double area = Math.pow(getRadius(), 2);
- area *= Math.PI;
- return area;
- }
- public int getRadius() {
- return super.getA();
- }
- public void set(ShapeColor sc, int radius) {
- this.a = radius;
- this.sc = sc;
- }
- public String toString() {
- String superToString = super.toString();
- return String.format("Circle (2D %s,%s))\nArea = %.3f \nI am a circle shape", superToString, getRadius(),
- area());
- }
- }
- class Rectangle extends TwoD {
- public Rectangle() {
- }
- public Rectangle(ShapeColor sc, int length, int width) {
- this.sc = sc;
- this.a = length;
- this.b = width;
- }
- public Rectangle(Rectangle r) {
- this.sc = r.getShapeColor();
- this.a = r.getLength();
- this.b = r.getWidth();
- }
- @Override
- public double area() {
- // TODO Auto-generated method stub
- return (getA() * getB());
- }
- public int getLength() {
- return super.getB();
- }
- public int getWidth() {
- return super.getA();
- }
- public void set(ShapeColor sc, int length, int width) {
- this.sc = sc;
- this.a = length;
- this.b = width;
- }
- public String toString() {
- String superToString = super.toString();
- return String.format("Rectangle (2D %s,%s,%s))\nArea = %.3f \nI am a rectangle shape", superToString,
- getLength(), getWidth(), area());
- }
- }
- class Triangle extends TwoD {
- public Triangle() {
- }
- public Triangle(ShapeColor sc, int a, int b, int c) {
- super(sc, a, b, c);
- }
- public Triangle(Triangle t) {
- this.a = t.getA();
- this.b = t.getB();
- this.c = t.getC();
- this.sc = t.getShapeColor();
- }
- @Override
- public double area() {
- // TODO Auto-generated method stub
- return .5 * getB() * getC();
- }
- public int getA() {
- return super.getA();
- }
- public int getB() {
- return super.getB();
- }
- public int getC() {
- return super.getC();
- }
- public void set(ShapeColor sc, int a, int b, int c) {
- this.a = a;
- this.b = b;
- this.c = c;
- this.sc = sc;
- }
- public String toString() {
- String superToString = super.toString();
- return String.format("Triangle (2D %s,%s,%s,%s))\nArea = %.3f \nI am a triangle shape", superToString, getA(),
- getB(), getC(), area());
- }
- }
- class Trapezoid extends TwoD {
- private int h;
- public Trapezoid() {
- }
- public Trapezoid(ShapeColor sc, int a, int b, int c, int h) {
- this.h = h;
- this.a = a;
- this.b = b;
- this.c = c;
- this.sc = sc;
- }
- public int getA() {
- return super.getA();
- }
- public int getB() {
- return super.getB();
- }
- public int getC() {
- return super.getC();
- }
- public int getD() {
- return super.getD();
- }
- public int getHeight() {
- return h;
- }
- @Override
- public double area() {
- // TODO Auto-generated method stub
- return (getHeight() * (getA() + getB() / 2));
- }
- public void set(ShapeColor sc, int a, int b, int c, int d) {
- this.a = a;
- this.b = b;
- this.c = c;
- this.d = d;
- this.sc = sc;
- this.h = ((a + b) / 2);
- }
- public String toString() {
- String superToString = super.toString();
- return String.format("Trapezoid (2D %s,%s,%s,%s,%s)%s)\nArea = %.3f \nI am a trapezoid shape", superToString,
- getA(), getB(), getC(), getD(), getHeight(), area());
- }
- }
- abstract class ThreeD implements Shape, Resizable {
- protected ShapeColor sc;
- protected double a;
- public ThreeD() {
- }
- public ThreeD(ShapeColor sc, double a) {
- this.sc = sc;
- this.a = a;
- }
- public ThreeD(ThreeD td) {
- this.sc = td.sc;
- this.a = td.getA();
- }
- public double getA() {
- return a;
- }
- public void set(ShapeColor sc, double a) {
- this.sc = sc;
- this.a = a;
- }
- public void resize() {
- this.a = (a * .9);
- }
- protected double volume() {
- return Math.pow(a, 3);
- }
- public String toString() {
- return String.format("(%s (%s,%.0f) ", "3D", sc, a);
- }
- }
- class Sphere extends ThreeD {
- public Sphere() {
- }
- public Sphere(ShapeColor sc, double a) {
- super(sc, a);
- }
- public Sphere(Sphere s) {
- this.a = s.a;
- this.sc = s.sc;
- }
- @Override
- public double area() {
- // TODO Auto-generated method stub
- return (4 * Math.PI * Math.pow(a, 2));
- }
- protected double volume() {
- return super.volume() * 4 / 3 * Math.PI;
- }
- public double getA() {
- return a;
- }
- public void set(ShapeColor sc, double a) {
- this.a = a;
- this.sc = sc;
- }
- public String toString() {
- String returnvalue = "";
- returnvalue += String.format("Sphere %s \nSurface Area = %.3f\nVolume = %.3f", super.toString(), area(),
- volume());
- resize();
- returnvalue += String.format(
- "\nSize reduced by 10%% :Sphere %s \nUpdated Surface Area = %.3f\nUpdated Volume = %.3f",
- super.toString(), area(), volume());
- return returnvalue;
- }
- }
- class Cube extends ThreeD {
- public Cube() {
- }
- public Cube(ShapeColor sc, double a) {
- super(sc, a);
- }
- public Cube(Cube c) {
- this.a = c.a;
- this.sc = c.sc;
- }
- @Override
- public double area() {
- // TODO Auto-generated method stub
- return (Math.pow(a, 2) * 6);
- }
- protected double volume() {
- return super.volume();
- }
- public double getA() {
- return a;
- }
- public void set(ShapeColor sc, double a) {
- this.a = a;
- this.sc = sc;
- }
- public String toString() {
- String returnvalue = String.format("Cube %s \nSurface Area = %.3f\nVolume = %.3f ", super.toString(), area(),
- volume());
- resize();
- returnvalue += String.format(
- "\nSize reduced by 10%% : Cube %s \nUpdated Surface Area = %.3f \nUpdated Volume = %.3f ",
- super.toString(), area(), volume());
- return returnvalue;
- }
- }
- class Tetrahedron extends ThreeD {
- public Tetrahedron() {
- }
- public Tetrahedron(ShapeColor sc, double a) {
- super(sc, a);
- }
- public Tetrahedron(Tetrahedron t) {
- this.a = t.a;
- this.sc = t.sc;
- }
- @Override
- public double area() {
- // TODO Auto-generated method stub
- return (Math.sqrt(3) * Math.pow(a, 2));
- }
- protected double volume() {
- return super.volume() / (6 * Math.sqrt(2));
- }
- public double getA() {
- return a;
- }
- public void set(ShapeColor sc, double a) {
- this.a = a;
- this.sc = sc;
- }
- public String toString() {
- String returnvalue = "";
- returnvalue += String.format(" Tetrahedron %s \nSurface Area = %.3f\nVolume = %.3f ", super.toString(), area(),
- volume());
- resize();
- returnvalue += String.format(
- "\nSize reduced by 10%% :Tetrahedron %s \nUpdated Surface Area = %.3f\nUpdated Volume = %.3f ",
- super.toString(), area(), volume());
- return returnvalue;
- }
- }
- public class test {
- private static int getInt() {
- Random rand = new Random();
- return rand.nextInt(10);
- }
- private static double getDouble() {
- Random rand = new Random();
- double maxRange = 10;
- double minRange = 1;
- double randomValue = minRange + (maxRange - minRange) * rand.nextDouble();
- return randomValue;
- }
- private static ShapeColor getColor() {
- Random rand = new Random();
- return ShapeColor.values()[rand.nextInt(ShapeColor.values().length)];
- }
- private static TwoD getTwoD() {
- ArrayList<TwoD> td = new ArrayList<TwoD>();
- Rectangle r = new Rectangle();
- Circle c = new Circle();
- Triangle t = new Triangle();
- Trapezoid tr = new Trapezoid();
- td.add(r);
- td.add(tr);
- td.add(c);
- td.add(t);
- Random rand = new Random();
- return td.get(rand.nextInt(td.size()));
- }
- private static ThreeD getThreeD() {
- ArrayList<ThreeD> td = new ArrayList<ThreeD>();
- Sphere s = new Sphere();
- Cube c = new Cube();
- Tetrahedron t = new Tetrahedron();
- td.add(s);
- td.add(c);
- td.add(t);
- Random rand = new Random();
- return td.get(rand.nextInt(td.size()));
- }
- private static void displayList(ArrayList<Shape> alist) {
- int i = 0;
- for (Shape s : alist) {
- i++;
- System.out.printf("\nShape %d: ", i);
- System.out.println(s.toString());
- String x = s.getClass().getName();
- System.out.print("------------------------------");
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Random rand = new Random();
- int str = rand.nextInt(3);
- ArrayList<Shape> alist = new ArrayList<Shape>();
- while (str != 0) {
- switch (str) {
- case 1:
- TwoD get = getTwoD();
- if (get.getClass().getName().contains("Rectangle")) {
- Rectangle r = new Rectangle();
- r.set(getColor(), getInt(), getInt());
- alist.add(r);
- } else if (get.getClass().getName().contains("Circle")) {
- Circle c = new Circle();
- c.set(getColor(), getInt());
- alist.add(c);
- } else if (get.getClass().getName().contains("Triangle")) {
- Triangle t = new Triangle();
- t.set(getColor(), getInt(), getInt(), getInt());
- alist.add(t);
- } else {
- Trapezoid tr = new Trapezoid();
- tr.set(getColor(), getInt(), getInt(), getInt(), getInt());
- alist.add(tr);
- }
- break;
- case 2:
- ThreeD get2 = getThreeD();
- if (get2.getClass().getName().contains("Sphere")) {
- Sphere s = new Sphere();
- s.set(getColor(), getDouble());
- alist.add(s);
- } else if (get2.getClass().getName().contains("Cube")) {
- Cube c = new Cube();
- c.set(getColor(), getDouble());
- alist.add(c);
- } else {
- Tetrahedron t = new Tetrahedron();
- t.set(getColor(), getDouble());
- alist.add(t);
- }
- break;
- default:
- break;
- }
- str = getInt();
- }
- displayList(alist);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement