0

I have this first method that is supposed to return the title of a book that refers back to the instance variable. This works fine but it makes me select the inserted title instead of using the user inputted title immediately, is there a way for it to return the title without me going through the extra step of selecting the title in a drop down list?

I have a second method that is supposed to return the remaining chapters by finding the different between (BookChapters) and (BookMark), but when I run it it doesn't return the difference and like the first method it makes me choose the remaining chapters in a drop down list.

This is for an introductory programming course, I've tried looking up java documentation on returning mathematical statements but I haven't achieved much.

private String BookTitle;
private int BookChapters;
private int BookMark;
public int RemainingChapters;

// instance variables //

public Textbook()
{
    // initialise instance variables
    BookTitle = "Lord of the Flies";
    BookChapters = 3;
    BookMark = 0;  
}

// my constructor //

public String getTitle(String BookTitle)
{
    return BookTitle; 
}

// my first method to return the title of the book //

public int getRemainingChapters (int RemainingChapters)
{
    return RemainingChapters = BookChapters - BookMark;
}

// my second method to return the remaining chapters //

https://i.stack.imgur.com/hv7oh.jpg

this image should help me explain my issue regarding the first method

Marc Xu
  • 55
  • 1
  • 8
  • 3
    Getter methods should not take arguments. Your `getTitle` method returns its own argument, not the object's field. Declare it as `public String getTitle() { ... }` with no arguments. – kaya3 Nov 07 '19 at 14:53
  • Also, you don't `return RemainingChapters = BookChapters - BookMark`. Either do the assignment, or return a value, don't mix both in a single operation. It might work (I assume it doesn't), but it's confusing at the very least – AJPerez Nov 07 '19 at 14:55
  • Does this answer your question? [How do getters and setters work?](https://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work) – QBrute Nov 07 '19 at 15:00
  • @kaya3 how should my return statement be now that I want it to return what was input by the user? – Marc Xu Nov 07 '19 at 15:13
  • Your return statement should not change; it will correctly return the field (which was set in the constructor) so long as there isn't another variable with the same name shadowing it. – kaya3 Nov 07 '19 at 15:15
  • If my return statement doesnt change it just returns null and not what the user inputs Edit: i mean, I want the BookTitle field to be variable and return different statements depending on what the user inserts so i removed it in the constructor – Marc Xu Nov 07 '19 at 15:19

1 Answers1

0
public String getTitle() { 
    return BookTitle;  }

Getters should not take parameters but rather return the objects (class level) variables.

Maybe you intended to write a setter which would be achieved like this:

public String getTitle(String BookTitle) {
    this.BookTitle = BookTitle;  }

this.BookTitle = BookTitle; can be interpreted as set the objects BookTitle to the function parameter BookTitle.

Similarly, this function should be as following

public int getRemainingChapters ()
{
    return (BookChapters - BookMark);
}
Rumi
  • 196
  • 7