Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sun.security.provider.SHA;
- import java.lang.reflect.Array;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.util.Stack;
- enum Color {
- RED, GREEN, BLUE
- }
- interface Scalable {
- void scale(float scaleFactor);
- }
- interface Stackable {
- float weight();
- }
- abstract class Shape implements Scalable, Stackable {
- String id;
- Color color;
- public Shape(String id, Color color) {
- this.id = id;
- this.color = color;
- }
- abstract public String getType();
- @Override
- public String toString() {
- return String.format("%s: %-5s%-10s%10.2f\n", getType(), id, color, weight());
- }
- }
- class Circle extends Shape {
- float radius;
- public Circle(String id, Color color, float radius) {
- super(id, color);
- this.radius = radius;
- }
- @Override
- public void scale(float scaleFactor) {
- radius *= scaleFactor;
- }
- @Override
- public float weight() {
- return (float) (radius * radius * Math.PI);
- }
- @Override
- public String getType() {
- return "C";
- }
- }
- class Rectangle extends Shape {
- float width;
- float height;
- public Rectangle(String id, Color color, float width, float height) {
- super(id, color);
- this.width = width;
- this.height = height;
- }
- @Override
- public void scale(float scaleFactor) {
- width *= scaleFactor;
- height *= scaleFactor;
- }
- @Override
- public float weight() {
- return width * height;
- }
- @Override
- public String getType() {
- return "R";
- }
- }
- class Canvas {
- ArrayList<Shape> shapes;
- public Canvas() {
- this.shapes = new ArrayList<>();
- }
- private int indexToAdd(Shape shape) {
- /* Beginning case */
- if (shapes.size() == 0) {
- return 0;
- }
- /* Midway case */
- for (int i = 0; i < shapes.size(); i++) {
- if (shapes.get(i).weight() < shape.weight()) {
- return i;
- }
- }
- return shapes.size();
- }
- /* Adding a circle */
- public void add(String id, Color color, float radius) {
- Circle circle = new Circle(id, color, radius);
- shapes.add(indexToAdd(circle), circle);
- }
- /* Adding a rectangle */
- public void add(String id, Color color, float width, float height) {
- Rectangle rectangle = new Rectangle(id, color, width, height);
- shapes.add(indexToAdd(rectangle), rectangle);
- }
- public void scale(String id, float scaleFactor) {
- Shape temp = null;
- for (int i = 0; i < shapes.size(); i++) {
- if (shapes.get(i).id.equals(id)) {
- temp = shapes.get(i);
- break;
- }
- }
- /* We store it in temp because we're going to remove it */
- shapes.remove(temp);
- temp.scale(scaleFactor);
- shapes.add(indexToAdd(temp), temp);
- }
- @Override
- public String toString() {
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < shapes.size(); i++) {
- sb.append(shapes.get(i).toString());
- }
- return sb.toString();
- }
- }
- public class ShapesTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Canvas canvas = new Canvas();
- while (scanner.hasNextLine()) {
- String line = scanner.nextLine();
- String[] parts = line.split(" ");
- int type = Integer.parseInt(parts[0]);
- String id = parts[1];
- if (type == 1) {
- Color color = Color.valueOf(parts[2]);
- float radius = Float.parseFloat(parts[3]);
- canvas.add(id, color, radius);
- } else if (type == 2) {
- Color color = Color.valueOf(parts[2]);
- float width = Float.parseFloat(parts[3]);
- float height = Float.parseFloat(parts[4]);
- canvas.add(id, color, width, height);
- } else if (type == 3) {
- float scaleFactor = Float.parseFloat(parts[2]);
- System.out.println("ORIGNAL:");
- System.out.print(canvas);
- canvas.scale(id, scaleFactor);
- System.out.printf("AFTER SCALING: %s %.2f\n", id, scaleFactor);
- System.out.print(canvas);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment