Results 1 to 12 of 12
Thread: Help needed with java
- 10-30-2009, 09:50 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 5
- Rep Power
- 0
Help needed with java
Hello everyone
I'm a Student and new to java and I have been given a question which I have to go through. I have come across a problem with one of the questions and am stuck, so I was wondering if you guys could help me out.
here is my code so far:
and this is the question/**
* A Class that maintains Information about a book
* This might form part of a larger application such
* as a library system, for example.
* @author (your name)
* @version (a version number or a date)
*/
public class Book
{
// instance variables or fields
private String author;
private String title;
/**
* Set the author and title when the book object is constructed
*/
public Book(String bookAuthor, String bookTitle)
{
author = bookAuthor;
title = bookTitle;
}
/**
* Return The name of the author.
*/
public String getAuthor()
{
return author;
}
/**
* Return The name of the title.
*/
public String getTitle()
{
return title;
}
}
any help will be greatly appreciated. ThankyouAdd a further instance variable/field pages to the Book class to store the number of pages in the book.
This should be of type int and should be set to 0 in the Constructor.
Add a second Constructor with signature
public Book(String bookAuthor, String bookTitle, int noPages) so it has a third parameter passed to it as well as the author and title;
this parameter is used - obviously?? - to initialise the number of pages.
Note: This is easiest done by making a copy of the existing Constructor and adding the parameter.
Add a getPages() accessor method that returns the number of pages in the book.
Add a method printDetails() to your Book class. This should print out the Author title and number of pages to the Terminal Window. It is your choice as to how the data is formatted, perhaps all on one line, perhaps on three, and with or without explanatory text. For instance you could print out in the format:
Title: Robinson Crusoe, Author: Daniel Defoe, Pages:226
Add a further instance variable/field refNumber() to your Book class. This stores the Library's reference number. It should be of type String and be initialised to the empty String "" in the constructor, as its initial value is not passed in as a parameter. Instead a public mutator method with the signature:
public void setRefNumber(String ref) should be created. The body of this method should assign the value of the method parameter ref to the refNumber.
Add a corresponding getRefNumber() accessor method to your class so you can check that the mutator works correctly
Modify your printDetails() method to include printing the reference number of the book.
However the method should print the reference number only if it has been set - that is the refNumber has a non-zero length.
If it has not been set, print "ZZZ" instead.
Hint Use a conditional statement whose test calls the length() method of the refNumber String and gives a result like:
Title: Jane Eyre, Author: Charlotte Bronte, Pages:226, RefNo: CB479 or, if the reference number is not set:
Title: Robinson Crusoe, Author: Daniel Defoe, Pages:347, RefNo: ZZZ
Modify your setRefNumber() method so that it sets the refNumber field only if the parameter is a string of at least three characters. If it is less than three, then print an error message (which must contain the word error) and leave the field unchanged
Add a further integer variable/field borrowed to the Book class, to keep a count of the number of times a book has been borrowed. It should (obviously??) be set to 0 in the constructor.
Add a mutator method borrow() to the class. This should increment (add 1 to) the value of borrowed each time it is called.
Include an accessor method getBorrowed() that returns the value of borrowed
Modify Print Details so that it includes the value of the borrowed field along with some explanatory text
- 10-30-2009, 09:58 PM #2
-
Agree. We have no idea just what you need help on.
Much luck.
- 10-30-2009, 11:06 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 5
- Rep Power
- 0
Well the question is in the second quote. it is asking me to add certian codes to my code (first quote) but I don't know where to put them and what I should write :(
-
where to put what? The second quote is just a print out of the assignment, and that's not what we're asking for. We want to know what specific question(s) you have. This means that you have to write out specifically in your own words what is tripping you up. This will require some effort on your part, but that's OK as it helps you to learn. Otherwise we really can't help you.
- 10-31-2009, 12:43 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Also posted at Sun Forums.
Ozzy: Fubarable is right, we need to know what your problem is. And it would be a very good thing to either have the dialog in one forum or, at the very least, let the participants in each forum know about the other posts. You are likely to get much the same response whereever you post your homework.
- 10-31-2009, 12:34 PM #7
Member
- Join Date
- Oct 2009
- Posts
- 5
- Rep Power
- 0
Ok sorry about that, as you may have realized I am not very good at explaining things. So I will try my best.
Basically In the first part it is asking me to adda field to store the number of pages and set the constructer to 0.
I did this at the end of my code, and this what I put:
then it says to add getPages() accessor methods so it returns the number of pages and I added this under the above code.* Create a page number to store number of pages in book.
*/
public int noPages()
{
nopages = 0;
}
}
but when I compile it I get 2 error messages and they both say/**
* @Return The number of pages in the book.
*/
public int getPages()
{
return pages;
}
"cannot find symbol variable noPages" and "cannot find symbol variable pages"
So what am I doing wrong exactly. I hope this made more sense.
- 10-31-2009, 01:57 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
If those methods are in your Book class I can imagine that your compiler whines at you: you don't have a (private) member variable 'pages' or 'noPages' in your Book object (why do you need both variables anyway?).
Your setter method is wrong too: it doesn't return anything so why make it of return type 'int' in the first place? Basically getters and setters have the following structure:
About your first confession: you should practice explaining stuff; no matter to whom, even to yourself, it really helps understanding something when you're able to explain the details of that something.Java Code:public class YourClass { private T property; // your property to be set and get. ... public void setProperty(T property) { ... this.property= property; // set the property } ... public T getProperty() { ... return this.property; // get the property } }
kind regards,
Jos
- 11-01-2009, 09:38 PM #9
Member
- Join Date
- Oct 2009
- Posts
- 5
- Rep Power
- 0
Thanks for that it helped a lot :D
But I have another problem now. I have to produce a code that prints the details of author, book and number of pages. this is my code:
But when I post my answer I get this back:/**
* Print book information.
*/
public void printDetails()
{
// Simulate the printing of book details.
System.out.println("##################");
System.out.println("# Title");
System.out.println("# Author");
System.out.println("# Pages");
System.out.println("##################");
System.out.println();
}
Does this mean that I have to put those exact words in the plaes where I have put Author, title and pages?# Test 1 (1.0 out of 1)
The compilation was successful
# Test 2 (0.0 out of 1)
The output should have been:
Author name included
Book Title included
Number of Pages included
Edit: I tried editing the code like this:
But I am getting the same error/**
* Print book information.
*/
public void printDetails()
{
// Simulate the printing of book details.
System.out.println("##################");
System.out.println("# Title: Robinson Crusoe");
System.out.println("# Author: Daniel Defoe");
System.out.println("# Pages:226");
System.out.println("##################");
System.out.println();
}Last edited by ozzy; 11-01-2009 at 09:43 PM.
-
1) Where are you calling the printDetails method so that this method will run?
2) Shouldn't this method use the fields held by the Book class including the author and title fields?
3) If the code you paste here is properly indented to begin with, it'll be much easier for us to read. Please consider this.
- 11-01-2009, 09:54 PM #11
Member
- Join Date
- Oct 2009
- Posts
- 5
- Rep Power
- 0
ermm I didn't quite understand that XD.
This is my full code so far:
basically I have to add a printDetails() to my book class and I have print out the Author title and number of pages to the Terminal Window. and it said:/**
* A Class that maintains Information about a book
* This might form part of a larger application such
* as a library system, for example.
* @author (your name)
* @version (a version number or a date)
*/
public class Book
{
// instance variables or fields
private String author;
private String title;
private int pages;
/**
* Set the author and title when the book object is constructed
*/
public Book(String bookAuthor, String bookTitle, int noPages)
{
author = bookAuthor;
title = bookTitle;
pages = noPages;
}
/**
* Return The name of the author.
*/
public String getAuthor()
{
return author;
}
/**
* Return The name of the title.
*/
public String getTitle()
{
return title;
}
/**
* Create a page number to store number of pages in book.
*/
public Book(int pages)
{
pages = 0;
}
/**
* @Return The number pages in book.
*/
public int getPages()
{
return pages;
}
/**
* Print book information.
*/
public void printDetails()
{
// Simulate the printing of book details.
System.out.println("##################");
System.out.println("# Title: Robinson Crusoe");
System.out.println("# Author: Daniel Defoe");
System.out.println("# Pages:226");
System.out.println("##################");
System.out.println();
}
}
"For instance you could print out in the format:
Title: Robinson Crusoe, Author: Daniel Defoe, Pages:226"
Do I have to add more code to it or something?
-
Right now the method will always print Title: Robinson Crusoe and Author: Daniel Defoe. Is this what you desire? Myself, I'd use the author and title fields that are present in the class.
For instance:
Again, all of your code is left justified and very difficult to read. Please only post indented code.Java Code:public class Song { String singer; String title; public Song(String singer, String title) { this.singer = singer; this.title = title; } // getters and setters, etc public void showStuff() { System.out.println("Song Info"); // If I want to show the object's singer, I'd use the field // in this System.out.println statement System.out.println("Singer: " + singer); //... same for title } }
Similar Threads
-
Help needed with downloader in java
By falcommoney in forum New To JavaReplies: 3Last Post: 01-28-2009, 01:31 PM -
java help needed
By mayhewj7 in forum New To JavaReplies: 1Last Post: 01-28-2009, 05:33 AM -
Help needed in Java
By javanewbie in forum New To JavaReplies: 2Last Post: 07-08-2008, 07:55 PM -
Java experts needed- 30 minute online Java projects
By michelle in forum Jobs OfferedReplies: 0Last Post: 03-05-2008, 11:47 PM -
Java help needed
By jeneal in forum New To JavaReplies: 0Last Post: 11-21-2007, 03:04 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks