Results 1 to 3 of 3
- 12-10-2008, 03:13 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 3
- Rep Power
- 0
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() //{ }Thanks.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("########################"); } } }
[edit] Been fiddling a bit, is this looking a bit better?Last edited by Phalanx; 12-10-2008 at 04:00 PM.
- 12-10-2008, 04:20 PM #2
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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?Neil Coffey
Javamex - Java tutorials and performance info
- 12-10-2008, 04:40 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 3
- Rep Power
- 0
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?
Done more fiddling, seems to be working alright at the moment. How's this looking so far?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(); } } } }Last edited by Phalanx; 12-10-2008 at 05:12 PM.
Similar Threads
-
Operator < cannot be applied to java.lang.Object, Object
By Albert in forum Advanced JavaReplies: 2Last Post: 11-26-2010, 02:12 AM -
[SOLVED] Is it bad programming style to just typecast an object?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 12-09-2008, 05:02 PM -
Java Programming in Netbeans
By cserenop51 in forum NetBeansReplies: 5Last Post: 11-05-2008, 03:21 AM -
Java networking programming (I)
By Java Tutorial in forum Java TutorialReplies: 0Last Post: 12-24-2007, 07:21 PM -
Java Programming
By JavaForums in forum Java TutorialReplies: 0Last Post: 07-28-2007, 11:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks