• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with book oriented classes  RSS feed

 
Mani Sudi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using blue j and I need to help to write the coding for two scenarios.

1. The Book class is a subclass of the LoanItem class and has just one attribute, which corresponds to the return date (YYMMDD) , which is a string of text. The parameters of the constructor represent the ISBN, the title and the Publisher , and these three values are passed together to the constructor of the LoanItem class. The return date is initialised in the constructor by being assigned the value of “000000” and has a corresponding accessor method.

The Book class has a method to borrow a book, which accepts a borrower number and a return date as parameters.
The month and day are extracted from the return date. If the borrower number is less than or equal to 0, a suitable error message is output. Otherwise (i.e. if the borrower number is ok), the month and day are checked together in a compound condition and, if either the month is greater than 12 or the day is greater than 31, a suitable error message is output. Otherwise (i.e. if there are no errors in the parameters) the return date of the book is given the value input and the method to set the borrower number residing in the LoanItem class is called. A method to return a book will call the method in the LoanItem class that resets the borrower number to 0 and will assign the return date to “000000”. A method to display a book will call the method in LoanItem to display the ISBN, the title of the item, the publisher. and the borrower number before displaying the return date.

2. The Offprint class is also a subclass of the LoanItem class and has two attributes, which correspondto a chapter of the book and the hour the chapter is to be returned. Both attributes are integers. The parameters of the constructor represent the ISBN, the title of the item, the publisher and these three values are passed together to the constructor of the LoanItem class. The chapter and hour to be returned are initialised to 0 in the constructor and they both have corresponding accessor methods.

The Offprint class has a method to release a chapter of a book, which accepts a borrower number, the chapter of the book and the current hour as parameters. If the borrower number is less than or equal to 0,a suitable error message is output. Otherwise (i.e. if the borrower number is ok), the chapter and the current hour are checked together in a compound condition and, if either the chapter is less than or equal to 0, or the current hour is less than 9 (i.e. before the library opens at 9am), a suitable error message is output. Otherwise (i.e. if there are no errors in the parameters) the method to set the borrower number residing in the LoanItem class is called, the chapter of the book is given the value input and the hour the offprint is to be returned is given the following value. If the current hour is greater than or equal to 18, the hour the offprint is to be returned is set to 21 (because the library closes at 9pm), otherwise the hour the offprint is to be returned is set to the current hour + 3 (because the offprint can be loaned for up to 3 hours)

A method to return an offprint will call the method in the LoanItem class that resets the borrower number to 0 and will assign the hour the offprint is to be returned to 0 and the book chapter to 0. A method to display an off print will call the method in LoanItem to display the ISBN, the title of the item, the publisher and the borrower number, before displaying the hour the offprint is to be returned and the book chapter.

I have done the LoanItem class myself but I need help on the other 2 classes. I would really appreciate if someone helps. Thanks.
 
Mani Sudi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the coding for the LoanItem Class
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch. What do you have so far, and where are you stuck making progress? (Aside from the fact that the class you posted won't compile due to multiple problems with uppercase/lowercase and incorrect variable names).
 
Mani Sudi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Welcome to JavaRanch. What do you have so far, and where are you stuck making progress? (Aside from the fact that the class you posted won't compile due to multiple problems with uppercase/lowercase and incorrect variable names).


This is what I have done for the book class:

instance variables - replace the example below with your own
private String returnDate "TTMMDD"

/**
* Constructor for objects of class Book
*/
public Book(string isbn, string publisher, string title)
{
super(theISBN, theTitle, thePublisher)
}

/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int setReturnDate()
{
"000000"
}

I have The Book c lass is a subclass of the LoanItem class and has just one attribute, which corresponds to the return date(YYMMDD) and the return date is initialised in the constructor by being assigned the value of
“000000” and has a corresponding accessor method. I'm not sure the coding above is correct.

The borrow number has to be equal or less than 0 but I'm not sure how to write the if or else on the program. This is what I have done for the if and else so far, it's incomplete:

/**
*
*/
public void borrownumber
{
if (borrownumber <0
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My first advice is: compile often. That code snippet, too, has several problems that prevent compilation. The longer you wait with compilation, the more errors there will be, which will become frustrating at some point.
 
Mani Sudi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Welcome to JavaRanch. What do you have so far, and where are you stuck making progress? (Aside from the fact that the class you posted won't compile due to multiple problems with uppercase/lowercase and incorrect variable names).


what do you mean it "won't compile due to multiple problems with uppercase/lowercase and incorrect variable names"? I would really appreciate if you correct me.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried compiling the LoanItem class? What you posted will definitely not compile.
 
Mani Sudi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Have you tried compiling the LoanItem class? What you posted will definitely not compile.


No it's not compiling. I just came on here, so that somebody would actually help me. it's so frustrating trying to compile the program.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's why I advise to compile often so that the error messages don't get too numerous. The first to correct is uppercase/lowercase spelling. Java is case-sensitive, so "title" is not the same as "Title" (that's just one example, it happens several times).

And you need to pay attention to what a method returns. If it's declared "void" then it can't return anything. But if it has a return type, then it actually needs to return a value of that type. And all methods must either be void, or declare a return type. All 4 methods you have in the LoanItem class suffer from at least one of the problems.

And in the constructor you're using variables that you have not declared anywhere.
 
Mani Sudi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Yes, that's why I advise to compile often so that the error messages don't get too numerous. The first to correct is uppercase/lowercase spelling. Java is case-sensitive, so "title" is not the same as "Title" (that's just one example, it happens several times).

And you need to pay attention to what a method returns. If it's declared "void" then it can't return anything. But if it has a return type, then it actually needs to return a value of that type. And all methods must either be void, or declare a return type. All 4 methods you have in the LoanItem class suffer from at least one of the problems.

And in the constructor you're using variables that you have not declared anywhere.


Oh I see, I'm starting to understand. I have a problme on this :

public int setBorrownumber()
{
borrowNumber = 0;
}

Bluej is saying that the code above is missing return statement.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right - the method is declared to return an "int", but it should probably return nothing. You may also want to think about whether you ever need to set the borrower number to something other than 0 (in which case that method should have a parameter).
 
Mani Sudi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Right - the method is declared to return an "int", but it should probably return nothing. You may also want to think about whether you ever need to set the borrower number to something other than 0 (in which case that method should have a parameter).


The LoanItem class finally been complied, the method that I used to declare the borrow number was void. I just need to work on the other 2 classes but it will be tricky..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!