• 締切済み

Javaの内積についてです

今下記のようになっていてgetInnerProductが内積を求めるメソッドです。 public class MyVector { private double[] elements; static private int count = 0; public MyVector(){ elements = new double[2]; elements[0] = 0.0; elements[1] = 0.0; } public MyVector(double x, double y){ elements = new double[2]; elements[0] = x; elements[1] = y; } double getLength(){ return Math.sqrt(elements[0] * elements[0] + elements[1] * elements[1]); } int getInnerProduct(MyVector v){ return (this.elements[0] * v.elements[0] + this.elements[1] * v.elements[1]); } } このように作ったのですがエラーが出てくるので、何が違うか、どうなれば動くか教えてください

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

みんなの回答

  • salsberry
  • ベストアンサー率69% (495/711)
回答No.1

> エラーが出てくる そのエラーメッセージをちゃんと読みましょう。読んでも分からなければ、エラーメッセージで検索するか、質問時にエラーメッセージを書きましょう。 getInnerProduct()の返値型と、実際にreturnしようとしている式の型を見比べてください。

関連するQ&A

  • javaの図形なのですが

    javaの図形なのですが、結果が球の形にそってぐるぐるっと リンゴの皮むきのような感じの線になるはずなのですが動かない、 というよりもどこを修正すればいいのか見当もつかずさまよっています。正直、助けていただきたいです。 たぶん抜けてるところがあるはずなんですが、何が抜けてて何を入れれば良いかわからないので教えてほしいです。 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ui {public static void main(String[] args) { JFrame jf = new JFrame("ui"); MyCanvas mc = new MyCanvas(); mc.setPreferredSize(new Dimension(700,700)); jf.getContentPane().add(mc); jf.pack(); jf.setVisible(true); } } class MyCanvas extends JPanel { static double [][] p = { {ここに数値を入れるのですが多いので投稿できないので省きます} }; double _xa = 30; double _ya = 40; int doX, doY; public void paintComponent( Graphics g ) { super.paintComponent(g); setBackground(Color.black); g.setColor(Color.white); disp_3Dobj(_xa, _ya, getWidth()/2, getHeight()/2, g);( } void disp_3Dobj(double xa, double ya, int px, int py, Graphics g) { int [] p2d; int n = p[0].length; for (int i = 0; i < n; i++) { } } int [] get2Dpos(double x, double y, double z, double xa, double ya, int px, int py) { final double D2Rad = Math.PI/180.0; double a = xa*D2Rad, b = ya*D2Rad; double x1, y1, zt; double sinA = Math.sin(a), sinB = Math.sin(b); double cosA = Math.cos(a), cosB = Math.cos(b); int [] pos = new int[2]; x1 = x*cosB + z*sinB; zt = -x*sinB + z*cosB; y1 = y*cosA - zt*sinA; pos[0] = px+(int)Math.rint(x1); pos[1] = py-(int)Math.rint(y1); return pos;}}

  • Javaの基礎のプログラム

    結城浩さんの本に載っていたプログラムです。 ですが、説明がわかりにくくていまいち処理がわからなかったので 質問させていただきました。 どうやら、X軸とY軸がありY軸は下に行けばいくほど数字が大きくなる。 X軸は右に行けばいくほど大きくなる。座標上にある、二つの長方形の重なりあう部分の座標を求めるプログラムのようです。(イメージとしては、下に添付してある画像のような感じになるようです。) 質問としては一つです。 プログラム中のこの部分なのですが、おそらく「長方形の座標を示している」行であると思います。 a = new Rectangle(0, 0, 20, 10); b = new Rectangle(5, 5, 20, 10); c = new Rectangle(20, 10, 20, 10); d = new Rectangle(-10, -20, 100, 200); e = new Rectangle(21, 11, 20, 10); その後の処理としては、aとb→c→d→eというようにそれぞれ比較していっているのもなんとなくわかります。 ですが、しっかり解説されていなかったので、この4つの数字がどのように座標を示しているのかよくわかりませんでした。 よろしくお願いします。 class Rectangle { final int INITIAL_WIDTH = 10; final int INITIAL_HEIGHT = 20; int width; int height; int x; int y; Rectangle() { width = INITIAL_WIDTH; height = INITIAL_HEIGHT; x = 0; y = 0; } Rectangle(int width, int height) { this.width = width; this.height = height; this.x = 0; this.y = 0; } Rectangle(int x, int y, int width, int height) { this.width = width; this.height = height; this.x = x; this.y = y; } void setLocation(int x, int y) { this.x = x; this.y = y; } void setSize(int width, int height) { this.width = width; this.height = height; } public String toString() { return "[" + x + ", " + y + ", " + width + ", " + height + "]"; } Rectangle intersect(Rectangle r) { int sx = Math.max(this.x, r.x); int sy = Math.max(this.y, r.y); int ex = Math.min(this.x + this.width, r.x + r.width); int ey = Math.min(this.y + this.height, r.y + r.height); int newwidth = ex - sx; int newheight = ey - sy; if (newwidth > 0 && newheight > 0) { return new Rectangle(sx, sy, newwidth, newheight); } else { return null; } } public static void main(String[] args) { Rectangle a, b, c, d, e; a = new Rectangle(0, 0, 20, 10); b = new Rectangle(5, 5, 20, 10); c = new Rectangle(20, 10, 20, 10); d = new Rectangle(-10, -20, 100, 200); e = new Rectangle(21, 11, 20, 10); System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); System.out.println("e = " + e); System.out.println("a と a の重なり = " + a.intersect(a)); System.out.println("a と b の重なり = " + a.intersect(b)); System.out.println("a と c の重なり = " + a.intersect(c)); System.out.println("a と d の重なり = " + a.intersect(d)); System.out.println("a と e の重なり = " + a.intersect(e)); } }

    • ベストアンサー
    • Java
  • C#のプログラミングについて(基礎・・)

    どうしても エラーが出ます エラーの表示は 下のようなものなのですが、どういう意味かわかりません。 C:\Documents and Settings\gc60117\デスクトップ\Project3\CodeFile1.cs(20): 引数を '2' 個指定できる、メソッド 'Vector' のオーバーロードはありません。  自分なりのプログラミングデータを記述します。 間違いを指摘していただけると助かります。 using System; class Vector { private double x; private double y; public double Length { get{return Math.Sqrt(x*x+y*y);} } public void Write() { Console.Write("({0},{1})",x,y); } public static Vector I { get{return new Vector(1,0);} } public static Vector J { get{return new Vector(0,1);} } public static Vector operator*(double k,Vector a) { Vector t= new Vector(); t.x=k*a.x; t.y=k*a.y; return t; } public static double operator*(Vector a, Vector b) { double z= new double(); z=a.x*b.x+a.y+b.y; return z; } public static Vector operator+(Vector a,Vector b) { Vector u=new Vector(); u.x=a.x+b.x; u.y=a.y+b.y; return u; } public static double Angle(Vector A,Vector B) { double r=new double(); r=A*B/(A.Length*B.Length); r=Math.Acos(r); r=r/6.28*360; return r; } } class kadai53 { static void Main() { Vector A; double x, y; Console.Write("ベクトルAの成分を入力してください\n"); Console.Write("x成分は ? "); x = double.Parse(Console.ReadLine()); Console.Write("y成分は ? "); y = double.Parse(Console.ReadLine()); A = x * Vector.I + y * Vector.J; //静的プロパティと,一つ目の*演算子,+演算子の呼び出し Console.Write("A = "); A.Write(); Vector B = new Vector(0, 1); Console.Write("\nB = "); B.Write(); Console.WriteLine("\nベクトルAとベクトルBの内積の値は{0}です", A*B);//二つ目の*演算子の呼び出し Console.Write("ベクトルAとベクトルBがなす角度は{0}度です\n", Vector.Angle(A,B)); } }

  • C++で2点間の内積と距離を求めるプログラム

    C++で2点間の内積と距離を求めるプログラムを作ったのですが、コンパイルの時点でエラーが6つも出てしまいます>< 何がいけないのでしょうか? #include <iostream> #include <cmath> using namespace std; int main(void){ int x, y, z, w, l, n; cin >> x >> y >> z >> w; n=x*w+y*z;   l=sqrt((x-z)(x-z)+(y-w)(y-w)); cout << "距離は" << l << "で、内積は" << n; return 0; }

  • プログラミングC言語

    プログラミングC言語の問題で、 「ベクトルの内積を用いて、2つのベクトルの成す角度を求めるプログラムを作成せよ。」 という問題をやっていて、以下の画像のような答えになるのですが、上手くいきません。 どのようにすればいいのでしょうか? 作ったソースコード #include<stdio.h> #include<math.h> double naiseki(int *vecterA, int *vecterB) { double rad, deg; rad = acos( (vecterA[0]*vecterB[0] + vecterA[1]*vecterB[1]) / ( sqrt((double)(vecterA[0]*vecterA[0] + vecterA[1]*vecterA[1])) * sqrt((double)(vecterB[0]*vecterB[0] + vecterB[1]*vecterB[1])) ) ); deg = rad/3.141592*180; return deg; } int main(void) { double va[2]; double vb[2]; printf("v1_x:"); scanf("%lf", &va[0]); printf("v1_y:"); scanf("%lf", &va[1]); printf("v2_x:"); scanf("%lf", &vb[0]); printf("v2_y:"); scanf("%lf", &vb[1]); puts("内積から求めたベクトルの角度は"); printf("%f",naiseki(va,vb)); puts("です。"); return(0); } 最後のnaiseki(va,vb)のところで互換性がありませんと でてしまいます。 double naiseki のソースコードはこのままでプログラムが 動くようにしてほしいです。 よろしくお願いします!

  • C言語でsqrt(a^2+b^2)のテーブル引き

    プログラムに悩んでいるものです. とある画像処理のプログラムを組んでいるのですが,処理が遅くテーブル引きを組んでいます. 三角関数などはすんなりできたのですが,質題にもある通りsqrt(a^2+b^2)が実現できず,この場を借りて質問させていただきました. 以下にプログラムの一部を示します. ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー void filter(unsigned char* d, short *dx, short *dy, int w, int h) { ///// テーブル生成 ///// static int c_size = 0; // static 次の呼び出しでも値保持 static double *c_sqrt = NULL; // c_size = 255;              // u,v:0~255 c_sqrt = (double *)malloc(sizeof(double)*c_size*c_size); // 領域確保 for(int i=0; i<c_size; ++i){     // 有りえるすべての値を生成 for(int j=0; j<c_size; ++i){ c_sqrt[i*j] = sqrt( (double)(i*i + j*j) ); } } ///// d = sqrt(dx^2 + dy^2) ///// for(int y = 1; y < h-1; ++y){ for(int x = 1; x < w-1; ++x){ double u = (double)dx[y*w+x]; double v = (double)dy[y*w+x]; int val = (int)c_sqrt[ (int)(u*v) ] /4; if (val>255) val=255; d[y*w+x] = val; } } } ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー 見てご察し頂ける(?)と思いますが,この関数は何回も呼び出すので,上のほうででテーブル引きしようとしてます. ただ明らかなプログラム経験不足のためかうまくいってません. 個人的にはc_sqrtを別途関数c_sqrt(u,v)にしたほうがよいのかと思ってます. どういうプログラム記述をすれば,このテーブル引きが実現できるでしょうか? ご回答,お力添え,よろしくお願い致します.

  • javaのプログラムについて

    質問させていただきます // 数値積分 class Integral1 { public static void main(String[] args) { final int n = 100; // 区間数 final double a = 0; // 始点 final double b = 1; // 終点 final double h = (b-a) / n; // 区間の幅 double x, y; double S = 0; // Sを0で初期化 int i; // 区間 for(i = 1; i <= n ; i++) { // 区間1からnまで x = a + h * i; // 区分積分 y = Math.sqrt(1-x*x); // yを計算 S += y * h; } System.out.println("S = " + S + " 4S = " + 4*S); } } これは区分積分法で計算する数値積分のプログラムなのですが、 これをシンプソン法に改造したプログラムに直していただきたいのですが・・・ どなたかお願いします

  • javaについて質問です。

    import javax.swing.*; import java.awt.*; import java.awt.event.*; class MoveField extends JPanel {       private int xc;       private int yc;       int d = 50;       Color c=Color.red;       int pause =1000;       boolean cont=true; thread th;       MoveField(){}       MoveField(int d){this.d=d;}       MoveField(int d,Color c){this(d);this.c=c;}       boolean isHit(int x,int y){             int w=(x-xc)*(x-xy)+(y-yc)*(y-yc);             if(w<=d*d){return true;}       return false; }       void start(){             cont=true; th= new Thread(this);             th.start(); } void stop() {cont=false:} public void run(){ when(cont){ int x=0 int y=0 if(Math.random()<0.5){x=(int)(Math.random()*(getWidth()-d));} if(Math.random()<0.5){y=(int)(Math.random()*(getHeight()=d));} xc=x; xy=y; repaint(); try{Thread.sleep(pause);}catch(Exception e){} } } @Override public void paint(Graphics g){ g.setColor(Color.white); g.fillRect(0,0,getWidth(),getHeight()); gsetColor(c); g.fillOval(xc,yc,d,d); } } class MoveHit extends JFrame impkements ActionListener{ MoveField field; JTextField txt; nt pt=0; MoveHit(){ setDefaultCloseOperation(EXIT_ON_CLOSE); field=new MoveField(); add(field); JPanel p=new JPanel(); p.add(new Jlabel("Point")); txt=new JTextField(20); p.add(txt); add(p.BorderLayout.NORTH); p=new JPanel() JButton b; b=new JBotton("Start");b.addActionListener(this);p.add(b); b=new JBotton("Stop");b.addActionListener(this);p.add(b); add(p, BorderLayout.SOUTH); }   @Override public void actionPerformed(ActionEvent ev){ string cmd=ev.getActionCommand(); if(cmd.epuals("Stop")){ pt=0; txt.setText(""); Field.start(); } elese if(cmd.equals("Stop"))field.stop(); } public void mouseClicked(MouseEvent ev){ int x=ev.getX(); int y=ev.getY(); if(field.isHit(x,y)){ pt++; Toolkit.getDefaultToolkit().beep(); txt.setText(""+pt); } } public void mousePressed(MouseEvent ev){} public void mouseReleased(Mousevent ev){} public void mouseEntered(MouseEvent ev){} public void mouseExited(MouseEvent ev){} public static void main(String [] args){ MoveHit tx=new MOveHit(); tx.setSize(600,500); tx.setVisible(true); } } このプログラムがうまく作動しません。 エラーは 1.MoveHit.java:30: ';' がありません。 void stop() {cont = false:} 2.MoveHit.java:33: ';' がありません。 when(cont){ 3.MoveHit.java:46: 注釈は -source 1.4 でサポートされていません (注釈を使用可能にするには、-source 1.5 を試してください) @Override 4.MoveHit.java:55: '{' がありません。 class MoveHit extends JFrame impkements ActionListener { です。 また、MoveHitクラスがマウスイベントの処理に対応していないらしいので改善したいです。 ほかに修正点があれば教えてほしいです。協力お願いします。

  • 以外のクラスに分数同士の比較を行うメソッドを追加してください

    以外のクラスに分数同士の比較を行うメソッドを追加してください 自身(this)の分数よりxが小さい場合は1を値とし、xのほうが大きい場合は-1、等しい場合は0を値とする class Bunsu { final static Bunsu ZERO=new Bunsu(0,1); final static Bunsu ONE=new Bunsu(1,1); int bunshi; int bunbo; Bunsu() {} Bunsu (int s,int b) {set(s,b);} void set(int x,int y) { bunshi=x; bunbo=y; int w=gcd(bunshi,bunbo); bunshi=bunshi/w; bunbo=bunbo/w; } static int gcd(int x,int y) { while (y!=0) { int r=x % y; x=y; y=r; } return x; } double jissuChi() { return (double)bunshi/bunbo; } Bunsu seki(Bunsu x) { int s=bunshi*x.bunshi; int b=bunbo*x.bunbo; return new Bunsu(s,b); } Bunsu wa(Bunsu x) { int s=bunshi*x.bunbo+x.bunshi*bunbo; int b=bunbo*x.bunbo; return new Bunsu(s,b); } public String toString() { return bunshi+"/"+bunbo; }

  • 以下のクラスで"2/3"のように「/」の前が分子、後が分母となるような

    以下のクラスで"2/3"のように「/」の前が分子、後が分母となるような分数で初期化するにはどうすればいいですか? class Bunsu { final static Bunsu ZERO=new Bunsu(0,1); final static Bunsu ONE=new Bunsu(1,1); int bunshi; int bunbo; Bunsu() {} Bunsu (int s,int b) {set(s,b);} void set(int x,int y) { bunshi=x; bunbo=y; int w=gcd(bunshi,bunbo); bunshi=bunshi/w; bunbo=bunbo/w; } static int gcd(int x,int y) { while (y!=0) { int r=x % y; x=y; y=r; } return x; } double jissuChi() { return (double)bunshi/bunbo; } Bunsu seki(Bunsu x) { int s=bunshi*x.bunshi; int b=bunbo*x.bunbo; return new Bunsu(s,b); } Bunsu wa(Bunsu x) { int s=bunshi*x.bunbo+x.bunshi*bunbo; int b=bunbo*x.bunbo; return new Bunsu(s,b); } public String toString() { return bunshi+"/"+bunbo; }

専門家に質問してみよう