以下の問で以下のようなソースを作りました。
問.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がわかりません。どうしたらいいのですか?解説とソースをお願いします。汚いソースで買得も難しいと思いますがよろしくお願いします。
No.2ベストアンサー
- 回答日時:
質問者による過去の質問のいくつかは拝見しています。
寄せられた回答に対して補足もお礼も一言も返さず,ただ質問を締め切る,という態度には良い印象を持っていません。
しかし,基礎もなんにも分からぬまま課題を丸投げしているのではなく,数十行にわたる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);
}
}
}
}
}
}
}
No.3
- 回答日時:
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つの円の中心の距離が、
なんかより大きければ、共通部分はない
なんかに等しければ接する
なんか未満なら重なっている
でしたよね?
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・14歳の自分に衝撃の事実を告げてください
- ・架空の映画のネタバレレビュー
- ・「お昼の放送」の思い出
- ・昨日見た夢を教えて下さい
- ・ちょっと先の未来クイズ第4問
- ・【大喜利】【投稿~10/21(月)】買ったばかりの自転車を分解してひと言
- ・メモのコツを教えてください!
- ・CDの保有枚数を教えてください
- ・ホテルを選ぶとき、これだけは譲れない条件TOP3は?
- ・家・車以外で、人生で一番奮発した買い物
- ・人生最悪の忘れ物
- ・【コナン30周年】嘘でしょ!?と思った○○周年を教えて【ハルヒ20周年】
- ・ハマっている「お菓子」を教えて!
- ・最近、いつ泣きましたか?
- ・夏が終わったと感じる瞬間って、どんな時?
- ・10秒目をつむったら…
- ・人生のプチ美学を教えてください!!
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・都道府県穴埋めゲーム
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
実数からの小数部の取得
-
0dの意味を教えてください
-
javaでのプログラミング
-
System.err. printlnとSystem.o...
-
ダブルクォーテーションのrepla...
-
オブジェクトの中のプロパティ...
-
javaで質問です。 文字列2023/2...
-
C言語のポインターに関する警告
-
パソコンキーボードで時分秒を...
-
Javaで改行などが出来ないのです。
-
変数を動的に利用するには?
-
JPanelの重ね方/OverlayLayout他
-
JSPやサーブレットでSystem.out...
-
ダブルクォーテーションを含む...
-
IF関数でEmpty値を設定する方法。
-
Path型をString型へ変換する(Java)
-
System.out.printlnの意味がよ...
-
動的配列が存在(要素が有る)か...
-
ORA-01858: 数値を指定する箇所...
-
EXCEL VBA で、0から?1から?
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
0dの意味を教えてください
-
実数からの小数部の取得
-
べき乗
-
Javaで何パーセント%かを表示...
-
Javaコンパイルエラー
-
最大値と最小値の求め方
-
double型変数値の整数部分のみ...
-
double型の足し算について
-
javaでC++のdefine文に相当する...
-
java のfor文について質問です。
-
java プログラム 範囲を指定し...
-
Javaのeval関数処理
-
Math.sin() を使った計算が合わ...
-
doubleとintの違い
-
自然対数の底Eを含むStringの数...
-
割り算
-
四捨五入
-
プログラムのおかしいところを...
-
doubleで入力するには?
-
実数 2.0 に対して int(2.0) ==...
おすすめ情報