The most obvious error you are making is to put a list of parameters in a class. Class is not method. Java does not have the Primary constructor (exists in Kotlin and there is proposal for this in C#) which is more or less what you’re trying to do, although I doubt it was the intention.
In fact you will have to create a constructor to do what you want. It seems that you have merged the syntax of the class with that of the constructor. Then you have to create the instance variables to be used by these methods.
But there are other errors. The type of text in Java is String
and cannot be written in lower case. To know the size of the text you have to call the method length()
. It is not a property, as it is in other languages.
samebook
(bad name) should probably be static
.
It is not common in Java to use _
in variable names. Methods are often lowercase. Some of these methods are called getters and stters. Do not use the class name in the method name unless you have a very good reason for this, this is redundant. Example of what would be better: setTitle()
, getTitle()
, getText()
and length()
. And the class name is usually used in upper case. No mistake, of course.
There are some other things that in real code may not be quite what you should do, but it seems to me an exercise and this does not influence. I don’t even know what the requirements are.
Then it would look like this:
class Book {
private String _title;
private String _text;
Book(String title, String text) {
_title = title;
_text = text;
}
void NewTitle(Book livro, String newtitle) {
livro._title = newtitle;
}
String BookTitle(Book livro) {
return livro._title;
}
String BookText(Book livro) {
return livro._text;
}
int BookLength(Book livro) {
return (livro._title.length() + livro._text.length());
}
boolean samebook(Book livro1, Book livro2){
return (livro1._title == livro2._title && livro1._text == livro2._text);
}
}
I put in the Github for future reference.
Missed of stackoverflow is?
– ppalacios
I don’t understand what could be Wrong to Give an { expected error after the public class book.
– André Soares
Suggestion, if you speak Portuguese, write in English. Otherwise, you should Ask it in stackoverflow.com.
– ppalacios
Oh, I didn’t notice! Could you help me then? Thank you!
– André Soares
Good evening André and what is the error that appears in the Console?
– Guilherme Nascimento
Error: Expected { where you have the ( aseguir a class book.
– André Soares
André better translate the question and put the whole error described in the java console along with the edit, because if not the question will be closed and after you edit, it can take a while to reopen, note that closing a question is nothing big, is only to avoid answers while the question does not follow the minimum quality.
– Guilherme Nascimento
You have placed parentheses after the class name (as if you were writing a constructor). Remove the parentheses. It will not compile yet, as you are trying to assign the fields from the "constructor" parameters. Stop assigning them (only state, or explicitly assign to
null
). Then compile. Finally, you can create a constructor that assigns these fields from the parameters. P.S. If you translate the question, I put it as an answer (I don’t do it now but take it downvote.. :P)– mgibsonbr
P.S. This is Java itself, or is it C#? If it’s Java, it also has the string problem with lowercase letter (is
String
). And in any case, you do not need to pass the instance itself as parameter (as in Python, for example), just omit it and usethis
to refer to the current object (or, in the case of fields, only refer to it by name - as opposed to Javascript bythis
).– mgibsonbr
If it is
Java
Voce must use String and not String.– David Schrammel
I do not understand why these negatives. They do not know how to do. This does not mean that the question is bad. If it was because it was in English, even worse.
– Maniero
@I think the negative votes were before the issue, or not? However it may have been that the AP did not translate the question and who ended up translating was another colleague, maybe someone thinks that the AP did not show the minimum commitment. However I will leave my +1 to support :)
– Guilherme Nascimento
Were, but not justified. This failure to understand the site only. If they did this, they punished the content wanting to punish the person, which is not our goal.
– Maniero
@Andrésoares Take a look at [tour]. You can accept an answer if it solved your problem. You can vote for every post on the site as well. Did any help you more? You need something to be improved?
– Maniero