Blue-J-book-exercise

Where is my mistake?

Page 1 of 1

11 Replies - 3764 Views - Last Post: 09 June 2009 - 06:56 AM Rate Topic: -----

#1 novich1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-June 09

Blue-J-book-exercise

Post icon  Posted 06 June 2009 - 09:55 AM

public class Book
{
	// The fields.
	private String author;
	private String title;

	/**
	 * Set the author and title fields when this object
	 * is constructed.
	 */
	public Book(String bookAuthor, String bookTitle)
	{
		author = bookAuthor;
		title = bookTitle;
	}

	// Add the methods here ...
}
	public String getbookAuthor()
	{
		return author;
	}


Mod edit - Please :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Blue-J-book-exercise

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,149
  • Joined: 07-September 06

Re: Blue-J-book-exercise

Posted 06 June 2009 - 10:02 AM

First of all, Java doesn't allow methods to not be a part of a class, so this code:
	public String getbookAuthor()
	{
		return author;
	}


Is placed incorrectly.

Other than that, I don't see any errors in the code provided.

Are you getting any other errors?
Was This Post Helpful? 0
  • +
  • -

#3 Tanira  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 102
  • Joined: 30-May 09

Re: Blue-J-book-exercise

Posted 06 June 2009 - 10:04 AM

View Postnovich1, on 6 Jun, 2009 - 08:55 AM, said:

public class Book
{
	// The fields.
	private String author;
	private String title;

	/**
	 * Set the author and title fields when this object
	 * is constructed.
	 */
	public Book(String bookAuthor, String bookTitle)
	{
		author = bookAuthor;
		title = bookTitle;
	}

	// Add the methods here ...

	public String getbookAuthor()
	{
		return author;
	}
}


Try that. You were ending public class Book{.
Always check your curly braces the top curly brace should go all the way to the bottom.
Was This Post Helpful? 0
  • +
  • -

#4 novich1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-June 09

Re: Blue-J-book-exercise

Posted 06 June 2009 - 10:09 AM

Thanks for help, a am really appreciated.
Was This Post Helpful? 0
  • +
  • -

#5 novich1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-June 09

Re: Blue-J-book-exercise

Posted 07 June 2009 - 11:13 PM

public class Book
{
	// The fields.
	private String author;
	private String title;
	private int pages;
	private String refNumber;

	/**
	 * Set the author and title fields when this object
	 * is constructed.
 public Book(String bookAuthor, String bookTitle,int bookPages,String bookrefNumber)
	{
		author = bookAuthor;
		title = bookTitle;
		pages = bookPages;
		refNumber = bookrefNumber;
	}
// Add the methods here ...

	public String getbookAuthor()
	{
		return author;
	}
	public String getTitle()
	{
		return title;
		
	}
  public String   printAuthor()
	{

	  return  author;
	}
	 
	 public String ptintTitle()
	 {
		 return title;
		}
		public int getPages()
  {
		return pages;
	}
	public int printPages()
	{
		return pages;
	}
	public void printDetails()
	{

		System.out.println("book1");
		 System.out.println(" bookAuthor ");
		System.out.println(" title ");
		System.out.println (" pages ");

	   
	}
}	


I do my exercise(book-exercise)but when i write the name of book,author and title there is no result.When a call method "void printDetails"
there is no details except ("bookAuthor","title","pages")Why?

*Edited to add the [ code] tags

This post has been edited by pbl: 08 June 2009 - 06:25 PM

Was This Post Helpful? 0
  • +
  • -

#6 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 664
  • Joined: 20-March 09

Re: Blue-J-book-exercise

Posted 08 June 2009 - 12:32 AM

Can you post your public static void main(String[] args) method?
Was This Post Helpful? 0
  • +
  • -

#7 novich1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-June 09

Re: Blue-J-book-exercise

Posted 08 June 2009 - 11:46 AM

public static void main(String[]args)
	{
		System.out.println("book1");
		System.out.println("bookAuthor");
		System.out.println("title");
		System.out.println("pages");
	}


*Edited to add the [ code] tags

This post has been edited by pbl: 08 June 2009 - 06:26 PM

Was This Post Helpful? 0
  • +
  • -

#8 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Blue-J-book-exercise

Posted 08 June 2009 - 12:33 PM

Your doing this slightly wrong. In future, you will problably be asking for inputs for the values. But as you are new we will hard code them. So you have a constructor
public Book(String bookAuthor, String bookTitle,int bookPages,String bookrefNumber)

which for now you can think of creates 1 book Object (purist hate this description but i think it makes it easier for newbies). In order to create our book, we need to do a few things. In you main, you need to point to the type of Object your creating and the values you want to give it. We know from your constructor that your object expects a String, Sting, int and a String, in precisely that order.
So lets reference the Object type and pass some values
Book myBook = new BooK("BFG", "Raul Dhal", 35, "Penguin books");


So the first part is saying we are creating a Book Object called myBook (myBook is your variable name for this Object now). Then in the second part, we are assigning the Object with them values. So now we have created a book called myBook. To print this out now, all you need to do is
System.out.println(myBook.toString());

So you are printing out the whole Object, and this will provide you with all the information about the Book you have created. If you are unfamiliar with toString, dont worry to much about that now. In easy language, it just turns your Book Object into a String so that it can be printed. So thats the idea about creating Objects. What you were doing before is just printing out four Strings. You were not creating any Objects. So lets take your main method and create some Books
public static void main(String[] args){

Book myBook = new BooK("BFG", "Raul Dhal", 35, "Penguin books");
Book myBook1 = new BooK("Harry Potter", "JK Rowling", 2, "Peacock books");
Book myBook2 = new BooK("Angels and Demons", "Dan Brown", 65, "Wiked books");

System.out.println(myBook.toString()+"\n");
System.out.println(myBook1.toString()+"\n");
System.out.println(myBook2.toString()+"\n");

}


The "\n" just adds a blank line after every Book output so they are not all jumbled together. Have a play about with this and see where you go. Any more help needed just ask. Code not tested.
Was This Post Helpful? 0
  • +
  • -

#9 n00bc0der  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 27-November 08

Re: Blue-J-book-exercise

Posted 08 June 2009 - 12:36 PM

You need to create the objects like this:

Book newBook = new Book("Example", "Chris Fraser");

Then you can use a method to print:

newBook.printDetails();

You have a bit of errors in your code, but I'm not going to do it all for you. There's my piece of help for you.
Was This Post Helpful? 0
  • +
  • -

#10 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Blue-J-book-exercise

Posted 08 June 2009 - 12:37 PM

Also, just seen you have printDetails method in yuor book class. This can be used also in place of toString. You would have to alter it though cos at the moment you are just asking it to print out the Strings you have given it, not the variables which hold the values.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Blue-J-book-exercise

Posted 08 June 2009 - 06:27 PM

Novish1
I have edited your previous posts to add the [ code] tags
in the future please
:code:

Help us to help you
Was This Post Helpful? 0
  • +
  • -

#12 novich1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-June 09

Re: Blue-J-book-exercise

Posted 09 June 2009 - 06:56 AM

Thanks,for help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1