Stuck with Java Objected Orientated Programming - Array

Discussion in 'Java' started by Phalanx, Dec 10, 2008.

  1. Phalanx

    Phalanx

    Joined:
    Dec 10, 2008
    Messages:
    1
    Hey, I stumbled upon this forum on google and people seem to be very helpful, so I thought I'd post my problem (ethical or unethical?)

    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.

    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.

    Code:
    public class BookStore
    {
        Book[] stock;
        int NoOfBooks;
    
        public void initiliaseArray (int NoOfBooks)
        {
            NoOfBooks = NoOfBooks;
            stock = new Book[NoOfBooks];
        }
        
        public void addBook (Book)
        {
        }
    
    
    }
    
    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.
     
    Phalanx, Dec 10, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jure Sah
    Replies:
    21
    Views:
    958
  2. ben
    Replies:
    8
    Views:
    643
  3. alck1234@gmail.com

    need help in object orientated programming

    alck1234@gmail.com, Jul 10, 2008, in forum: C++
    Replies:
    4
    Views:
    347
    James Kanze
    Jul 11, 2008
  4. Allen Dardenelle

    Stuck on Page 27 of Programming Ruby

    Allen Dardenelle, Dec 31, 2005, in forum: Ruby
    Replies:
    5
    Views:
    117
    Allen Dardenelle
    Dec 31, 2005
  5. foldface@yahoo.co.uk

    Asynchronus calls and callbacks to Object orientated code

    foldface@yahoo.co.uk, Feb 12, 2004, in forum: Javascript
    Replies:
    3
    Views:
    145
    Michael Winter
    Feb 16, 2004
Loading...

Share This Page