I am new here and didn't do well on a problem in school and was wondering what i did wrong!

PROBLEM: Create a Book class that stores book information. Your Book class will store the following:

•book title
•author’s first name
•author’s last name
•book ISBN-10 number
•publisher
•price
Create a test application that creates several objects of type Book class and adds each book to an ArrayList. Your test application should then do the following:

•Iterate through all books in the ArrayList and print the information out in readable form
•Print the total number of books stored
•Prompt you for an index and print the book information stored in the index


CODE /w errors (can't figure out why they error):

import java.util.*;

public class book

{ public String title;
  public String authorname;
  public String authorlast;
  public String ISBN;
  public String publisher;
  public String price;
  public static int count = 0; //number books created

  //initial book, add 1 to static count

 public book (String first, String last, String ISBN, String publisher, String price)


   {
      title = title;
      authorname = authorname;
      authorlast = authorlast;
      ISBN = ISBN;
      publisher = publisher;
      price = price;
      ++ count;
      system.out.printf( "%s, %s Title: %s Price: %s ISBN: %s Publisher: %s; count = %d\n", authorname, authorlast, title, price, ISBN, publisher, count );

   }

public String toString() {
      {
        return String.format( "%s, %s Title: %s Price: %s ISBN: %s Publisher: %s count", authorname, authorlast, title, price, ISBN, publisher, count );
      }
}


public class booktest
{
   public static void main (String[] args)
   {
      System.out.printf( "books before instantiation: %d\n", book.getCount() );

      ArrayList <book> bookArrayList = new ArrayList <book> ();

      book book1 = new book("Frog", Erik, Anderson, "221667691169", "Leapfrog productions", "69.69"
      book book2 = new book("Frog2", Erik, Anderson, "221667691170", "Leapfrog productions", "68.68"
      book book3 = new book("Frog3", Erik, Anderson, "221667691171", "Leapfrog productions", "77.77"

      System.out.println();

      for(book book : bookArrayList)
         System.out.println( book.title + " " +
         System.out.println( book.authorname + " " +
         System.out.println( book.authorlast + " " +
         System.out.println( book.ISBN + " " +
         System.out.println( book.publisher + " " +
         System.out.println( book.price + " " +
            book.getCount() );
         
         System.out.println();


         System.out.println("Total number of books = " + bookArrayList.size());
   }

I am missing something i just don't know what as i am new to this!
Yes the assignment was due Feb 27, 2011 thats why i am asking now because i didn't do well on it! Also any help or assistance be appreciated!

Also any inforation on a good compiler embeded into a ide that helps with syntax errors for java and can also has a GUI builder? Prefer freeware!

I run win7 64bit

If there isn't one any tips on what ide with embedded compiler and syntax checker and a seperate GUI builder?

I am having alot of trouble with syntax and this week is GUI building!

Member Avatar for ztini
public book (String first, String last, String ISBN, String publisher, String price)
 
 
   {
      title = title;
      authorname = authorname;
      authorlast = authorlast;
      ISBN = ISBN;
      publisher = publisher;
      price = price;
      ++ count;
      system.out.printf( "%s, %s Title: %s Price: %s ISBN: %s Publisher: %s; count = %d\n", authorname, authorlast, title, price, ISBN, publisher, count );

1) In constructor and methods alike, the parameter variables have precedence over your global variables, such as: public String title;

So when you do, title = title

You are saying parameter.title = parameter.title and not storing it in your global variable.

If you're going to use the same variable name for the global and the parameter, you need to tell the compiler to use "this" object.

public book (String first, String last, String ISBN, String publisher, String price) {
      this.title = title;
      this.authorname = first;
      this.authorlast = last;
      this.ISBN = ISBN;
      this.publisher = publisher;
      this.price = price;
}

2) On naming conventions:

Classes begin with uppercase letters and are mixed case: LightSwitchControl BookTest
Methods begin with lowercase and are mixed case as well: turnLightOn(), toString()
Variables also being with lowercase and are mixed case: boolean isTurnedOn, String authorName ;

3) Global variables should almost always be private. Otherwise, I can access them and change them outside your program. If you need to provide access to a global variable, such as title, you should implement two methods:

public String getTitle() { }
public void setTitle(String title) { }

4) In your test method, you need to add the books you created to your book arrayList.

Book b1 = new Book(...);
ArrayList<Book> books = new ArrayList<Book>();
books.add(b1);

Note: with naming conventions, you can call lists/groups of objects by their plural form, such as "books" in this case. This is the only time you should ever use plural.

5) As far as compilers, I suggest BlueJay for starting out. It doesn't have all the bells and whistles of full blown IDE's like Eclipse, but unlike notepad, it can compile directly inside application without all that annoying command line stuff in the dos prompt.

ok was remapping i all out!

import java.util.*;

public class book

{ public String title;
  public String authorname;
  public String authorlast;
  public String ISBN;
  public String publisher;
  public String price;
  public static int count = 0; //number books created

  //initial book, add 1 to static count

 public book (String first, String last, String ISBN, String publisher, String price)


   {
      title = title;
      authorname = authorname;
      authorlast = authorlast;
      ISBN = ISBN;
      publisher = publisher;
      price = price;
      ++ count;
      system.out.printf( "%s, %s Title: %s Price: %s ISBN: %s Publisher: %s; count = %d\n", authorname, authorlast, title, price, ISBN, publisher, count );

   }

public String toString() {
      {
        return String.format( "%s, %s Title: %s Price: %s ISBN: %s Publisher: %s count", authorname, authorlast, title, price, ISBN, publisher, count );
      }
}


public class booktest
{
   public static void main (String[] args)
   {
      System.out.printf( "books before instantiation: %d\n", book.getCount() );

      ArrayList<book> bookArrayList = new ArrayList<book> ();

      book book1 = new book("Frog", "Erik", "Anderson", "221667691169", "Leapfrog productions", 69.69) ;
      book book2 = new book("Frog2", "Erik", "Anderson", "221667691170", "Leapfrog productions", 68.68) ;
      book book3 = new book("Frog3", "Erik", "Anderson", "221667691171", "Leapfrog productions", 77.77) ;

      bookArrayList.add(book1);
      bookArrayList.add(book2);
      bookArrayList.add(book3);

      System.out.println( );

      for(book book : bookArrayList)
         System.out.println( book.title + " " +
                             book.authorname + " " +
                             book.authorlast + " " +
                             book.ISBN + " " +
                             book.publisher + " " +
                             book.price + "\n");

         System.out.println( );


         System.out.println("Total number of books = " + bookArrayList.size());

   }

is now where i am at! I noticed on on few parts you mentioned that i didn't do yet! Is that the reason i am getting the errors at the end?

public book (String first, String last, String ISBN, String publisher, String price) {      this.title = title;      this.authorname = first;      this.authorlast = last;      this.ISBN = ISBN;      this.publisher = publisher;      this.price = price;

Is this reason i get the errors at the end? so if i change it to this.* will i get the count?
Also you mentioned to upper and lowercase would this be example of wht you mean

this.authorLast

is above what you mean?

Member Avatar for ztini

You need to add a parameter for title in your constructor and reread my previous post.

ok after further editing and adding everything down to one strange error!
the very end and this is what i have

import java.util.*;

public class book

{ public String title;
  public String authorname;
  public String authorlast;
  public String ISBN;
  public String publisher;
  public String price;
  public static int count = 0; //number books created

  //initial book, add 1 to static count

 public book (String first, String last, String ISBNnumber, String pub, String p)


   {
      title = title;
      authorname = first;
      authorlast = last;
      ISBN = ISBNnumber;
      publisher = pub;
      price = p;
      ++ count;
      system.out.printf( "%s, %s Title: %s Price: %s ISBN: %s Publisher: %s; count = %d\n", authorname, authorlast, title, price, ISBN, publisher, count );

   }

public String toString() {
      {
        return String.format( "%s, %s Title: %s Price: %s ISBN: %s Publisher: %s count", authorname, authorlast, title, price, ISBN, publisher, count );
      }
}


public class booktest
{
   public static void main (String[] args)
   {
      System.out.printf( "books before instantiation: %d\n", book.getCount() );

      ArrayList<book> bookArrayList = new ArrayList<book> ();

      book book1 = new book("Frog", "Erik", "Anderson", "221667691169", "Leapfrog productions", 69.69) ;
      book book2 = new book("Frog2", "Erik", "Anderson", "221667691170", "Leapfrog productions", 68.68) ;
      book book3 = new book("Frog3", "Erik", "Anderson", "221667691171", "Leapfrog productions", 77.77) ;

      bookArrayList.add(book1);
      bookArrayList.add(book2);
      bookArrayList.add(book3);

      System.out.println( );

      for(book book : bookArrayList)
         System.out.println( book.title + " " +
                             book.authorname + " " +
                             book.authorlast + " " +
                             book.ISBN + " " +
                             book.publisher + " " +
                             book.price + "\n");

         System.out.println( );


         System.out.println("Total number of books = " + bookArrayList.size());

   }

so now to fullfill the requirements of this project what am i missing and how can i get this to compile! As i am down to 1 error

Member Avatar for ztini

Line 26, System should be capitalized.

[B]System[/B].out.printf( "%s, %s Title: %s Price: %s ISBN: %s Publisher: %s; count = %d\n", authorname, authorlast, title, price, ISBN, publisher, count );

1) You should remove the static count from the book class.

The Book class is singular, one instance of it literally represents 1 book. One book does not know how many other books there are; only containers like book shelves, libraries, and arrayList know this information. Therefore, it should reside in your booktest class. The good news is that you don't need to keep track of it anyway...arrayList has a method size() that will tell you how many books you have.

2) You still need to add "title" as a parameter to your constructor and update the assignments to use "this."

Your Book class probably needs to look like this:

public class Book {

	public String title, author, ISBN, publisher, price;

	public Book(
			String title, 
			String author, 
			String ISBN, 
			String publisher, 
			String price) {
		
		this.title = title;
		this.author = author;
		this.ISBN = ISBN;
		this.publisher = publisher;
		this.price = price;
	}

	public String toString() {
		return String.format(
				"Title: %s Author: %s ISBN: %s Publisher: %s Price: %s", 
				title, author, ISBN, publisher, price);
      }
}

However, one issue. How does this scale? What if you want to add a String to represent ISBN-10? You would have to change your constructor and all your methods. Oy, that would suck, right?

What if your code was adaptable and efficient to boot?

import java.util.HashMap;
import java.util.Map.Entry;


public class Book {
	
	private HashMap<String, String> details = 
		new HashMap<String, String>();
	
	public Book(
			String title,
			String author,
			String ISBN,
			String publisher,
			String price) {
		
		details.put("Title", title);
		details.put("Author", author);
		details.put("ISBN", ISBN);
		details.put("Publisher", publisher);
		details.put("Price", price);
	}
	
	@Override
	public String toString() {	
		String line = "";
		for (Entry<String, String> detail : details.entrySet())
			line = detail.getKey() + ": " + 
				detail.getValue() + '\n' + line;
		return line;
	}
	
	public String getDetail(String detail) {
		if (details.keySet().contains(detail))
			return details.get(detail);
		return null;
	}
	
}

Now you can all the different details about a book you want to your program without much headache; the only place you would need to update is the constructor.

1) You should remove the static count from the book class.

The Book class is singular, one instance of it literally represents 1 book. One book does not know how many other books there are; only containers like book shelves, libraries, and arrayList know this information. Therefore, it should reside in your booktest class.

Not everybody will agree with this particular piece of advice. Yes, if you have a Library class this is where the info about how many & which books should reside. If you haven't then it's a common practice to have a static variable or two to hold info about the class as a whole - eg how many/which instances have been created. There isn't a definite right or wrong here.
On the other hand, I definitely wouldn't expect this application data to be defined in a "test" class.
So, best advice: create a Library class. Second best advice, have a static ArrayList in the Book class and each instance to it in the constructor.

my wrong, mistake


Add...

@ JamesCherrill

maybe not mistake, your post wasn't complete infos, I'm missing here 2.nd coins side, just my view

<OT>I'm against local static variable, shows about not good and correct code conceptions and knowledges how (f.e.) Static Variable works,
</OT>

and there is boomerang returns, side effect, if/when newbee should declared int myCount = new int(static int variable);

I did say "not everyone will agree"!
It's my opinion and experience that static variables can be useful, and that the designers of Java put them is for good reasons; it's not automatically a mistake or bad design to use them. However, I don't think we should hijack this thread to debate the point. Let's agree to differ and let the the OP get on with his homework!

agreed final :-)

Yes, i am still updateing but as time goes i also have to continue school! So i am still updateing this assignment i have done thats why it isn't closed! But also trying to also learn GUI system and how to get this Nimbus to work or how to even initalize it in Java. yes we went from this assignment here above to building a GUI.
Also I like the Hashmap set up you put up and how it would scale with little edits, but I haven't learned that yet! Any referential web address I could get familar with that at? I nice learning reference on that would be nice!

I think @ztini was just having a little joke when he posted that HashMap idea.
It looks simple, but it breaks just about every principle behind the design of Java. There are good reasons why you won't see that in real-life systems, nor are you likely ever to be taught it.
You will do a lot better to stick to the standard approach of instance variables and getters/setters.

Please no more jokes! I am litterly new at this! My course is a advanced programming course and we cover alot of stuff in 5 weeks! IE. All of Java in 5 weeks! I sleep little and take alot (mostly everything)of what you guys/women say sereously!

I am new to this and have no programming experience before this course or very little! Any extra help on this would be great!! I like references to material and anything you could possibly think of to help a new Java programmer! The book we are using is Java How to Program (Eighth Edition) by Deitel and Deitel I like this book very much and I go to the Deitel web page also for informative information!
But thank you all for the help on learning this as i ask these questions after i turn in the assignment! I don't cheat and I am a perfectionist so I like to know why I have doe certain things wrong!

Final note on this programming application is it best to have it broke in to two java applications 1 for the actual and 1 for the test portion?

As the book i am reading from shows the actual program then it starts on line 1 for the test program? If this is the case would both files have to be in the same folder or how does that work?

Very short answers:
Yes, best to have your program (in one or more classes) with a separate class that runs tests from "outside" the program. When you build and ship the final program you can leave your test class(es) out. Google "JUnit" - a standard way of defining running those test classes in an automated way.
"both files in the same folder" will work, but for the full answer Google "Java packages".
J.

OK BACK AT IT

import java.util.*;

public class book

{ public String title;
  public String authorname;
  public String authorlast;
  public String ISBN;
  public String publisher;
  public String price;
  public static int count = 0; //number books created

  //initial book, add 1 to static count

 public book (String first, String last, String ISBN, String publisher, String price)


   {
      this.title = title;
      this.authorname = authorname;
      this.authorlast = authorlast;
      this.ISBN = ISBN;
      this.publisher = publisher;
      this.price = price;
      ++ count;
      System.out.printf( "%s, %s Title: %s Price: %s ISBN: %s Publisher: %s; count = %d\n", authorname, authorlast, title, price, ISBN, publisher, count );

   }

public String toString() {
      {
        return String.format( "%s, %s Title: %s Price: %s ISBN: %s Publisher: %s count", authorname, authorlast, title, price, ISBN, publisher, count );
      }
}


public class booktest
{
   public static void main (String[] args)
   {
      System.out.printf( "books before instantiation: %d\n", book.getCount() );

      ArrayList<book> bookArrayList = new ArrayList<book> ();

      book book1 = new book("Frog", "Erik", "Anderson", "221667691169", "Leapfrog productions", 69.69) ;
      book book2 = new book("Frog2", "Erik", "Anderson", "221667691170", "Leapfrog productions", 68.68) ;
      book book3 = new book("Frog3", "Erik", "Anderson", "221667691171", "Leapfrog productions", 77.77) ;

      bookArrayList.add(book1);
      bookArrayList.add(book2);
      bookArrayList.add(book3);

      System.out.println( );

      for(book book : bookArrayList)
         System.out.println( book.title + " " +
                             book.authorname + " " +
                             book.authorlast + " " +
                             book.ISBN + " " +
                             book.publisher + " " +
                             book.price + "\n");

         System.out.println( );


         System.out.println("Total number of books = " + bookArrayList.size());


   }

ERROR IS
C:\Users\Ghost\Desktop\BIT-Programming\ITPR423\Unit3\book.java:70: reached end of file while parsing
}

Member Avatar for ztini

Add another "}" after your toString method to close the class Book.

Better to delete the duplicated { on line 31.
also
new book(...) calls don't match the constructor's signature
looks like another missing } at the end of class booktest
If you prefer to omit the{} around a single-statement for loop, its especially important get the indentation of the subsequent lines right (eg lines 63/66 v. confusing.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.