0

Problem: My overloading constructors are undefined, even though I'm doing the right thing (I think so?).

The source code:

class Book { 
    String section;
    float subject2;
    char section2, author;
    int subject, author2, year;

Book(String section, int subject, char author, int author2, int year) { 
    this.section = section;
    this.subject = subject;
    this.author = author;
    this.author2 = author2;
    this.year = year;
}
Book(char section2, float subject2, char author, int author2, int year) {
    this.section2 = section2;
    this.subject2 = subject2;
    this.author = author;
    this.author2 = author2;
    this.year = year;
}
void displayData() {
    System.out.println(section + subject + " ." + author + author2 + " " + year);
}
void displayData2() {
    System.out.println(section2 + subject2 + " ." + author + author2 + " " + year);
}}
public class TestBook {
  public static void main(String[] args) {
    Book book1 = new Book("LB", 2395, "C", 65, 1991);
    Book book2 = new Book("E", 185.86, "P", 277, 2010);
    book1.displayData();
    book2.displayData2();
    s.close();  
}}

Apologies for the length if it bothers. Thanks for helping in advanced!

4
  • 4
    "C" is a String , 'C' is a char . Commented Oct 29, 2021 at 10:10
  • 1
    Oh, I didn't noticed it but thanks, still. "Small help makes big difference." Commented Oct 29, 2021 at 10:21
  • 1
    I assume this is really just "throw away exercise code", but still: use meaningful names in your code, especially for the fields of your classes. You see, section vs section2, what is the sense of that? In other words: when writing code, also practice that "how to come up with good names" part. Commented Oct 29, 2021 at 12:21
  • Not trying to be rude but, I'm still a learning student, sir. I haven't been on the part where I also have to worry my naming conventions. However, I am somewhat interested what you mean by "meaningful". What sort of names can come up from someone who has already an expertise in this field? Commented Oct 29, 2021 at 12:45

1 Answer 1

3

When you call :

Book book2 = new Book("E", 185.86, "P", 277, 2010);

Change "E" to 'E' if you want to call the second constructor with char, otherwise "E" is the String object. Same thing for the field author (change "P" to 'P')

By the way, instead of creating displayData() method, you can override the toString() method :

@Override
public String toString() {
    return "your string with information";
}

so you can call this method with :

System.out.println(book.toString());
System.out.println(book); // The same thing ! It will call the toString() method
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.