Question:
Please help in JAVA programming, in an array problem!?
2006-11-05 10:49:16 UTC
Please help my, I need a program that has a class called shelf that is capable of holding a fixed number of books, in which:
Users can addABook, at a particular position, This should place a book at the specified position, losing any book which might have previously resided at the specified position.

Users should be able to getABook() by specifying the book's position. If no book is located at the specified position, the Bookshelf.BookNotFoundException should be thrown.

The user should be able to getAList() which should provide a nicely formatted String containing the titles of each Textbook. This listing should list only actual textbooks, not null references.

If ever the user specifies an index that is not valid, the IndexOutOfBoundsException should be thrown

Please...Please help me,
Thanks
Four answers:
2006-11-05 11:19:12 UTC
I'm going to assume you need to design a Textbook class to represent the books on the shelf.



You need to design the Shelf class like this:



public class Shelf

{

private Textbook[] books;



// constructor

public Shelf(int numBooks)

{

// initialize the books array with length=numBooks

books = new Textbook[numBooks];

}



// adds a book to the shelf at position p

public void addABook(Textbook tb, int p)

{

books[p] = tb;

}



// get a book from the shelf at position p

public Textbook getABook(int p)

{

if (books[p] == null)

throw new BookNotFoundException();

else

return books[p];

}



// get a list of all textbooks on the shelf

public String getAList()

{

// return string, starts out empty

String list = "";

// walk through books array

// add each book's title to the return String

for (int i = 0; i < books.length(); i++)

{

if(books[i] != null) // if this spot on shelf is not empty

list += books[i].getTitle() + "\n";

}

return list;

}

}



The IndexOutOfBoundsException is thrown automatically when an invalid array index is used, but you can explicitly throw the exception the same way I threw the BookNotFoundException.



You still need to design a Textbook class with a constructor that lets you enter its title and a getTitle() method that returns the title in a String. And you still need to define the BookNotFoundException, which I haven't done.



You might want to double check my syntax on the exception throwing; not sure if I did it entirely right. Good luck.
DiamandaG
2006-11-05 11:03:30 UTC
ok..well so far I have a D in CS1..so dont take my word for this...im sure its about 1% right...



public class Shelf {

public ArrayList locationCells;



public void setLocationCells(ArrayList book) {

locationCells = book ;

}



public Sting getABook()

eh..i dont know...



i really am sorry, lol, my lab grade is a 40..im confusing myself here, rly sory
2016-10-15 14:04:46 UTC
865ccb4ab0e063e5caa3387c1a8741mport java.text textile.Dec865ccb4ab0e063e5caa3387c1a8741malFormat; 865ccb4ab0e063e5caa3387c1a8741mport java.text textile.NumberFormat; NumberFormat formatter = new Dec865ccb4ab0e063e5caa3387c1a8741malFormat("000"); Str865ccb4ab0e063e5caa3387c1a8741ng quantity =formatter.format(rannum[i]); gadget.out.pr865ccb4ab0e063e5caa3387c1a8741nt( quantity +";");
Rizwan
2006-11-05 10:58:08 UTC
go and lay your hands on a java book or site and then you will be able to do it.

COOK YOUR OWN FOOD!!!!!!!!!!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...