Java Class Book()

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 10162 Views - Last Post: 07 October 2009 - 10:47 PM Rate Topic: -----

#1 rezurexun  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 07-October 09

Java Class Book()

Post icon  Posted 07 October 2009 - 06:25 PM

public class Book
{
	private String title;
	private String author;
	private int numpages;

	public Book(int np) //np = numpages = number of pages
	{
		set(np);
	}

	public Book()
	{
		setBook("", "", 0);
	}

}



this is what i have so far and i am stumped..i don't even know where to start..if anyone can help me, please feel free..i need all the help that i can get..thanks in advance..

Is This A Good Question/Topic? 0
  • +

Replies To: Java Class Book()

#2 pbl  Icon User is offline

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

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

Re: Java Class Book()

Posted 07 October 2009 - 06:37 PM

That would be a better start

public class Book
{
	private String title;
	private String author;
	private int numpages;

	public Book(String t, String a, int np)
	{
		title = t;
		author = a;
		numpages = np;
	}

	public Book()
	{
		this("", "", 0);
	}

}


Was This Post Helpful? 1
  • +
  • -

#3 rezurexun  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 07-October 09

Re: Java Class Book()

Posted 07 October 2009 - 06:47 PM

so my basic format and outline was good enough right? lol

btw..why did you use "this"? can you explain what that means?
Was This Post Helpful? 0
  • +
  • -

#4 rezurexun  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 07-October 09

Re: Java Class Book()

Posted 07 October 2009 - 06:58 PM

Thanks for the quick reply, pbl! Well just to give you guys a background, this is the question that my professor gave me:

*************************************************************
1.Design a class Book.
a.It characteristics are the title, author and number of pages.
b.Create the default constructor.
c.Create constructors with parameters.
d.Create the set and get methods for each data member.
e.Create the toString( ) method.

Implement and test the various methods for 4 Book objects.

2.Extend the class Book.
a.Add the method equals that returns true if two objects contain the same title and author.
b.Add the copy constructor.

Implement and test these methods for the Book objects.
*************************************************************

And this is what I have so far:

public class Book
{
	private String title;
	private String author;
	private int numpages;
	
	public Book(String t, String a, int np) //np = numpages = number of pages
	{
		title = t;
		author = a;
		numpages = np;
	}

	public Book()
	{
		this("", "", 0);
	}

	public String getTitle()
	{
		return title;
	}

	public String getAuthor()
	{
		return author;
	}

	public int getNumPages()
	{
		return numpages;
	}
	
	public void printBook()
	{
		
	}
}



I need A LOT of help...
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

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

Re: Java Class Book()

Posted 07 October 2009 - 07:01 PM

View Postpbl, on 7 Oct, 2009 - 05:37 PM, said:

That would be a better start

public class Book
{
	private String title;
	private String author;
	private int numpages;

	public Book(String t, String a, int np)
	{
		title = t;
		author = a;
		numpages = np;
	}

	public Book()
	{
		this("", "", 0);
	}

}



this("", "", 0);

recalls myself but the constructor with 3 parameters passing as arguments "" "" and 0 so:
the constructor public Book(String t, String a, int np) will be call which will set title and author to "" and numpage to 0
Was This Post Helpful? 0
  • +
  • -

#6 rezurexun  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 07-October 09

Re: Java Class Book()

Posted 07 October 2009 - 07:05 PM

Oh, okay. I get it. Thanks for clearing that up for me. I'm very new in Java programming. I did VB2005 last semester and now we just started on this new language. I still have a long way to go and I need lots and lots of help. Thanks for contributing!
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

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

Re: Java Class Book()

Posted 07 October 2009 - 07:06 PM

I'll let you do the getter() and setter() this is documented everywhere here is your test and toString

public class Book
{
	private String title;
	private String author;
	private int numpages;

	public Book(String t, String a, int np)
	{
		title = t;
		author = a;
		numpages = np;
	}

	public Book()
	{
		this("", "", 0);
	}

	public String toString() {
	   return "Title: " + title + " Author: " + author + " NbPages: " + numpages;
	}

	// tester
	public static void main(String[] args) {
	   Book book = new Book("Hound of the Baskerville", "Arthur Conan Doyle", 240);
	   System.out.println("My favorite book is: " + book);
	}

}


Was This Post Helpful? 1
  • +
  • -

#8 rezurexun  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 07-October 09

Re: Java Class Book()

Posted 07 October 2009 - 07:12 PM

Thank you so much, pbl! I owe you big time! I'll try my hardest to figure out the "getter()" and "setter()" so please be patient with me. Thanks again!
Was This Post Helpful? 0
  • +
  • -

#9 rezurexun  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 07-October 09

Re: Java Class Book()

Posted 07 October 2009 - 07:33 PM

PBL!! I FINISHED IT!! YEAY!! I'M SO HAPPY LMAO!!

Here's my code...

Book.java:
public class Book
{
	private String title;
	private String author;
	private int numpages;
	
	public Book(String t, String a, int np)
	{
		title = t;
		author = a;
		numpages = np;
	}

	public Book()
	{
		this("", "", 0);
	}

	public String toString()
	{
	   return ("Title: " + title + ", Author: " + author + ", Number of Pages: " + numpages);
	}
	
	public void setBook(String t, String a, int np)
	{
		title = t;
		author = a;
		numpages = np;
	}

	public String getTitle()
	{
		return title;
	}

	public String getAuthor()
	{
		return author;
	}

	public int getNumPages()
	{
		return numpages;
	}
}



Main.java:
public class Main
{
	public static void main(String[] args)
	{
	   Book Book = new Book();
	   Book.setBook("Book 1", "Author 1", 101);
	   System.out.println(Book);
	   Book.setBook("Book 2", "Author 2", 102);
	   System.out.println(Book);
	   Book.setBook("Book 3", "Author 2", 103);
	   System.out.println(Book);
	   Book.setBook("Book 4", "Author 2", 104);
	   System.out.println(Book);
	}
}



Does it look good?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

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

Re: Java Class Book()

Posted 07 October 2009 - 08:35 PM

View Postrezurexun, on 7 Oct, 2009 - 06:33 PM, said:

Main.java:
public class Main
{
	public static void main(String[] args)
	{
	   Book Book = new Book();
	   Book.setBook("Book 1", "Author 1", 101);
	   System.out.println(Book);
	   Book.setBook("Book 2", "Author 2", 102);
	   System.out.println(Book);
	   Book.setBook("Book 3", "Author 2", 103);
	   System.out.println(Book);
	   Book.setBook("Book 4", "Author 2", 104);
	   System.out.println(Book);
	}
}



Does it look good?


Yes but very very confusing

Book Book = new Book();

The concensus between all Java (OK 98.5% ol all Java) programmers is:
Class starts with a capital letter, object by a lower case letter

Book book = new Book();

then you can do

book.setBook();

Book.setBook() is very confusing... are you calling the static method of the class book or an instance method

also you haven't fiullfill your assignment you have all your getters but your teacher wants the follwring methods:

void setTitle(String title)
void setAuthor(String author)
void setNumPages(int np)
Was This Post Helpful? 0
  • +
  • -

#11 rezurexun  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 07-October 09

Re: Java Class Book()

Posted 07 October 2009 - 08:49 PM

Oh yeah! I was just thinking of that! Thanks for the advice. I will append my code right away and show you the final product. But while I'm doing that, can you please explain how I'm going to do the second part? I already tried it. Take a look...

	public boolean equals(Book otherBook)
	{
		return (title == otherBook.title && author == otherBook.author);

	}

	public void makeCopy(Book otherBook)
	{
		title = otherBook.title;
		author = otherBook.author;
	}

	public Book getCopy()
	{
		Book temp = new Book();

		temp.title = title;
		temp.author = author;

		return temp;
	}



