J
J
jspie2018-01-19 14:35:01
Java
jspie, 2018-01-19 14:35:01

How to properly organize the hierarchy of classes and methods of the square?

It is necessary to develop a class Square, line and point. The square must have methods: resize, rotate. You need to do this without a graphical interface. You just need to display the coordinates or the coefficient of change. I don't know how it's easier to organize it all. So far it turns out something like this:

package com.company;

import java.util.ArrayList;
import java.util.List;

class Rect{
    private int size = 0;
    private int rot = 0;

    List<Line> lines;

    public Rect(){}

    public Rect(int _size){
        this.size = _size;

        lines = new ArrayList<Line>();

        Line lines1 = new Line(0,0, 0, _size);
        lines.add(lines1);

        Line lines2 = new Line(0, _size, _size, _size);
        lines.add(lines2);

        Line lines3 = new Line(_size , _size, _size, _size);
        lines.add(lines3);

        Line lines4 = new Line(_size,0, 0,0);
        lines.add(lines4);
    }

    public void setSize(int _size) {
        this.size = _size;

        for(int i = 0; i < 4; i++)
        {
            System.out.println(lines.get(i).getLine());
        }
    }

    public void Rotation(int u){

    }

    public void Show()
    {

    }
}

class Dot {
    private int x = 0;
    private int y = 0;

    public Dot(int _x, int _y) {
        this.x = _x;
        this.y = _y;
    }

    public int getX() { return x; }

    public void setX(int x) { this.x = x; }

    public int getY() { return y; }

    public void setY(int Y) { this.y = y; }

    @Override
    public String toString() {
        return getClass().getSimpleName() + "{x: " + x
                + ", y: " + y +
        "}";
    }
}

class Line {
    Dot dot1;
    Dot dot2;

    List<Dot> dots;

    public Line(){

    }

    public Line(int x1, int y1, int x2, int y2) {
        dot1 = new Dot(x1, y1);
        dot2 = new Dot(x2, y2);

        dots = new ArrayList<Dot>();

        dots.add(dot1);
        dots.add(dot2);
    }

    public void setLine(int x1, int y1, int x2, int y2) {
        dots = new ArrayList<Dot>();

        dot1.setX(x1);
        dot1.setY(y1);

        dot2.setX(x2);
        dot2.setY(y2);

        dots.add(dot1);
        dots.add(dot2);
    }

    public List<Dot> getLine()
    {
        return dots;
    }
}
public class Main {

    public static void main(String[] args) {
        Rect rect = new Rect(10);
        rect.setSize(12);

    }
}

But I ran into problems when I can not change the current coordinates, etc. Most likely I did not organize the structure correctly. Help a newbie

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kryukov, 2018-01-19
@jspie

The simplest (head-on) approach would be to make one structure. I'll omit all getters, setters, and other Java stuff, just to keep the code as simple as possible. In general, they are more than worth using.

class Rect {
    public int aX;
    public int aY;
    public int bX;
    public int bY;
    public int cX;
    public int cY;
    public int dY;
    public int dX;
}

And then turn back from this data structure the things you already need. Again, I will omit all unnecessary and leave only the method itself (for example, multiplication).
public Rect multiply(Rect source, float amount) {
        Rect result = new Rect();
        result.aX = source.aX * amount;
        result.aY = source.aY * amount;
        result.bX = source.bX * amount;
        result.bY = source.bY * amount;
        result.cX = source.cX * amount;
        result.cY = source.cY * amount;
        result.dX = source.dX * amount;
        result.dY = source.dY * amount;
        return result;
    }

If you want a more "object-oriented" structure, then you first need to understand which objects in it will actually have behavior. Well, that is, for example, in the classic examples with cats-dogs-animals, for some reason no one seeks to make a language object in a cat, which will be responsible for a specific "Meow". It is the same here, if a point (as an object) does not carry any functionality by itself, it is not at all a fact that it is needed. Accordingly, the first thing that is asked here is to collect a square (or, for that matter, a polygon) as just a collection of edges. Then something like this will happen:
class Edge {
    public float aX;
    public float aY;
    public float bX;
    public float bY;
}

class Poly extends ArrayList<Edge> {
    public Poly multiply(Poly source, float amount) {
        Poly result = new Poly();
        for (Edge edge : source) {
            Edge newEdge = new Edge();
            newEdge.aX = edge.aX * amount;
            newEdge.aY = edge.aY * amount;
            newEdge.bX = edge.bX * amount;
            newEdge.bY = edge.bY * amount;
            result.add(newEdge);
        }
        return result;
    }
}

The problem with this approach, as I described above, is that the point itself does not exist and cannot contain logic. For example, if you need to move a point on the screen, then it will not be easy to implement this logic in this case, because you have to look for all the edges that are affected by this. This can be solved if we introduce a class of points, which will turn our code into something like this:
class Dot {
    public float x;
    public float y;
}

class Edge {
    public Dot a;
    public Dot b;
}

class Poly extends ArrayList<Dot> {
    public List<Edge> edges = new ArrayList<>();

    public Poly multiply(Poly source, float amount) {
        Poly result = new Poly();
        // Map just to remember old dots bindings
        Map<Dot, Dot> newDots = new HashMap<>();
        for (Dot dot : source) {
            Dot newDot = new Dot();
            newDot.x = dot.x * amount;
            newDot.y = dot.y * amount;
            result.add(dot);
            newDots.put(dot, newDot);
        }
        
        for (Edge edge : edges) {
            Edge newEdge = new Edge();
            newEdge.a = newDots.get(edge.a);
            newEdge.b = newDots.get(edge.b);
            result.edges.add(newEdge);
        }
        
        return result;
    }
}

As you can see, adding a new entity made the whole code more complicated, and cloning an object much more complex. Although now it is possible to change the point and not think about the fact that some edges will be forgotten.
In general, all data structures should primarily be based on how this data will be used.
This code is given solely as an answer to the question and to think, you should not write this way in real applications. I hope the answer is helpful ;)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question