Results 1 to 3 of 3
  1. #1
    Phalanx is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default Stuck with Java Object Orientated Programming - Arrays

    Hi there, I've been going through my coursework and I just can't get my head around using Arrays in a class. I'll post the question followed by the code. I've been using BlueJ to do all this.

    1. Create a new class called BookStore. Your shop will have two fields, an array storing a collection of books (Book[] anyname) and an integer for the number of books in the collection.

    Add a constructor which initialises the field variables appropriately. (Here you should construct an empty array object, where you can determine the size).


    The question after this is:

    2. Write a method addBook that takes a Book object as parameter and puts it in the next free space in the collection. What should you do with the field variable for the number of existing books?

    Well, I think I've done 1. fine, but I'm not sure about two, I just can't get my head around "Book" as an object. I understand the last part, I should reduce the number of existing books.

    I'll post my code for the Bookstore class and the Book class.

    Java Code:
    import java.util.*;
    
    public class BookStore
    {
        private List<Book> books;
        private int Storagespace;
        
        public void Bookstore(int size)
        {
            books = new ArrayList<Book>();
            Storagespace = size;
        }
        
        public void addBook(Book newBook)
        {
            if(books.size() == Storagespace)
            {
                System.out.println("Storage has reached maximum capacity :(");
            }
            else
            {
            books.add(newBook);
            }
        }
        
        public int numberOfBooks()
        {
            return books.size();
        }
        
        public void printBooks()
        {
            for (Book book : books)
            {
                book.bookcheck();
            }
        }
        
        //public void printoldBooks()
        //{
            
    
    }
    Java Code:
    public class Book
    {
        
    private String author;
    private String title;
    private int year;
    private double price;
    private double pricespecial = 0;
    
        public Book()
        { }
        
    
        public void getauthor(String authorhold)
        {
            author = authorhold;
        }
        
        public void gettitle(String titlehold)
        {
            title = titlehold;
        }
        
        public int getyear(int yearhold)
        {
            year = yearhold;
            return year;
        }
        
        public double getprice(double pricehold)
        {
           price = pricehold;
           return price; 
        }
        
        public void reduceprice (double percentage)
        {
            pricespecial = Math.round(price * percentage);   
        }
        
        public void lettercount ()
        {
            if (title == null)
            return;
            
            int lettercount = 0;
            
            for (int i = 0; i < title.length(); i++)
            {
            
                if (Character.isLetter(title.charAt(i)))
                    lettercount++;
                    
            }
            
            System.out.println("There are " + lettercount + " letters in the title");
        
            
            
           
        }
        
        public void bookcheck()
        {
            if (pricespecial > 0)
            {
            System.out.println("########################");
            System.out.println("# Title: " + title);
            System.out.println("# Author: " +author);
            System.out.println("# Published: " + year);
            System.out.println("# was  " + price);
            System.out.println("# now  " + pricespecial);
            System.out.println("########################");
            }
            
            else
            {
            System.out.println("########################");
            System.out.println("# Title: " + title);
            System.out.println("# Author: " +author);
            System.out.println("# Published: " + year);
            System.out.println("#  " + price);
            System.out.println("########################");   
            }
        }
        
    }
    Thanks.

    [edit] Been fiddling a bit, is this looking a bit better?
    Last edited by Phalanx; 12-10-2008 at 05:00 PM.

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default You're not using an array!

    One thing that may be confusing you is that you're not actually using an array -- instead, you've chosen to use an ArrayList. Now actually, in real life, an ArrayList is a preferable solution to the problem, but it's not what your assignment was asking you to do.

    If you do use an array, you have the issue that an array is a fixed size, so you need an extra variable to keep track of how many books you've put into the array so far -- that's the "field variable for the number of existing books".

    Another thing to think about is what happens when your book store is full. At the moment, you're just printing a message to the screen. But what use is that to the program? The part of the program that calls addBook() may need to know whether or not adding the book succeeded. How are you going to signal to the calling part of the program, then, whether or not it succeeded?

  3. #3
    Phalanx is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by neilcoffey View Post
    One thing that may be confusing you is that you're not actually using an array -- instead, you've chosen to use an ArrayList. Now actually, in real life, an ArrayList is a preferable solution to the problem, but it's not what your assignment was asking you to do.

    If you do use an array, you have the issue that an array is a fixed size, so you need an extra variable to keep track of how many books you've put into the array so far -- that's the "field variable for the number of existing books".

    Another thing to think about is what happens when your book store is full. At the moment, you're just printing a message to the screen. But what use is that to the program? The part of the program that calls addBook() may need to know whether or not adding the book succeeded. How are you going to signal to the calling part of the program, then, whether or not it succeeded?
    Thanks for the reply, I figured ArrayList wasn't a very reliable solution to my problem. I'm not exactly sure how I'm going to implement an actual array in this case.

    How's this?

    Java Code:
    import java.util.*;
    
    public class BookStore
    {
        private int i = 0;
        private Book[] collection;
        private int Storagespace;
        private int limit;
        
        public void Bookstore(int Storagespace)
        {
            limit = Storagespace;
            collection = new Book[Storagespace];
        }
        
        public void addBook(Book newBook)
        { 
           collection[i] = newBook;
           i++;   
        }
        
        public void print()
        {
            for (i = 0; i < limit; i++)
            {
            collection[i].bookcheck();
            }
        }
        
        public void printold()
        {
            for (i = 0; i < limit; i++)
            {
                if (collection[i].isbookold == true)
                {
                collection[i].bookcheck();
                }   
            }
        }
    }
    Done more fiddling, seems to be working alright at the moment. How's this looking so far?
    Last edited by Phalanx; 12-10-2008 at 06:12 PM.

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 03:12 AM
  2. Replies: 2
    Last Post: 12-09-2008, 06:02 PM
  3. Java Programming in Netbeans
    By cserenop51 in forum NetBeans
    Replies: 5
    Last Post: 11-05-2008, 04:21 AM
  4. Java networking programming (I)
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 12-24-2007, 08:21 PM
  5. Java Programming
    By JavaForums in forum Java Tutorial
    Replies: 0
    Last Post: 07-29-2007, 12:10 AM

Posting Permissions

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