Results 1 to 13 of 13
  1. #1
    MaXER is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default What the error means?

    NetBeans IDE 7.1.2 and I'm a newbie in java, trying Oracle's tutorials and excersises (Exercise 1 Answers to Questions and Exercises: Interfaces (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)), so:

    the backwardSeq.java code is:
    Java Code:
    public class backwardSeq implements CharSequence {
            
        public CharSequence backwardSeq(CharSequence charSeq){
            
            int l = charSeq.length();
            
            if (l < 2)
                return charSeq;
            
            CharSequence charBackSeq = charSeq.subSequence(1, l-1);
            return (CharSequence) (charSeq.charAt(l-1) +
                    backwardSeq(charBackSeq).toString());
            //for (int i = 0; l-1; i++){
                
        }
     /*   public static void main(String[] args) {
            CharSequence charSeq = new CharSequence("Write a small main method to test your class; make sure to call all four methods.");
            String strBack = (String) new backwardSeq();
            System.out.println(strBack);
      
    
    
        }
        * 
        */
    }
    and I get the following error hint:
    backwardSeq is not abstract and does not override abstract method subSequence(int,int)

    Can anyone explain what the error means and how can I fix it?
    TIA
    Last edited by MaXER; 06-22-2012 at 10:07 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,093
    Rep Power
    20

    Default Re: What the error means?

    You are implementing the interface CharSequence, but you are not implementing one or more of the methods declared in that interface. The compiler is then suggesting that this should be an abstract class, since there are unimplemented methods.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Jeeva7 is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: What the error means?

    Hi

    Interface -> has only declarations of the method no implementation of codes
    Abstract -> partially implemented/ completed class
    Class -> fully implemented/completed class

    If you want to implement interface and create a class you have to complete all the methods implementation.
    If you want to implement interface and create a abstract class then you don't need to complete all the methods implementation.

    It gives the error because you didn't completely implemented all the methods but you declared as class
    now you can declare as abstract class and later you can extend this abstract class
    or else you have to complete all the methods declared in the charsequence class

    Java API:
    CharSequence (Java 2 Platform SE v1.4.2)

    refer the above and complete the remaining method definitions

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,093
    Rep Power
    20

    Default Re: What the error means?

    If you're going to link to the API at least make it one that isn't from a no longer supported version.
    CharSequence.
    Google seems to default 1.4.2.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    MaXER is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: What the error means?

    Tolls:
    My ignorance makes me agree and still be confused...

    Jeeva7:
    I think I understand better now!

    Thank you both for your time and effort.

  6. #6
    MaXER is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: What the error means?

    A step further to the same excercise, the following is a working excerpt from Oracle's answer (http://docs.oracle.com/javase/tutori...uenceDemo.java).

    What I don't understand is how line
    System.out.println(s);
    calls method toString()?

    TIA

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package helloworldapp;

    /**
    *
    * @author supervisor
    */
    // CharSequenceDemo presents a String value -- backwards.
    public class CharSequenceDemo implements CharSequence {
    private String s;

    public CharSequenceDemo(String s) {
    //It would be much more efficient to just reverse the string
    //in the constructor. But a lot less fun!
    this.s = s;
    }

    //If the string is backwards, the end is the beginning!
    private int fromEnd(int i) {
    return s.length() - 1 - i;
    }

    public char charAt(int i) {
    if ((i < 0) || (i >= s.length())) {
    throw new StringIndexOutOfBoundsException(i);
    }
    return s.charAt(fromEnd(i));
    }

    public int length() {
    return s.length();
    }

    public CharSequence subSequence(int start, int end) {
    if (start < 0) {
    throw new StringIndexOutOfBoundsException(start);
    }
    if (end > s.length()) {
    throw new StringIndexOutOfBoundsException(end);
    }
    if (start > end) {
    throw new StringIndexOutOfBoundsException(start - end);
    }
    StringBuilder sub =
    new StringBuilder(s.subSequence(fromEnd(end), fromEnd(start)));
    return sub.reverse();
    }

    public String toString() {
    StringBuilder s = new StringBuilder(this.s);
    return s.reverse().toString();
    }
    /*
    //Random int from 0 to max.
    private static int random(int max) {
    return (int) Math.round(Math.random() * max + 0.5);
    }
    */
    public static void main(String[] args) {
    CharSequenceDemo s =
    new CharSequenceDemo("Write a class that implements the CharSequence interface found in the java.lang package.");

    System.out.println(s);
    //exercise charAt() and length()
    /*for (int i = 0; i < s.length(); i++) {
    System.out.println(s.charAt(i));
    }
    */
    //exercise subSequence() and length();
    /* int start = random(s.length() - 1);
    int end = random(s.length() - 1 - start) + start;
    System.out.println(s.subSequence(start, end));

    //exercise toString();
    System.out.println(s);
    */

    }
    }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,093
    Rep Power
    20

    Default Re: What the error means?

    Because that's what the code in println() does for Objects.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    MaXER is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: What the error means?

    Quote Originally Posted by Tolls View Post
    Because that's what the code in println() does for Objects.
    I've tried to find your explanation here: Java 2 Platform SE v1.3.1: Class PrintStream .

    So what you say comes out from method String.valueOf(Object) which in turn calls method Object.toString()?
    And which in turn It is recommended that all subclasses override this method. ?
    And the last reference explaining the existence of toString() method in the above code?

    Thanks
    Last edited by MaXER; 06-29-2012 at 01:20 PM.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,093
    Rep Power
    20

    Default Re: What the error means?

    That's exactly what the API says.
    I would recommend referring to a newer one than 1.3, as that's been dead for years. 1.6 or 1.7 is what you want.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    MaXER is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: What the error means?

    Thanks Tolls for the confirmation and point me to the right direction.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: What the error means?

    Please go through the Forum Rules -- particularly the third paragraph.

    Also go through Guide For New Members and BB Code List - Java Programming Forum

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    MaXER is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: What the error means?

    Ok, I come back to the same exercise as I have two missing points.
    Thanks in advance for any comments.

    1. After execution of line 54, what is the value of var charSequenceDemo s? NetBean IDE 7.1.2 shows it reversed in debugging mode, but could it be cause of the use of method toString() to show it? So, is the value of s:
    a. the original string, but when we come to "see" it, we are forced to reverse it or
    b. the string is reversed, which I don't understand why.

    2. Line 38, if end = s.length() then fromEnd(end) = -1 and method subSequence is not exactly as the interface CharSequence describes. Of course main is protected in line 63 (with the -1), but shouldn't be better, to ommit -1 in lines 13, 63?
    Java Code:
    // CharSequenceDemo presents a String value -- backwards.
    public class CharSequenceDemo implements CharSequence {
        private String s;
    
        public CharSequenceDemo(String s) {
            //It would be much more efficient to just reverse the string
            //in the constructor. But a lot less fun!
            this.s = s;
        }
    
        //If the string is backwards, the end is the beginning!
        private int fromEnd(int i) {
            return s.length() - 1 - i;
        }
    
        public char charAt(int i) {
            if ((i < 0) || (i >= s.length())) {
                throw new StringIndexOutOfBoundsException(i);
            }
            return s.charAt(fromEnd(i));
        }
    
        public int length() {
            return s.length();
        }
    
        public CharSequence subSequence(int start, int end) {
            if (start < 0) {
                throw new StringIndexOutOfBoundsException(start);
            }
            if (end > s.length()) {
                throw new StringIndexOutOfBoundsException(end);
            }
            if (start > end) {
                throw new StringIndexOutOfBoundsException(start - end);
            }
            StringBuilder sub = 
                new StringBuilder(s.subSequence(fromEnd(end), fromEnd(start)));
            return sub.reverse();
        }
    
        public String toString() {
            StringBuilder s = new StringBuilder(this.s);
            return s.reverse().toString();
        }
    
        //Random int from 0 to max.
        private static int random(int max) {
            return (int) Math.round(Math.random() * max + 0.5);
        }
    
        public static void main(String[] args) {
            CharSequenceDemo s =
                new CharSequenceDemo("Write a class that implements the CharSequence interface found in the java.lang package.");
    
            //exercise charAt() and length()
            for (int i = 0; i < s.length(); i++) {
                System.out.println(s.charAt(i));
            }
    
            //exercise subSequence() and length();
            int start = random(s.length() - 1);
            int end = random(s.length() - 1 - start) + start;
            System.out.println(s.subSequence(start, end));
    
            //exercise toString();
            System.out.println(s);
    
        }

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,093
    Rep Power
    20

    Default Re: What the error means?

    Your IDE should allow you to "open" the object and see what the value of the String is as stored in the CharSequenceDemo object.
    That should answer your first question.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 04-17-2012, 01:03 AM
  2. what does the gray (x) means?
    By pandaman0212 in forum Eclipse
    Replies: 3
    Last Post: 06-28-2010, 03:24 PM
  3. cant understand what this means
    By hasysf in forum New To Java
    Replies: 19
    Last Post: 09-05-2009, 04:27 PM
  4. what does this error means?
    By tiiim83 in forum New To Java
    Replies: 2
    Last Post: 01-06-2009, 03:17 PM
  5. Does anyone know what these error means?
    By minifish in forum New To Java
    Replies: 2
    Last Post: 11-04-2008, 04:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •