• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stuck on isBorrowed method implementation.  RSS feed

 
Jeremy Levy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll start by saying I recently decided to pick up head first java (which I like very much) and try my hand at learning java. So i'm just starting out.

I'm going through the book and I decided it would be great to do some more hands on practice. I found a practice problem on the web and i'm having some problems completing it. Basically I have a class called Book. Its supposed to have a few simple methods that set the title of the book, mark it as returned or borrowed. So specifically my problem lies in the implementation of the method that decides if the book isBorrowed. I was thinking i'd use a simple if/else statement but the way i've been trying hasn't been successful. This is what i've tried so far.

A push in the right direction would be appreciated.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got three boolean variables ?
You don't seem to use isBorrowed, so get rid of that.
Next, can a book be both borrowed and returned at the same time ?
If not, then you only need one variable - lets call it borrowed. If it is true, then the book has been borrowed, if it is false then the book has been returned.
Your isBorrowed method then just needs to return the value of borrowed.
 
Jeremy Levy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply.

I guess I should have explained earlier. The template for this exercise was given. So I would like to keep all the methods how they are for now. There's a second part that uses this class for something else.
Basically the methods where written with the bodys empty so I just have to fill in the method body with whatever makes it work. I created the primitives at the top except for the first one String title;
The directions for this part of the exercise and what output I should is below.

First we need a class to model books. Start by creating a class called Book. Copy and paste the skeleton below. This class defines methods to get the title of a book, find out if it is available, borrow the book, and return the book. However, the skeleton that we provide is missing the implementations of the methods. Fill in the body of the methods with the appropriate code. The main method tests the methods. When you run the program, the output should be:

Title (should be The Da Vinci Code): The Da Vinci Code
Rented? (should be false): false
Rented? (should be true): true
Rented? (should be false): false


my output looks like this.

Title (should be The Da Vinci Code): The Da Vinci Code
Borrowed? (should be false): false
Borrowed? (should be true): true
Borrowed? (should be false): true



I need to use isBorrowed to check the state of the book. "Returns true if the book is rented, false otherwise"
I don't think a book should be able to be both borrowed and returned at the same time.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Levy wrote:The template for this exercise was given. So I would like to keep all the methods how they are for now. There's a second part that uses this class for something else.
Basically the methods where written with the bodys empty so I just have to fill in the method body with whatever makes it work. I created the primitives at the top

I didn't say you should get rid of any of your methods.
My comments were all about the primitives that you added.
Assuming a book can't be both borrowed and returned at the same time, you can implement all the methods using only the one boolean variable.

Delete the returned and isBorrowed variables and then think about how you can implement the returned() and isBorrowed() methods using only the borrowed variable.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you have a Book... Based on the requirements your book object should have a title and a state of whether its borrowed or returned (available)... As Joanne suggested you only need one field to hold the information of whether the book is out or in... it will be used like a switch in which you toggle on or off... use this switch in each of the methods given to perform the required operation... if its borrowed by calling borrowed() the state of the switch will be toggled making the book unavailable... when its returned by calling returned() the state of the switch will be toggled making the book available... when you call isBorrowed() its reports whether its available or not based on the current state of the switch...
 
Jeremy Levy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for the help! I got it now. That was simple enough. Guess I was just using too many variables and confusing myself on the isBorrowed logic. BTW, the switch analogy was great.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!