dポイントプレゼントキャンペーン実施中!

以下の問で以下のようなソースを作りました。
問.2次元上の円と長方形を表すデータファイルが与えられたとき、以下の処理を行うプログラムを作成せよ。ただし、長方形は必ず座標軸に並行変を持つとする。
1.全図形の合計面積を表示せよ
2.一番免責の大きい図形データを表示せよ
3.周の長さ順に図形データを表示せよ
4.重なり合ってるすべての円のペアを列挙せよ
class Shape{

int id;
int type;
public double getArea() { return 0.0; }

public double getPerimeter() { return 0.0; }
}
class Circle extends Shape{
double cx,cy;
double r;

Circle(int id, int type, double cx, double cy, double r){
this.id = id;
this.type = type;
this.cx = cx;
this.cy = cy;
this.r = r;
}

public double getArea(){
return 3.14159265 * r * r;
}

public double getPerimeter(){
return 3.14159265 * 2 * r;
}
}
class Rectangle extends Shape{
double lx,ly,rx,ry;
Rectangle(int id,int type,double lx,double ly,double rx,double ry){
this.id = id;
this.type = type;
this.lx = lx;
this.ly = ly;
this.rx = rx;
this.ry = ry;
}

public double getArea(){
return (rx - lx) * (ly - ry);
}

public double getPerimeter(){
return 2 * (ly - ry) + 2* (lx - rx);
}
}
class ShapeTest{
public static void main(String[] args) {
Shape shapes[] = new Shape[5];
shapes[0] = new Circle(1, 1, 50.0, 50.0, 50.0);
shapes[1] = new Rectangle(2, 2, -100.0, 100.0, 0.0, 0.0);
shapes[2] = new Circle(3, 1, 0.0, 0.0, 10.0);
shapes[3] = new Circle(4, 1, 50.0, 0.0, 30.0);
shapes[4] = new Rectangle(5, 2, -75.0, 75.0, -25.0, 25.0);

double sum = 0.0;
for(int i=0;i<shapes.length;i++){
sum += shapes[i].getArea();
}
System.out.println("合計面積は " +sum);

Shape maxShape = shapes[0];
for(int i=1;i<shapes.length;i++){
if(shapes[i].getArea() > maxShape.getArea()){
maxShape = shapes[i];
}
}
System.out.println("一番面積の大きい図形は ID=" + maxShape.id);

for(int i=0;i<shapes.length-1;i++){
   for(int j=i+1;j<shapes.length;j++){
    if(shapes[j].getArea() > shapes[i].getArea()){
     Shape tmp = shapes[i];
     shapes[i] = shapes[j]; 
     shapes[j] = tmp;
     }
    }
   }
System.out.println("周の長さの大きい順は ");
for(int i=0;i<shapes.length;i++){
System.out.print(" " +shapes[i].id);
}
System.out.println();
}

}

合計面積は 23495.574275
一番面積の大きい図形は ID=2
周の長さの大きい順は
2 1 4 5 3

問3まではできたのですが、問4がわかりません。どうしたらいいのですか?解説とソースをお願いします。汚いソースで買得も難しいと思いますがよろしくお願いします。

A 回答 (3件)

質問者による過去の質問のいくつかは拝見しています。



寄せられた回答に対して補足もお礼も一言も返さず,ただ質問を締め切る,という態度には良い印象を持っていません。

しかし,基礎もなんにも分からぬまま課題を丸投げしているのではなく,数十行にわたるJavaのコードが書ける人物のようですので,解答例を示してみる気になりました。

class Shape {
int id;
int type;
}

class Circle extends Shape{
double cx,cy;
double r;

Circle(int id, int type, double cx, double cy, double r){
this.id = id;
this.type = type;
this.cx = cx;
this.cy = cy;
this.r = r;
}

boolean isOverlap(Circle that) {
if (this.r + that.r > Math.sqrt(Math.pow(this.cx - that.cx, 2) + Math.pow(this.cy - that.cy, 2))) {
return true;
} else {
return false;
}
}
}

class Rectangle extends Shape{
double lx,ly,rx,ry;

Rectangle(int id,int type,double lx,double ly,double rx,double ry){
this.id = id;
this.type = type;
this.lx = lx;
this.ly = ly;
this.rx = rx;
this.ry = ry;
}
}

class Q7831321 {
public static void main(String[] args) {
Shape shapes[] = new Shape[5];
shapes[0] = new Circle(1, 1, 50.0, 50.0, 50.0);
shapes[1] = new Rectangle(2, 2, -100.0, 100.0, 0.0, 0.0);
shapes[2] = new Circle(3, 1, 0.0, 0.0, 10.0);
shapes[3] = new Circle(4, 1, 50.0, 0.0, 30.0);
shapes[4] = new Rectangle(5, 2, -75.0, 75.0, -25.0, 25.0);

for (int i = 0; i < shapes.length - 1; i++) {
if (shapes[i] instanceof Circle) {
for (int j = i + 1; j < shapes.length; j++) {
if (shapes[j] instanceof Circle) {
if (((Circle)shapes[i]).isOverlap((Circle)shapes[j])) {
System.out.printf("円ID=%dと円ID=%dは重なる\n", shapes[i].id , shapes[j].id);
}
}
}
}
}
}
}
    • good
    • 0
この回答へのお礼

いつもお礼を入力せずすいませんでした。これからは気をつけます。ありがとうございました。

お礼日時:2013/07/21 10:13

4は半径30の円だから、円周は 2 * PI * 30 ≒ 188


5は1辺が50の長方形(正方形)だから、周の長さは 2 * 50 + 2*50 = 200
よって、 5 > 4 です。

> 周の長さの大きい順は
> 2 1 4 5 3
では 4 > 5となってますから、事実と異ります


って話を
http://oshiete.goo.ne.jp/qa/7826345.html
でしたはずですが、なおってません。

ついでに気付きましたが
> public double getPerimeter(){
> return 2 * (ly - ry) + 2* (lx - rx);
> }
では長さが負になることがあります。


問4 は、まず数学(算数?)の図形の問題を解いて「2つの円が重なる」とは、どんな状態か考えます。
2つの円の中心の距離が、
なんかより大きければ、共通部分はない
なんかに等しければ接する
なんか未満なら重なっている

でしたよね?
    • good
    • 0

「問4がわかりません」というのは, 具体的にはどこがどうわからないのですか?

    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!