Java/help!

Advertisement


Question
QUESTION: in a piece of my code I have written the following method:
public String toStirng() {
     String s = "";
     for (int i=0; i<this.coef.length; i++){
        if(this.coef[i]<0){
         s += "-";
        }
        else if (this.coef[i]>0 && i!=0){
         s += "+";
        }
        else{
         s = "";
        }
        if (this.coef[i]==0)
         break;
        else if (this.coef[i]==1 && i!=(this.coef.length)-1)
         s += "x^"+ (degree()-i);
        else if (this.coef[i]==1 && i==(this.coef.length)-1)
         s += "1";
        else
         s += this.coef[i] + "x^" + (degree()-i);
     }
     return s;
  }

but everytime i compile it displays the memory reference of the object rather than the object's conten that i need,,I don't know why this happpens!
Can you help me please ?
Thank you.

ANSWER: I don't see any problems with the code you have for toString(). As long as toString() isn't simply return super.toString() you shouldn't get any memory stuff displayed. It is possible that the code is not calling toString() on the class you define it in.

When are you expecting the toString() method to be used? If it's:
System.out.println(obj);
where obj is an instance of this class, then this should work.

If you are trying to print an array of objects then it will just show you the default toString() implementation, it will not call toString() on every reference in the array.

I cannot fully answer your question unless I know the place where you expect to see the contents instead of the default implementation.

---------- FOLLOW-UP ----------

QUESTION: public class Polynomial {
  
  int [] coef;
  
  public Polynomial(){
     coef = null;
  }
  public Polynomial (int [] coeff){
     coef = new int [coeff.length];
     for(int i=0; i<coeff.length; i++)
        coef[i] = coeff[i];
  }
  public int degree (){
     int i = 0;
     for(i=0; i<coef.length; i++){
        if(coef[i] != 0)
         break;
     }
     return (coef.length)-i-1;
  }
  public Polynomial add (Polynomial a){
     Polynomial r = new Polynomial (new int [a.coef.length]);
     for(int i=0; i<a.coef.length; i++)
        r.coef[i] = this.coef[i] + a.coef[i];
     return r;
  }
  public static Polynomial add (Polynomial a, Polynomial b){
     Polynomial r = new Polynomial (new int [a.coef.length]);
     for (int i=0; i<a.coef.length; i++)
        r.coef[i] = a.coef[i] + b.coef[i];
     return r;
  }
  public String toStirng() {
     String s = "";
     for (int i=0; i<this.coef.length; i++){
        if(this.coef[i]<0){
         s += "-";
        }
        else if (this.coef[i]>0 && i!=0){
         s += "+";
        }
        else{
         s = "";
        }
        if (this.coef[i]==0)
         continue;
        else if (this.coef[i]==1 && i!=(this.coef.length)-1)
         s += "x^"+ (degree()-i);
        else if (this.coef[i]==1 && i==(this.coef.length)-1)
         s += "1";
        else
         s += this.coef[i] + "x^" + (degree()-i);
     }
     return s;
  }
  
  public static void main(String[] args) {
  Polynomial a = new Polynomial (new int [] {4,3,2,2});
  Polynomial b = new Polynomial (new int [] {-4,5,0,-3});
  System.out.println("The degree of polynomial a = " + a.degree());
  System.out.println(a.add(b));
  }
}

here's the full code. I still can't figure out what is the problem with it!!

Answer
Oh, right, the answer is simple. You didn't spell toString() correctly in your method deceleration. Sorry for missing that earlier.

To avoid issues like this in the future, always add @Override directly above any method that is overriding another one or an interface method. This way the compiler will not compile your code if the name is not correct.

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.