dimanche 3 mai 2015

Trying to reference "this" referencing to a current string object. Not sure if I'm doing this right

quiz problem, final exam tomorrow and this problem makes NO sense to me, someone please please please show me the code segment that would fit the criteria. everything I try has failed. my biggest problem is I have no idea how to reference "this". So I have no idea if I am doing it right or not my teacher will not give me a yes or no answer, I am going to post my attempt at the code below and hopefully someone will correct my mistakes. I would be forever in your debt!

  1. The question >> Write a BigInt CLASS function int countDigits() that returns the number of significant digits in the BigInt object (i.e "this"). Assume we are using the string representation for the BigInt (leading zeros not removed) significant digits start with the left-most non-zero if BigInt has a value of 0 the number significant digits is 1
public int numDigits(int n)
{
  int i = 0;
  while(this.number.charAt(i)=='0')//this.number will reference a string object
    i++;
  String str = this.number.substring(i,number.length();)//this.number will reference the string object
  System.out.println("leading zeros removed : "+str);
  System.out.println(" number of significant digits  = "+str.length());
}

Aucun commentaire:

Enregistrer un commentaire