Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Week 4 - Assignment 4 Assignment 4 Due by Thursday before Midnight, end of Week

ID: 3634705 • Letter: W

Question

Week 4 - Assignment 4



Assignment 4
Due by Thursday before Midnight, end of Week 4 (20% of grade)

Required reading/research
Chapter 6 - User-Defined Methods and Classes
Classes and Objects (pr 440)
Instructions
Create a class called Bookcase that can keep track of books placed on different shelves of a bookcase. Use the Bookcase class code below as a starting point and write the code to complete each method.

When you are finished, use the Driver code below to test your Bookcase class and make sure it produces the following output:

Shelf #1 contains 3 books.
Shelf #2 contains 6 books.
Shelf #3 contains 3 books.
The total number of books is: 12

Bookcase class
public class Bookcase {
protected int numShelves = 4; // number of shelves in the bookcase
protected int[] booksPerShelf; // array to hold the books per shelf

// allocate 'booksPerShelf' array
Bookcase()
{
}

// set 'numShelves' and allocate 'booksPerShelf' array
Bookcase(int num)
{
}

// add 'numBooks' to shelf 'shelfNum'
void AddBook(int shelfNum, int numBooks)
{
}

// remove 'numBooks' from shelf 'shelfNum'
void RemoveBook(int shelfNum, int numBooks)
{
}

// get the number of books on a specific shelf
int GetNumBooks(int shelfNum)
{
}

// Return the total number of books in the bookcase
int TotalBooks()
{
}
}

Driver class
public class Driver {

public static void main(String[] args) {

int size = 3;

Bookcase b = new Bookcase(size);

b.AddBook(0, 3);
b.AddBook(0, 1);
b.AddBook(1, 6);
b.AddBook(1, 2);
b.AddBook(1, 3);
b.AddBook(2, 5);

b.RemoveBook(0, 1);
b.RemoveBook(1, 5);
b.RemoveBook(2, 2);

for (int idx = 0; idx < size; idx++)
System.out.println("Shelf #" + (idx + 1) + " contains " + b.GetNumBooks(idx) + " books.");

System.out.println("The total number of books is: " + b.TotalBooks());
}
}

Explanation / Answer

public class Bookcase { protected int numShelves; // number of shelves in the bookcase protected int[] booksPerShelf; // array to hold the books per shelf // allocate 'booksPerShelf' array Bookcase() { } // set 'numShelves' and allocate 'booksPerShelf' array Bookcase(int num) { booksPerShelf = new int[num]; numShelves = num; } // add 'numBooks' to shelf 'shelfNum' void AddBook(int shelfNum, int numBooks) { int num = booksPerShelf[shelfNum] + numBooks; booksPerShelf[shelfNum] = num; } // remove 'numBooks' from shelf 'shelfNum' void RemoveBook(int shelfNum, int numBooks) { int num = booksPerShelf[shelfNum] - numBooks; booksPerShelf[shelfNum] = num; } // get the number of books on a specific shelf int GetNumBooks(int shelfNum) { return booksPerShelf[shelfNum]; } // Return the total number of books in the bookcase int TotalBooks() { int total = 0; for (int i =0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote