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.
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
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.
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.
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);
*/
}
}
Re: What the error means?
Because that's what the code in println() does for Objects.
Re: What the error means?
Quote:
Originally Posted by
Tolls
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
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.
Re: What the error means?
Thanks Tolls for the confirmation and point me to the right direction.
Re: What the error means?
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? 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);
}
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.