Help with making a book class

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 3238 Views - Last Post: 20 September 2014 - 02:12 PM Rate Topic: -----

#16 DroidsEatApples   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 17-September 14

Re: Help with making a book class

Posted 19 September 2014 - 05:37 PM

View Postmacosxnerd101, on 19 September 2014 - 09:25 AM, said:

I think you're encountering clear issues with getting things working. I'm not confident enough to say that your code satisfies your requirements (aside from the test), as I do not know your requirements. I don't see any glaring issues, other than what I've pointed out. As I've said though- your professor and your course TAs are the best folks to give you the rubber stamp on your project.


So I got super mixed up and I was mixing my Book and Trilogy Class the whole time.

A book (for this Java course), consists of 3 variables: Title, Author and Publisher.

A Trilogy, consists of 4 variables: 3 Books, 3 Titles, 3 Authors and 3 Publishers.

Now here is what I have implemented:

public class Book {

	//variables
	private String Title;
	private String Author;
	private String Publisher;

	//constructor
	public Book(String Title, String Author, String Publisher) {
		this.Title = Title;
		this.Author = Author;
		this.Publisher = Publisher;
	}

	// setters and getters
	public void setBook(String Title, String Author, String Publisher) {
		this.Title = Title;
		this.Author = Author;
		this.Publisher = Publisher;
	}

	public String getTitle() {
		return Title;
	}

	public void setTitle(String Title) {
		this.Title = Title;
	}

	public String getAuthor() {
		return Author;
	}

	public void setAuthor(String Author) {
		this.Author = Author;
	}

	public String getPublisher() {
		return Publisher;
	}

	public void setPublisher(String Publisher) {
		this.Publisher = Publisher;
	}
}



public class Trilogy {

	public static void main(String[] args) {
	}

	// variables
	private String Book1, Book2, Book3;
	private String Title;
	private String Author;
	private String Publisher;

	// constructor
	public Trilogy(String Book1, String Book2, String Book3, String Title,
			String Author, String Publisher) {
		this.Book1 = Book1;
		this.Book2 = Book2;
		this.Book3 = Book3;
		this.Title = Title;
		this.Author = Author;
		this.Publisher = Publisher;
	}

	// setters and getters
	public void setTrilogy(String Book1, String Book2, String Book3,
			String Title, String Author, String Publisher) {
		this.Book1 = Book1;
		this.Book2 = Book2;
		this.Book3 = Book3;
		this.Title = Title;
		this.Author = Author;
		this.Publisher = Publisher;

	}
	public String getBook1() {
	return Book1;
	}
	
	public String getBook2() {
		return Book2;
	}
	public String getBook3() {
		return Book3;
	}

	public String getTitle() {
		return Title;
	}

	public void setTitle(String Title) {
		this.Title = Title;
	}

	public String getAuthor() {
		return Author;
	}

	public void setAuthor(String Author) {
		this.Author = Author;
	}

	public String getPublisher() {
		return Publisher;
	}

	public void setPublisher(String Publisher) {
		this.Publisher = Publisher;

	}
}



This last bit of code is my homework tester (this is not part of my code):

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class Hw3Tester {

	@Test
	public void testBookConstructor() {
		Book book = new Book("Fellowship of the Ring", "J.R.R. Tolkien",
				"George Allen && Unwin");
		Assert.assertEquals("Fellowship of the Ring", book.getTitle());
		Assert.assertEquals("J.R.R. Tolkien", book.getAuthor());
		Assert.assertEquals("George Allen && Unwin", book.getPublisher());
		score = score + 1;
	}

	@Test
	public void testSetTitle() {
		Book book = new Book("Fellowship of the Ring", "J.R.R. Tolkien",
				"George Allen && Unwin");
		book.setTitle("The Hobbit");
		Assert.assertEquals("The Hobbit", book.getTitle());
		score = score + 1;
	}

	@Test
	public void testSetAuthor() {
		Book book = new Book("Fellowship of the Ring", "J.R.R. Tolkien",
				"George Allen && Unwin");
		book.setAuthor("David Gowans");
		Assert.assertEquals("David Gowans", book.getAuthor());
		score = score + 1;
	}

	@Test
	public void testSetPublisher() {
		Book book = new Book("Fellowship of the Ring", "J.R.R. Tolkien",
				"George Allen && Unwin");
		book.setPublisher("David Gowans");
		Assert.assertEquals("David Gowans", book.getPublisher());
		score = score + 1;
	}

	@Test
	public void testSetTrilogyName() {
		Trilogy trilogy = new Trilogy();
		trilogy.setName("The Lord of the Rings");
		Assert.assertEquals("The Lord of the Rings", trilogy.getName());
		score = score + 1;
	}

	@Test
	public void testSetBook1() {
		Book book1 = new Book("Fellowship of the Ring", "J.R.R. Tolkien",
				"George Allen && Unwin");

		Trilogy trilogy = new Trilogy();
		trilogy.setBook1(book1);
		trilogy.setName("The Lord of the Rings");

		Book actualBook = trilogy.getBook1();
		Assert.assertEquals(book1.getTitle(), actualBook.getTitle());
		Assert.assertEquals(book1.getAuthor(), actualBook.getAuthor());
		Assert.assertEquals(book1.getPublisher(), actualBook.getPublisher());

		score = score + 1;
	}

	@Test
	public void testSetBook2() {
		Book book2 = new Book("The Two Towers", "J.R.R. Tolkien",
				"George Allen && Unwin");
		Trilogy trilogy = new Trilogy();
		trilogy.setBook2(book2);
		trilogy.setName("The Lord of the Rings");

		Book actualBook = trilogy.getBook2();
		Assert.assertEquals(book2.getTitle(), actualBook.getTitle());
		Assert.assertEquals(book2.getAuthor(), actualBook.getAuthor());
		Assert.assertEquals(book2.getPublisher(), actualBook.getPublisher());

		score = score + 1;
	}

	@Test
	public void testSetBook3() {
		Book book3 = new Book("The Return of the King", "J.R.R. Tolkien",
				"George Allen && Unwin");
		Trilogy trilogy = new Trilogy();
		trilogy.setBook3(book3);
		trilogy.setName("The Lord of the Rings");

		Book actualBook = trilogy.getBook3();
		Assert.assertEquals(book3.getTitle(), actualBook.getTitle());
		Assert.assertEquals(book3.getAuthor(), actualBook.getAuthor());
		Assert.assertEquals(book3.getPublisher(), actualBook.getPublisher());

		score = score + 1;
	}

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		score = 0;
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		System.out.println("Test Score = " + score + "/8");
		System.out
				.println("The final 2 points for the assignment will be graded manually.");
		System.out
				.println("1 point for comments. Your code must always be commented");
		System.out
				.println("1 point for your nouns and verbs list. Be sure to include this in your eclipse project that you zip and submit.");
	}

	private static int score;
}



A lot of errors got resolves on the homework tester, but I am still getting errors on tests 5, 6, 7 and 8 which lead me to believe that there is something wrong with my code that is creating these errors.

I feel like I am a lot closer, but I am confused to what is creating these errors.
Was This Post Helpful? 0
  • +
  • -

#17 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Help with making a book class

Posted 19 September 2014 - 05:41 PM

Quote

A Trilogy, consists of 4 variables: 3 Books, 3 Titles, 3 Authors and 3 Publishers.

It shouldn't. It should simply consist of 3 Books. Each Book has-a title, author, and publisher. You shouldn't be storing the title, author, and publisher fields in the Trilogy class.
Was This Post Helpful? 0
  • +
  • -

#18 DroidsEatApples   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 17-September 14

Re: Help with making a book class

Posted 19 September 2014 - 05:44 PM

Well, my assignment says for the Trilogy class there are 4 variables that I need setters and getters for, the only variables I can think of are the ones I mentioned. Shouldn't it be the same with the inclusion of "Book"? Or if it is Book 1, Book 2, Book 3, and then Trilogy, but that doesn't make as much sense to me.
Was This Post Helpful? 0
  • +
  • -

#19 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Help with making a book class

Posted 19 September 2014 - 05:56 PM

Quote

Well, my assignment says for the Trilogy class there are 4 variables that I need setters and getters for, the only variables I can think of are the ones I mentioned.

I can't think of four variables. Though frankly, the spec should tell you the attributes of a Trilogy.

Quote

Or if it is Book 1, Book 2, Book 3, and then Trilogy, but that doesn't make as much sense to me.

A Trilogy is a collection of three Books, by definition. It makes perfect sense to me, though I'd advocate a Book[].
Was This Post Helpful? 0
  • +
  • -

#20 DroidsEatApples   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 17-September 14

Re: Help with making a book class

Posted 19 September 2014 - 06:01 PM

View Postmacosxnerd101, on 19 September 2014 - 05:56 PM, said:

Quote

Well, my assignment says for the Trilogy class there are 4 variables that I need setters and getters for, the only variables I can think of are the ones I mentioned.

I can't think of four variables. Though frankly, the spec should tell you the attributes of a Trilogy.

Quote

Or if it is Book 1, Book 2, Book 3, and then Trilogy, but that doesn't make as much sense to me.

A Trilogy is a collection of three Books, by definition. It makes perfect sense to me, though I'd advocate a Book[].


Well, I am just going based off what my assignment says:


First create a list of nouns and verbs from the bulleted points in the Problem section. (Hint: there are no verbs in this case).
Drag and drop your list into your eclipse project.
Next implement your Book class.
add instance variables from your noun list that are appropriate for a Book. (Hint: there are 3)
implement both a setter and a getter for each instance variable.
a setter should follow the book's naming convention set{VariableName}. Example: setAuthor.
a getter should follow the book's naming convention get{VariableName}. Example: getAuthor.
implement a constructor that takes as arguments title, author, and publisher (in that order).
You can now start running tests. To run the tests right click on the Hw3Tester.java file -> run as... Junit Test. You may get a box saying you have errors. Ignore it and click "Proceed".
You will see some tests passed (green) and some tests failed (red) because we have not yet implemented our Trilogy class.
Next implement your Trilogy class.
add instance variables from your noun list that are appropriate for a Trilogy. (Hint: there are 4).
implement both a setter and a getter for each instance variable.
a setter should follow the book's naming convention set{VariableName}. Example: setBook1.
a getter should follow the book's naming convention get{VariableName}. Example: getBook1.
You can again run your tests as above. If everything was implemented correctly all tests pass you should see a green bar.
If all tests did not pass you can click on the failing test and it give you information on why it failed.
You can look at the test code for the test and see what the expected values are and what the tests are doing.

If you can make better sense of it, by all means, but he clearly says there are 3 variables for Book and 4 for Trilogy.

Based on my code, I know I am close because I have passed half of his tests already.

The point that isn't working is that it says that the Trilogy is not defined, almost all of the test errors stem from that.
Was This Post Helpful? 0
  • +
  • -

#21 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Help with making a book class

Posted 19 September 2014 - 06:41 PM

Quote

Next implement your Trilogy class.
add instance variables from your noun list that are appropriate for a Trilogy. (Hint: there are 4).

You have three Books, Title, Author, and Publisher. That's 6 variables, not 4. I would be inclined to look over your instructor's tests, at this line: trilogy.setName("The Lord of the Rings");.

Quote

The point that isn't working is that it says that the Trilogy is not defined, almost all of the test errors stem from that.

Please post the errors exactly as they appear.
Was This Post Helpful? 0
  • +
  • -

#22 DroidsEatApples   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 17-September 14

Re: Help with making a book class

Posted 20 September 2014 - 10:15 AM

Here are the errors in HW Tester:

The constructor Trilogy() is undefined

Multiple markers at this line
- The method setName(String) is undefined for the type
Trilogy

The constructor Trilogy() is undefined

The method setBook1(Book) is undefined for the type Trilogy

The method setName(String) is undefined for the type Trilogy

Type mismatch: cannot convert from String to Book,

Then I get the same errors except it says with "setBook2, and setBook3".

Don't really understand.

This post has been edited by macosxnerd101: 20 September 2014 - 10:29 AM
Reason for edit:: Removed quote

Was This Post Helpful? 0
  • +
  • -

#23 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Help with making a book class

Posted 20 September 2014 - 10:32 AM

First, you don't have to quote every post, especially large posts. There is a REPLY button at the button of the thread.

Quote

Multiple markers at this line
- The method setName(String) is undefined for the type
Trilogy

The constructor Trilogy() is undefined

The method setBook1(Book) is undefined for the type Trilogy

The method setName(String) is undefined for the type Trilogy

You didn't define these methods in the Trilogy class. Look at your code- where do you see these methods in the Trilogy class? You have to define a method to use it.

Quote

Type mismatch: cannot convert from String to Book,

I already explained- you need to be using type Book to model your Books in the Trilogy class, not type String. Also- why aren't you using Strings for author, title, and publisher in your Book class, but you're using them in your Trilogy class?
Was This Post Helpful? 0
  • +
  • -

#24 DroidsEatApples   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 17-September 14

Re: Help with making a book class

Posted 20 September 2014 - 11:02 AM

Well I am using Strings in my book class:

public class Book {

	//variables
	private String Title;
	private String Author;
	private String Publisher;

	//constructor
	public Book(String Title, String Author, String Publisher) {
		this.Title = Title;
		this.Author = Author;
		this.Publisher = Publisher;
	}

	// setters and getters
	public void setBook(String Title, String Author, String Publisher) {
		this.Title = Title;
		this.Author = Author;
		this.Publisher = Publisher;
	}

	public String getTitle() {
		return Title;
	}

	public void setTitle(String Title) {
		this.Title = Title;
	}

	public String getAuthor() {
		return Author;
	}

	public void setAuthor(String Author) {
		this.Author = Author;
	}

	public String getPublisher() {
		return Publisher;
	}

	public void setPublisher(String Publisher) {
		this.Publisher = Publisher;
	}
}



So just to be CLEAR, you said for my Trilogy class, not to use Strings to define books, but instead use Books?

For example:

"private Book Book1;"

Also, I see that the method is missing from Trilogy, which I will fix, but I do not know the correct sytax for a method;

isn't it:

public static void Trilogy(Book Book1, Book Book2, Book Book3) {}

Or more along those lines, but am I using Trilogy or "Book" in the Method, because everytime I use Trilogy it says I can't use that as a name since that is a constructor.

This post has been edited by macosxnerd101: 20 September 2014 - 11:14 AM
Reason for edit:: Seriously- STOP quoting large posts

Was This Post Helpful? 0
  • +
  • -

#25 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Help with making a book class

Posted 20 September 2014 - 11:15 AM

Again, STOP quoting large posts, please.

Quote

Or more along those lines, but am I using Trilogy or "Book" in the Method, because everytime I use Trilogy it says I can't use that as a name since that is a constructor.

A constructor is a method. You need to look up the syntax for a constructor.
Was This Post Helpful? 0
  • +
  • -

#26 DroidsEatApples   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 17-September 14

Re: Help with making a book class

Posted 20 September 2014 - 11:25 AM

I have a constructor in my class, so this doesn't make sense to me. Maybe if you could explain a little better please.

Every class has to have a constructor and I have a constructor in my Book and in my Trilogy class so I don't understand why I am not defining it correctly.

Furtermore, I didn't understand your statement of do not use Strings and use Books and I was hoping to get some more clarification on that.

I know I don't understand every concept, but I am putting in the effort. I know that I am just a beginner, but it seems whenever I ask for help everyone is suddenly against me and to be quite honest, it discourages me a bit.
Was This Post Helpful? 0
  • +
  • -

#27 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Help with making a book class

Posted 20 September 2014 - 11:31 AM

Quote

The constructor Trilogy() is undefined

No Trilogy constructor accepting no parameters is defined.

Quote

Furtermore, I didn't understand your statement of do not use Strings and use Books and I was hoping to get some more clarification on that.

I explained this on the last page. A String is text. A Book is an object with a title, author, and publisher. Which makes more sense to use in your Trilogy class? Does a Trilogy have three pieces of text or three Books?

Quote

but it seems whenever I ask for help everyone is suddenly against me and to be quite honest, it discourages me a bit.

Nobody is against you here. I've spent the past two pages helping you.

Quote

I know I don't understand every concept, but I am putting in the effort.

I would strongly encourage you to spend some additional time with your course notes. I feel like a lot of the questions you are asking have been covered in course notes or your textbook somewhere. At the very least, I do strongly advocate again going to see your professor in office hours.
Was This Post Helpful? 0
  • +
  • -

#28 DroidsEatApples   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 17-September 14

Re: Help with making a book class

Posted 20 September 2014 - 11:57 AM

I don't have the opprotunity to meet with my professor, thats why I am on here. I will look at my constructor in my Trilogy class to see if I made a mistake somewhere and I will update accordingly. I appreciate your help, I am just frustrated because I go to the lectures and the professor is super vague about everything and teaches the class as if we have already been programming prior to class (the class is an entry level course with no pre reqs).

Furthermore, he isn't very accommodating. He works full as a programmer, and he only teaches part time. This semster he is only teaching one course and thus he is only on campus one day out of the week. His office hours are during periods where I am in another class so it is literally impossible for me to see him. I communicate with him regularally via e-mail, but it often takes him several days to respond. We have a few tutors, but they are on the scarce side.
Was This Post Helpful? 0
  • +
  • -

#29 DroidsEatApples   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 17-September 14

Re: Help with making a book class

Posted 20 September 2014 - 12:33 PM

Anyways, this is my constructor for the Trilogy class.

public Trilogy(Book Book1, Book Book2, Book Book3,
			Book trilogyName) {
		this.Book1 = Book1;
		this.Book2 = Book2;
		this.Book3 = Book3;
		this.trilogyName = trilogyName;



I am not seeing my mistake.
Was This Post Helpful? 0
  • +
  • -

#30 DroidsEatApples   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 17-September 14

Re: Help with making a book class

Posted 20 September 2014 - 01:14 PM

So I am on my last line of code, I have successfully removed all of the errors except I keep getting 1 error.

This line:

public void setName(String trilogyName) {
		this.trilogyName = trilogyName;



These are the errors I am geting.

What am I doing wrong? This is preventing me to pass all tests successfully.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3