My problem is that I don't know how I'm going to incorporate that into my MAIN.JAVA class... :blink:

This is how I tried doing it. It works fine but it seems so...what's the word...USELESS??? lmao! I don't think that's what it's supposed to do...

	public static void main(String[] args)
	{
		Book Book1 = new Book();
		Book Book2 = new Book();
		Book Book3 = new Book();
		Book Book4 = new Book();

		Book1.setBook("Book 1", "Author 1", 101);
		System.out.println(Book1);
		Book2.setBook("Book 2", "Author 2", 102);
		System.out.println(Book2);
		Book3.setBook("Book 3", "Author 3", 103);
		System.out.println(Book3);
		Book4.setBook("Book 4", "Author 4", 104);
		System.out.println(Book4);
		
		if (Book1.equals(Book2) || Book1.equals(Book3) || Book1.equals(Book4)
				|| Book2.equals(Book1) || Book2.equals(Book3) || Book2.equals(Book4)
				|| Book3.equals(Book1) || Book3.equals(Book2) || Book3.equals(Book4)
				|| Book4.equals(Book1) || Book4.equals(Book2) || Book4.equals(Book3))
			System.out.println("There are same books in this list!");
		else
			System.out.println("Every book is different!");
	}



Thanks for all your help! :D

This post has been edited by rezurexun: 07 October 2009 - 08:51 PM

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

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

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

Re: Java Class Book()

Posted 07 October 2009 - 08:53 PM

public boolean equals(Book otherBook)
	{
		return (title == otherBook.title && author == otherBook.author);

	}


you cannot compare String using the == operator you have to

title.equals(otherBook.title)
Was This Post Helpful? 0
  • +
  • -

#13 rezurexun  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 07-October 09

Re: Java Class Book()

Posted 07 October 2009 - 08:58 PM

ok i corrected that :D thanks for the good eye!

also, i don't know how i'm going to fill the list now...

am i going to have to do:

book1.setTitle("blah");
book1.setAuthor("blah blah");
book1.setNumPages(1);

for all of the books that i'm creating? that's a lot of work, lol
Was This Post Helpful? 0
  • +
  • -

#14 rezurexun  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 07-October 09

Re: Java Class Book()

Posted 07 October 2009 - 09:11 PM

i just figured something out and you're prolly gonna laugh cos i think it's so awesome..but..here it is..this is all i have to do to make a record:

book1.setTitle("Book A");
book1.setAuthor("Author A");
book1.setNumPages(100);
System.out.println(book1.toString());

i didn't know i could use "System.out.println" on "book1.toString()", lol

i feel so noOb... :blink:

but oh well...hahaha...

oh and i can access the "title", "author", and "numpages" on my MAIN CLASS now

all i did was change the declarations to "public" in my BOOK CLASS lmao

so many things i figure out by being a nOOb..lol

i'm gonna keep trying this..

i appreciate all your help!

KEEP IT COMING :D
Was This Post Helpful? 1
  • +
  • -

#15 pbl  Icon User is offline

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

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

Re: Java Class Book()

Posted 07 October 2009 - 09:13 PM

View Postrezurexun, on 7 Oct, 2009 - 08:09 PM, said:

i just figured something out and you're prolly gonna laugh cos i think it's so awesome..but..here it is..this is all i have to do to make a record:

book1.setTitle("Book A");
book1.setAuthor("Author A");
book1.setNumPages(100);
System.out.println(book1.toString());

i didn't know i could use "System.out.println" on "book1.toString()", lol

i feel so noOb... :blink:

but oh well...hahaha...


Actually if you do

System.out.println(object);

Java calls the toString() method of this object that is why

System.out.println("My favorite book is: " + book);

worked

Now if you want to save a few nano seconds to the Java compoiler when you can append the .toString() to your object
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2