Answer the question
In order to leave comments, you need to log in
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);
}
}
Answer the question
In order to leave comments, you need to log in
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;
}
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;
}
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;
}
}
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;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question