Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Task :Make a class ComplexNumber in java.

Hey. I have a couple of problems with my task based on objects in java. I made this code down below. public class ComplexNumber{ double real; double imag;

public ComplexNumber(real,imag){
    this.real = real;
    this.imag = imag;
}

public void setReal(double real){
    this.real = real;
}
public double getReal(){
    return real;
}
public void setImag(double imag){
    this.imag = imag;
}
public double getImag(){
    return imag;
}

public ComplexNumber add (ComplexNumber i){
    double x = real + i.imag;
    double y = real + i.imag;
    ComplexNumber n1 = new ComplexNumber (x,y);
    return n1;
}
public ComplexNumber take (ComplexNumber i){
    double x = real + i.imag;
    double y = real + i.imag;
    ComplexNumber n1 = new ComplexNumber (x,y);
    return n1;
}
     public String toString(){
           return ;

}

public static void main ( String [] args){ ComplexNumber i = new ComplexNumber(2.0,3.0); }

My questions are : -Is this way correct. -should i make double real and double imag private? -Can you tell me if i did any errors on the constructor and get/set methods. -and are methods public ComplexNumber add , public ComplexNumber take correct, i feel like i should have made them with void but i am not totally sure.

  • And i think that i don't completely understand methods. Don't we have functions and void methods. Functions return value (public double add(double value1){return value1} )? And methods are there to do some task in the program which we can later call in the program ? Did i got that wrong ?

Sorry for possible spelling errors and thanks in advance.

I'll try answering your questions. I didn't see the class and data field declarations at the top--maybe they were cut off. If you're using getters and setters, and you want to uphold the principle of encapsulation (i.e. hide your data from users), then you should declare double real and double imag as private.

 private double real;
 private double imag;

 public ComplexNumber() {
       this(0, 0);
 }

 public ComplexNumber(double real, double imag) {
      this.real = real;
      this.imag = imag;

}

I used constructor overloading in this instance because it's always best to define your own default constructor in case the class is called without any arguments, like ComplexNumber();. This would be fine if you went ahead and initialized the private data fields to begin with, like private double real = 0, etc.

Otherwise, your getters and setters are fine. The only real reason to use them is if you're implementing encapsulation by declaring your class data fields as private and using methods to access/alter them. Your other two methods, add() and take(), need a return type because you're returning a value (n1). You cannot use void in this case because there is a return type; if there was no return type, you would use void. Either way, you need to specify whether there is a return type or not (void) in the method signature.

For your last question about functions and methods, don't get too hung up on the semantics of each term because they seem to differ in each language (or so it seems in my experience). In Java, there are only methods, which are functions that belong to a class, and all code belongs in a class (all are objects). In a language like JavaScript, you're using mostly functions unless you create an object and use prototypes, then it's a method. It's really weird. But, in short, there are only methods in Java, whether they return data or only perform actions on some data. I hope that helps.

1 Answer

  1. You can leave members of your class package-protected as they are right now. Usually people make them private however and getters/setters public.
  2. Constructor looks OK. nothing special.
  3. [EDIT]. Found nice Complex number implementation: take a look here. I think people do exactly what are you trying to achieve: http://introcs.cs.princeton.edu/java/97data/Complex.java.html

Also toString() has to have @Override annotation, this is important. Because you override method from super class.

One more advice: if you want to implement methods like addTwoComplexNumbers or somewhat like that. These methods are usually implemented in a separate class.

Also check Stack, here is nice discussion about complex numbers:

http://stackoverflow.com/questions/10098958/java-complex-numbers-3-classes

Method take needs to substract(it should have been double x = real - i.imag;, i messed it up ) and add is SUM. I will be careful about the @Override. I think that the teacher expects us that we do this methods in the same class. Thank you.