java形状_java – 面向对象的设计 – 形状

因此,经过多年的OOP,我从一个大学课程中完成了一个非常简单的家庭作业,以实现一个简单的面向对象的结构.

要求的设计:

>实施面向对象的解决方案,以创建以下形状:

>椭圆,圆,正方形,矩形,三角形,平行四边形.

>创建的每个形状必须具有以下参数:唯一ID,颜色.

以下功能:颜色变化,移动,区域,周长,内部,复制.

不需要有效性测试(不在类中,也不在用户输入中).

我的设计:

78443e1ca92a68694ac7049a3bfe4846.png

总的来说,一个非常简单的方法,shape_class / non-circular是抽象的,矩形/方形被组合成一个类,因为它们包含完全相同的参数,并且不需要有效性测试(没有理由将它们分成两个).

Shape类 – 实现静态id(唯一id)和处理颜色名称的init函数.

public abstract class shape_class {

static int STATIC_ID;

int id;

String color_name;

public shape_class(String color_name_input) {

this.id = STATIC_ID;

shape_class.STATIC_ID+=1;

if (Arrays.asList(toycad_globals.ALLOWED_COLORS).contains(color_name_input))

{

this.color_name = color_name_input;

}

}

public void change_color(String color_name_input) {

if (Arrays.asList(toycad_globals.ALLOWED_COLORS).contains(color_name_input)) {

this.color_name = color_name_input;

}

}

public abstract shape_class return_copy();

public abstract void move(double x, double y);

public abstract double area();

public abstract double circumference();

public abstract boolean is_inside(double x, double y);

}

**非圆形** – 接收一系列点(定义对象)并实现几乎所有必需的功能.

public abstract class non_circullar extends shape_class {

List line_list = new ArrayList();

List point_list = new ArrayList();

non_circullar(String color_name, point...input_point_list) {

super(color_name);

this.point_list = Arrays.asList(input_point_list);

for (int current_index =0; current_index< (input_point_list.length); current_index++) {

point current_first_point = input_point_list[current_index];

point current_second_point = input_point_list[(current_index+1)%input_point_list.length];

this.line_list.add(new line(current_first_point, current_second_point));

}

}

public point[] get_point_list_copy() {

int index = 0;

point [] new_array = new point[this.point_list.size()];

for (point current_point:this.point_list) {

new_array[index] = current_point.return_copy();

index+=1;

}

return new_array;

}

public double circumference() {

double sum = 0;

for (line current_line :this.line_list) {

sum += current_line.get_length();

}

return sum;

}

public void move(double x, double y) {

for (point current_point :this.point_list) {

current_point.move(x, y);

}

}

public boolean is_inside(double x, double y) {

int i;

int j;

boolean result = false;

for (i = 0, j = this.point_list.size() - 1; i < this.point_list.size(); j = i++) {

if ((this.point_list.get(i).y > y) != (this.point_list.get(j).y > y) &&

(x < (this.point_list.get(j).x - this.point_list.get(i).x) * (y - this.point_list.get(i).y) /

(this.point_list.get(j).y-this.point_list.get(i).y) + this.point_list.get(i).x))

{

result = !result;

}

}

return result;

}

int get_top_left_line_index() {

int top_left_line_index = 0;

int index = 0;

point best_point = this.line_list.get(0).get_average_point();

point current_point;

for (line current_line :this.line_list) {

current_point = current_line.get_average_point();

if (current_point.x < best_point.x) {

best_point = current_point;

top_left_line_index = index;

} else if (current_point.x == best_point.x && current_point.y > best_point.y) {

best_point = current_point;

top_left_line_index = index;

}

index +=1;

}

return top_left_line_index;

}

}

问题:

对于这项任务,设计问题减少了40分:

1)圆是一个椭圆,因此需要从中继承(即使它们不共享参数).

2)矩形/正方形是两个不同的实体,即使在这个实现中它们完全相同(没有有效性测试).

我很乐意从社区获得有关此设计的一些意见,设计问题是否“合法”,哪些可以做得更好?

编辑1:

椭圆表示为:两个点和d(对于椭圆上的点,它与两个点之间的距离必须等于d).

圆圈表示为:中心和半径.

我发现很难理解他们如何分享共同的障碍.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值