gaussian class in java -
i making class simulates gaussian integer. using constructor in addition method add 2 two parts of gint , return new gint sum. reason when trying implement method, java says gint required when initialize new gint , found void. why be? have included class below , indicated line causes error.
public class gint { private int real; private int imag; public void gint(int r) { imag=0; real=r; } public void gint(int r, int i) { real=r; imag=i; } gint add(gint rhs) { gint added; int nreal=this.real+rhs.real; int nimag=this.imag+rhs.real; added= gint(nreal,nimag); //--> says requires gint , found void return added; } }
use implementation , happy:
public class gint { private int real; private int imag; public gint(int r) { imag=0; real=r; } public gint(int r, int i) { real = r; imag = i; } gint add(gint rhs) { gint added; int nreal = this.real + rhs.real; int nimag = this.imag + rhs.real; added = new gint(nreal, nimag); return added; } }
comments:
- don't use class names begin lowercase letter (e.g.
gint
instead ofgint
) - constructors in java not have return type in java, removed
void
type in op - you need
new
operator create newgint
object in java
Comments
Post a Comment