• ベストアンサー

Java言語プログラミングについて質問です

以下の問で以下のようなソースを作りました。 問.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がわかりません。どうしたらいいのですか?解説とソースをお願いします。汚いソースで買得も難しいと思いますがよろしくお願いします。

  • Java
  • 回答数3
  • ありがとう数1

質問者が選んだベストアンサー

  • ベストアンサー
  • jjon-com
  • ベストアンサー率61% (1599/2592)
回答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); } } } } } } }

saresio-_-
質問者

お礼

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

その他の回答 (2)

  • kmee
  • ベストアンサー率55% (1857/3366)
回答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://okwave.jp/qa/q7826345.html でしたはずですが、なおってません。 ついでに気付きましたが > public double getPerimeter(){ > return 2 * (ly - ry) + 2* (lx - rx); > } では長さが負になることがあります。 問4 は、まず数学(算数?)の図形の問題を解いて「2つの円が重なる」とは、どんな状態か考えます。 2つの円の中心の距離が、 なんかより大きければ、共通部分はない なんかに等しければ接する なんか未満なら重なっている でしたよね?

  • Tacosan
  • ベストアンサー率23% (3656/15482)
回答No.1

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

関連するQ&A

  • Java言語プログラミングに関して質問です

    以下の問いでソースを作りました。 問.2次元上の円と長方形を表すデータファイルが与えられたとき、以下の処理を行うプログラムを作成せよ。ただし、長方形は必ず座標軸に並行変を持つとする。 1.全図形の合計面積を表示せよ 2.一番面積の大きい図形データを表示せよ 3.周の長さ順に図形データを表示せよ 4.重なり合ってるすべての円のペアを列挙せよ 5.重なり合っているすべての図形のペアも列挙せよ 図形データの例 1. 1, 50.0, 50.0, 50.0 2. 2, -100.0, 100.0, 0.0, 0.0 3. 1, 0.0, 0.0, 10.0 4. 1, 50.0, 0.0, 30.0 5. 2, -75.0, 75.0, -25.0, 25.0 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 この問題ですが、周の長さの大きい順は2→1→5→4→3になり、問4、5ができていません。どうしたらよろしいですか?教えてください。解説とソースをお願いします。

  • パッケージ化とコンパイルについて

    下記のプログラムにについての質問になります。 /*Rectangle.java*/ package com.shoeisha.shape; public class Rectangle { private int width; private int height; public Rectangle() { } public Rectangle(int width, int height) { this.width = width; this.height = height; } public int getArea(){ return width * height; } } /*Test.java*/ import com.shoeisha.shape.*; ^^(1) class Test { public static void main(String[] args) { Rectangle rc = new Rectangle(10, 15); System.out.println(rc.getArea()); } } (1)の箇所になりますが、com.shoeisha.shapeにあるクラス(Rectangle)を 利用し、プログラムを作成しようと思うのですが、Test.javaにてコンパイルエラーが生じてしまいます。 (1)の箇所をRectangleにするとコンパイルが通るようになり、「*」は使えないものかと思えてしまいます。 java.util.*;といった、既に用意されているものは普通に使えるのですが、自作でパッケージ化した物は 正規表現を使うことができないものなんでしょうか?? 何かの設定がされていないから、正規表現が使えないのではと思っておりますが、 それ以上がわかりませんでしたので、掲示板にて記載させていただきました。 以上、ご教授の程お願い致します。

    • ベストアンサー
    • Java
  • javaのreturnはどんなことをするんですか?

    javaのreturnはどんなことをするんですか? この内容でthisも知りたいですが 簡単に教えてください!!!!! public class Circle3 { int r ; public Circle3(){ this(0); } public Circle3(int r){ this.r = r; } public double getCircumference(){ return 3.14 * 2* r; } public double getArea(){ return 3.14*r*r; } public int getRadius(){ return r; } public void setRadius(int r) { this.r = r; } }

    • ベストアンサー
    • Java
  • プログラミングについての質問

    C言語において、区分求積法・台形公式・シンプソンの公式を行いたいのですがうまくいきません。 1/1+x*xを求めたいと思います。以下が途中まで作ったプログラムです。 #include <stdio.h> #define FROM 0.0 #define TO 1.0 double func(double x) { double out; out = 1.0 / ( 1.0 + x * x ); return (out); } double kubun(double start, double end, int num) { int i; double h, s; h = ( end - start ) / num; s = 0.0; for(i=0; i<num; i++) s += func( start + i * h + h / 2.0 ); return ( s * h ); } double daikei(double start,double end,int num) { int i; double h,s; h = ( end - start ) / num; s = 0.0; for(i=1; i<num-1; i++) s += func( i * h ); return ((start / 2.0 + s + end / 2.0) * h ); } double simpson(double start,double end,int num) { int i; double h,s; h = ( end - start ) / num; s = 0.0; for(i=1; i<num-1; i++) if(i%2 == 0){ s = 2 * func(i * h); }else{ s = 4 * func(i * h); } return ( (start + s + end) / 3 ); } 区分求積法はあったていると思いますが、不安なのでのせときます。 よろしくお願いします。

  • このプログラミングって正しい?

    与えられた実行列X,Y,Z(配列名x、y、z)から、X-YZを計算して、行列W(配列名w)に格納するメソッドを完成する。 Public static void XminusXY( double[][] x,double[][] y,double[][] z, double[][] w){ double wij; int el=z[0].length, m=y.length, n=z.length; for(int i = 0; i<m; i++){ for(int j = 0; j<n-1; j++){ wij=xij for(int k=0; k<el; k++) wij += -y[i][k]*z[k][j]; w[i][j]=wij; } } }

  • c言語プログラミングについて

    ニュートン法でx^2-2の根を初期値2、試行回数を200回までで求めるプログラムを作成しているのですがうまくいきません。 どなたかご教授お願いいたします。 #include<stdio.h> #include<math.h> int main() { int i; int N = 200; double x = 2.0; double xnew; double eps = 1e-16; for (i = 0; i < N; i++) { xnew = x - ( x * x - 2.0 ) / 2.0 * x; if (fabs(x - xnew) < eps) break; x = xnew; } printf("x=%.20lf\n", xnew); return(0); }

  • C言語プログラミングについて

    「要素数10の配列を準備する。 配列の各要素に0.0~1.0の乱数を入れる。 各配列に入力された乱数を出力する。 配列をオリジナル関数hanteiに渡す。 数hantei内において,各要素の値が0.5以上であれば1 , 0.5未満であれば0を出力する。」 C言語でこのような問題があるのですがどのように作ればよろしいのでしょうか? ちなみに以下のように作ってみて、「argcは一度も使用されていない」「argvは一度も使用されていない」とエラー(警告)が出てしまいました。修正、もしくは正しいプログラムを教えてくれませんか? #include <stdio.h> #include <time.h> #include <stdlib.h> #define SIZE 10 int hantei(double num) { return num>=0.5; } int main(int argc,char *argv[]) { double number[SIZE]; int i; srand((unsigned)time(NULL)); for(i=0;i<SIZE;i++) number[i]=(double)rand() / RAND_MAX; //手抜き乱数 for(i=0;i<SIZE;i++) { printf("%f %d\n",number[i], hantei(number[i])); } return 0; }

  • java の配列についての質問です。

    java の配列についての質問です。 java 及び C++についての知識は初心者です。 public abstract class Plan implements Shapeable { Shape[] shape = new Shape[5]; Shape[0] = new Rectangle(10,10,10,10); // x,y 座標 及び 長さ、高さ Shape[1] = new Triangle(10,10,30,10,20,20); // 3点間の座標        Shape[2] = new Triangle(20,10,40,10,30,20);        Shape[3] = new Triangle(15,15,35,15,25,25); Shape[4] = new Circle(20,20,20); // 円の座標と半径     public float[] calculaterAreas(){ ??????????????????? } 内容としては複雑なのですが、三角形、四角形、円の面積をShapeというclassの配列にあるデータ(座標や長さなど) を使ってShapeの配列のデータを置き換えてfloatのデータとして返すんですが・・・・。どうやっていいのかさっぱり わかりません。面積については、Rectangle,Triangle,Circleのクラスでそれぞれ計算できるようになっているのですが、それを持ってくる方法もわかりません・・・どうしたらいいのか教えてください 宜しくお願いします。 ちなみに Rectangle,Triangle,Circle class の area の メソットは --- Rectangle class public class Rectangle extends Shape{ int x; int y; int width; int height; public float area(Rectangle r) { return r.width * r.height; } --- Circle class public class Circle extends Shape{ int x; int y; int radius; public float area(Circle c) { return (float) (c.x * c.y * 3.14); } } --- Triangle class public class Triangle extends Shape{ int x; int y; int x2; int y2; int x3; int y3; public float area(Triangle t) { float dt1,dt2,dt3; float s1; float area1; dt1 = Point.distance(x, y); // Point は dt2 = Point.distance(x2, y2); dt3 = Point.distance(x3, y3); s1 = (dt1+dt2+dt3)/2; area1 = (float) Math.sqrt((s1-dt1)*(s1-dt2)*(s1-dt3)); return area1; } } *** Shape class public abstract class Shape implements Shapeable { public abstract float area(Shape obj); } *** Shapeable インターフェイス public interface Shapeable { float area (Shape obj); } ** コンストラクター等は省略してあります。

    • ベストアンサー
    • Java
  • javaプログラミングの質問です。

    プログラムで数値の奇遇、合計値、最大値までは出せたのですが平均値の出し方がわかりません。 どこに何を入れればいいかを教えてください。お願いします。 public class pazu{ public static void main(String[] args){ int sum =0,saidai; System.out.println("コマンドラインパラメータは"+args.length+"個です"); for(int i=0;i<args.length;i++){ int x=Integer.parseInt(args[i]); if(pazu.is_even(x)) System.out.println(args[i]+"は偶数です"); else System.out.println(args[i]+"は奇数です"); sum+=x; } saidai=pazu.max(args); System.out.println("合計:"+sum); System.out.println("最大:"+saidai); } static boolean is_even(int number){ return number%2==0; } static int max(String[] number){ int max =0; for(int i=0;i<number.length;i++){ if(max<Integer.parseInt(number[i])){ max=Integer.parseInt(number[i]); } } return max; } }

    • ベストアンサー
    • Java
  • C言語のプログラムなのですが、2点の座標(SとR)を指定して片方からも

    C言語のプログラムなのですが、2点の座標(SとR)を指定して片方からもう片方への道筋を座標でcsvファイルに記述していくプログラムを作りたいのですが、うまく動きません。 参照渡しの所が間違っている様な気もするのですがわかりません。わかる方いましたらご指摘ください! エラー内容:Segmentation Fault(core dumped) #include<stdio.h> #include<stdlib.h> #include<math.h> #include<time.h> int Fugo(int); void Flooding(int *,int *,int *,int *); typedef struct{ int id; int dX; int dY; }NodeStates; NodeStates Node1[50]; NodeStates Node2[50]; int main(void) { int SX1,SY1; int RX1,RY1; int SX2,SY2; int RX2,RY2; int i=1; int j=1; FILE *fp; fp=fopen("test.csv","w"); if(fp!=NULL){ printf("S1 Node PointX>>"); scanf("%d",&SX1); printf("S1 Node PointY>>"); scanf("%d",&RX1); printf("R1 Node PointY>>"); scanf("%d",&RY1); Node1[0].id = 1; Node1[0].dX = SX1; Node1[0].dY = SY1; printf("S2 Node PointX>>"); scanf("%d",&SX2); printf("S2 Node PointY>>"); scanf("%d",&SY2); printf("R2 Node PointX>>"); scanf("%d",&RX2); printf("R2 Node PointY>>"); scanf("%d",&RY2); Node2[0].id=1; Node2[0].dX=SX2; Node2[0].dY=SY2; srand((unsigned int)time(NULL)); fprintf(fp,"First\n"); fprintf(fp,"NodeID,X,Y\n"); fprintf(fp,"%d,%d,%d\n",Node1[0].id, SX1, SY1); while(!(SX1==RX1 && SY1==RY1)){ Flooding(&SX1, &SY1, &RX1, &RY1); Node1[i].dX=SX1; Node1[i].dY=SY1; Node1[i].id=i+1; fprintf(fp,"%d,%d,%d\n",Node1[i].id,Node1[i].dX,Node1[i].dY); i++; } fprintf(fp,"Second\n"); fprintf(fp,"NodeID,X,Y\n"); fprintf(fp,"%d,%d,%d\n",Node2[0].id, SX2, SY2); while(!(SX2==RX2 && SY2==RY2)){ Flooding(&SX2,&SY2,&RX2,&RY2); Node2[j].dX=SX2; Node2[j].dY=SY2; Node2[j].id=j+1; fprintf(fp,"%d,%d,%d\n",Node2[j].id,SX2,SY2); j++; } } fclose(fp); return 0; } void Flooding(int *a,int *b,int *c,int *d){ int *e,*f,r; *a=*a-*c; *b=*b-*d; *e=Fugo(*a); *f=Fugo(*b); *a=abs(*a); *b=abs(*b); r=rand()%3; if(*a!=0&&*b!=0){ if(r==0){ *a=*a-1; } else if(r==1){ *a=*a-1; *b=*b-1; } else *b=*b-1; } else if(*a==0)*b=*a-1; else if(*b==0)*a=*b-1; *a=*a**e+*c; *b=*b**f+*d; } int Fugo(int a){ int n; if(a>=0){ n=1; } else{ n=-1; } return n; }

専門家に質問してみよう