Results 1 to 13 of 13
Thread: What the error means?
- 06-22-2012, 09:03 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 6
- Rep Power
- 0
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:
and I get the following error hint: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); } * */ }
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?
TIALast edited by MaXER; 06-22-2012 at 10:07 AM.
- 06-22-2012, 10:32 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 06-22-2012, 03:16 PM #3
Member
- Join Date
- Jun 2012
- Posts
- 4
- Rep Power
- 0
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
- 06-22-2012, 03:56 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 06-29-2012, 08:49 AM #5
Member
- Join Date
- Jun 2012
- Posts
- 6
- Rep Power
- 0
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.
- 06-29-2012, 09:28 AM #6
Member
- Join Date
- Jun 2012
- Posts
- 6
- Rep Power
- 0
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);
*/
}
}
- 06-29-2012, 09:51 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 06-29-2012, 12:51 PM #8
Member
- Join Date
- Jun 2012
- Posts
- 6
- Rep Power
- 0
Re: What the error means?
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?
ThanksLast edited by MaXER; 06-29-2012 at 01:20 PM.
- 06-29-2012, 01:43 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 06-29-2012, 01:51 PM #10
Member
- Join Date
- Jun 2012
- Posts
- 6
- Rep Power
- 0
Re: What the error means?
Thanks Tolls for the confirmation and point me to the right direction.
- 06-30-2012, 11:47 AM #11
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
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-17-2012, 12:20 PM #12
Member
- Join Date
- Jun 2012
- Posts
- 6
- Rep Power
- 0
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); }
- 09-17-2012, 02:09 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Not sure what error means? javax.xml.bind.JAVAXBException
By SnakeDoc in forum New To JavaReplies: 1Last Post: 04-17-2012, 01:03 AM -
what does the gray (x) means?
By pandaman0212 in forum EclipseReplies: 3Last Post: 06-28-2010, 03:24 PM -
cant understand what this means
By hasysf in forum New To JavaReplies: 19Last Post: 09-05-2009, 04:27 PM -
what does this error means?
By tiiim83 in forum New To JavaReplies: 2Last Post: 01-06-2009, 03:17 PM -
Does anyone know what these error means?
By minifish in forum New To JavaReplies: 2Last Post: 11-04-2008, 04:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks