Results 1 to 14 of 14
  1. #1
    reeveliew is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default i need some tips on this question

    Introduction
    You have been requested to create an application for a bookshop that helps to keep track of books.

    Your Tasks:
    You are required to create the following classes for your application:
    1. The class to describe attributes of books (title, author, publisher and price). Provide relevant methods.
    2. The class should store collection of books (using an array). This class should also keep track of the total number of books in the collection, the total cost of the books in the collection, the average cost, the most expensive and cheapest book respectively
    3. There should be methods available to add books to the collection, remove books from the collection and output a report of the book collection (see the example of program output that follows at the end. Note how the output is formatted). Hint: Use tab to format the output.

    Program Requirements:
    You should implement inheritance, composition and arrays to store the book details. There is no need to implement any databases or files.


    Sample output

    Book Collection

    Number of Books : 4
    Total cost : $ 202.00
    Average cost : $ 50.50

    Book List:

    Title Author Publisher Price ($)

    Operating System Concepts Silberschatz Wiley 65.00

    SAMS Teach Yourself Perl Laura Lemay SAMS 95.00

    Programming in C Dr.P Sellapan Sejana 48.00

    Programming in Java John R. Hubbard McGraw-Hill 57.00


    Cheapest Book is:
    Programming in C Dr.P Sellapan Sejana 48.00

    Most Expensive Book is:
    SAMS Teach Yourself Perl Laura Lemay SAMS 95.00


    i am not asking for a complete code, but i hope you can help me in this assignment,i am still new in java and trying to ask some help from you guys.i need some hints or tips how to do this kind of question

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Now, the assignment is pretty self explanatory, it tells you excactely what classes to write up and what their fields and methods should be. I'll provide the backbone of the Book class without methods and constructors, just to get you started.
    Java Code:
    public class Book {
      private String title, author, publisher;
      private double price;
    }
    Try to write up the methods for this class. A tip, it's good practice for any class to override at least the toString() method, this should make the printAll method in the book collection class much easier. If you have any more questions, post them.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    reeveliew is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    may i ask how to set a input for this assignment?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Either read the input from a file or from the user.
    There are many sample programs here for both techniques.
    Look for FileReader and BufferedReader classes for from a file and the Scanner class for from a user at the keyboard.

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by reeveliew View Post
    i need some hints or tips how to do this kind of question

    here is some example code. the first class is Book which has almost all methods you wanted. this class has 2 constructors, the first parametrized with an int which creates the library with the given size. i used an array, because this was your requirement, but an arraylist is more convenient. the second constructor is for creating a book. a better design would be, if you store only books in the Book class and create a class called Library which holds all add, remove, print and so on methods. here is the code:

    Java Code:
    import java.util.Arrays;
    import java.util.List;
    import java.util.ArrayList;
    
    public class Book {
    	// This class should also keep track of the total number of books
    	public static int numberOfBooks = 0;
    	
    	private String title;
    	private String author;
    	private String publisher;
    	private double price;
    	Book[] library;
    	
    	public Book(int n) {
    	// The class should store collection of books (using an array).
    		library = new Book[n];     
    	}
    	
    	public Book(String t, String a, String p, double price) {
    		this.title = t;
    		this.author = a;
    		this.publisher = p;
    		this.price = price;
    	}
    	
    	public static int getNumberOfBooks() {
    		return numberOfBooks;
    	}
    
    	public String getTitle() {
    		return title;
    	}
    
    	public String getAuthor() {
    		return author;
    	}
    
    	public String getPublisher() {
    		return publisher;
    	}
    
    	public double getPrice() {
    		return price;
    	}
    
    	public Book[] getLibrary() {
    		return library;
    	}
    
    	public void addBook(Book book) {
    	// There should be methods available to add books to the collection
    		library[numberOfBooks] = book;
    		numberOfBooks++;
    	}
    	
    	public void removeBook(Book book) {
    		List<Book> list = new ArrayList<Book>(Arrays.asList(library));
    		list.remove(book);
    		library = list.toArray(library);
    		numberOfBooks--;
    
    	}
    	
    	public double totalCost() {
    	// the total cost of the books in the collection
    		double sum = 0;
    		for (int i = 0; i < Book.getNumberOfBooks(); i++) {
    			sum += library[i].getPrice();
    		}
    		return sum;
    	}
    	
    	// report of the book collection, for the moment with no tab
    	public void reportBooks() {
    		System.out.println("\n*** Total books in library: " + Book.numberOfBooks + " ***");
    		for (int i = 0; i < Book.getNumberOfBooks(); i++) {
    			System.out.println(library[i].getAuthor() + ","
    					+ library[i].getTitle() + ","
    					+ library[i].getPublisher()+ ","
    					+ library[i].getPrice());
    		}
    	}
    		
    	// TODO the average cost, 
    	// TODO the most expensive 
    	// TODO cheapest book respectively
    	
    }

    to run the code above i create a library and some books with this code:


    Java Code:
    public class BookExample {
    	public static void main(String[] args) {
    		// Create a store for 10 books
    		Book library = new Book(10);
    		// Create book b1
    		Book b1 = new Book("War and Peace", "Tolstoi", "Publisher 1", 60.80);
    		// add book b1
    		library.addBook(b1);
    		// Create book b2
    		Book b2 = new Book("Great Expectation", "Charles Dickens",
    				"Publisher 2", 73.20);
    		// add book b1
    		library.addBook(b2);
    
    		library.reportBooks();
    		System.out.println("\nTotal cost" + library.totalCost());
    
    		// remove Tolstoi
    		library.removeBook(b1);
    		// see what happens :)
    		library.reportBooks();
    
    		System.out.println("\nTotal cost: " + library.totalCost());
    	}
    }

    the output don't look like you want but i think you can format it. hope, this code give you some hints. have fun.

  6. #6
    reeveliew is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    i will test the code and try to understand how it work. by the way if i meet any problem can i drop you a mail?

  7. #7
    reeveliew is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    dude, i saw 2 book in the library, but the output only show one, how to solve it?

  8. #8
    reeveliew is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    need some help with those most expensive, cheapest and average of price

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    need some help with those most expensive, cheapest and average of price
    Cheapest will be the one with the lowest price. Get the price of the first one and compare against the next. If cheaper save it to compare against the next until done.
    Same for expensive.
    You must know how to to an average: Total/count

  10. #10
    reeveliew is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    ya i have those code but i got no idea how to put in there..

    System.out.println("Average Cost : RM. " + Average);
    for(int i=0; i < Price.length; i++){
    if (Price[i] > max){
    max = Price[i];
    tExp = Title[i];
    aExp = Author[i];
    pExp = Publisher[i];
    }
    if (Price[i] < min){
    min = Price[i];
    tCheap = Title[i];
    aCheap = Author[i];
    pCheap = Publisher[i];
    }

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    This is how you would do it manually:
    Take a piece of paper and draw a square on it to hold the lowest price.
    Put the price of the first item in the square.
    Get the next item.
    compare the price of that item with the price in the square
    If the price is lower, replace the value in the square with the current price.
    If more items loop back to the "Get the..." step
    When done with looking at all the items, the lowest price is in the square.

  12. #12
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by reeveliew View Post
    ya i have those code but i got no idea how to put in there..

    here is a small example how to implement min and max. you can take the code and put it in separate methods.


    Java Code:
    	public void bookStats() {
    		// initialize the cheapest book with element 0
    		Book min = library[0];
    		// initialize the most expensive book with element 0
    		Book max = library[0];
    		double sum = 0;
    		
    		for (int i = 0; i < Book.getNumberOfBooks(); i++) {
    			// the total cost of the books in the collection
    			sum += library[i].getPrice();
    			// find cheapest book
    			if (library[i].getPrice() < min.getPrice()) {
    				min = library[i];
    			}
    			// find mostexpensive
    			if (library[i].getPrice() > max.getPrice()) {
    				max = library[i];
    			}
    		}
    		// print the sum, min and max to the console
    		System.out.println("\n*** Statics of your library ***");
    		System.out.println("The total cost of all book are: " + sum);
    		System.out.println("The cheapest book in the library is " + min.getTitle() 
    				+ " from " + min.getAuthor() + " with a price of " + min.getPrice());
    		System.out.println("The most expensive book in the library is " + max.getTitle() 
    				+ " from " + max.getAuthor() + " with a price of " + max.getPrice());	
    	}

    and here is the output i got with my simulated 3 books

    *** Total books in library: 3 ***
    Tolstoi,War and Peace,Publisher 1,60.8
    Charles Dickens,Great Expectation,Publisher 2,73.2
    Josua Bloch,Effective Java (2nd Edition),Sun,100.75

    *** Statics of your library ***
    The total cost of all book are: 234.75
    The cheapest book in the library is War and Peace from Tolstoi with a price of 60.8
    The most expensive book in the library is Effective Java (2nd Edition) from Joshua Bloch with a price of 100.75

  13. #13
    reeveliew is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    i think i done already, wait for the final test result, thx for all the help

  14. #14
    reeveliew is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Java Newbie Overall Tips
    By Hossein in forum New To Java
    Replies: 4
    Last Post: 04-05-2010, 11:32 PM
  2. Tips needed
    By adz06 in forum New To Java
    Replies: 5
    Last Post: 12-07-2009, 02:35 AM
  3. need some derby tips:
    By kulangotski in forum Advanced Java
    Replies: 4
    Last Post: 03-09-2009, 06:09 PM
  4. Need some tips.
    By PVL268 in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 03:37 PM
  5. First project, need some tips..
    By Komala_aradhya in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 02:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •