Here is the class MyBookShelfTester public class MyBookShelfTester { public static void main(String[] args) { MyBookShelf bookShelf = new MyBookShelf(); Book book1 = new Book("Diaspora", "Greg Egan"); Book book2 = new Book("What if?", "Randall Munroe"); Book book3 = new Book("Foundation", "Isaac Asimov"); Book book4 = new Book("Solaris", "Stanislaw Lem"); Book book5 = new Book("The Hitchhiker's Guide to the Galaxy", "Douglas Adams"); bookShelf.addCount(book1); bookShelf.addBook(book2); bookShelf.addBook(book3); bookShelf.addBook(book4); bookShelf.addBook(book5); book1.addRating(5); book1.addRating(5); book1.addRating(4); book1.addRating(4); book5.addRating(5); book5.addRating(5); book5.addRating(4); book4.addRating(5); book4.addRating(5); book1.checkOut(); book2.checkOut(); book3.checkOut(); book4.checkOut(); book5.checkOut(); book1.returnToLibrary(); book2.returnToLibrary(); book3.returnToLibrary(); book4.returnToLibrary(); book5.returnToLibrary(); Book diaspora = bookShelf.getBookByTitle("Diaspora"); Book hitchhiker = bookShelf.getBookByTitle("The Hitchhiker's Guide to the Galaxy"); System.out.println("Average Rating for the book #" + book1.indexOf(diaspora) + ": " + diaspora.getAverageRating()); System.out.println("Average Rating for the book #" + book.indexOf(hitchhiker) + ": " + hitchhiker.getAverageRating()); if (diaspora.getAverageRating() == hitchhiker.getAverageRating()) { System.out.println("Diaspora and The Hitchhiker's Guide to the Galaxy have the same rating which is " + diaspora.getAverageRating() + "."); } else { System.out.println("Diaspora and The Hitchhiker's Guide to the Galaxy have different ratings, " + diaspora.getAverageRating() + " and " + hitchhiker.getAverageRating() + ", respectively."); } } } Here is the class Book: public class Book { private String title; private String author; private Boolean checkedOut = false; private double averageRating; private int ratingSum = 0; private int ratingCount = 0; /** * Sets the title. * @param String iTitle * title of the book * @return none */ public void setTitle(String iTitle) { title = iTitle; } /** * Gets the title. * @param none * @return title of the book */ public String getTitle() { return title; } /** * Sets author * @param String iAuthor * book author * @return none */ public void setAuthor(String iAuthor){ author = iAuthor; } /** * Gets the author. * @param none * @return String the author of the book */ public String getAuthor(){ return author; } /** * Adds a rating. * * @param rating * The user rating for this book. */ public void addRating(int rating) { ratingSum = rating + ratingSum; ratingCount = ratingCount + 1; averageRating = ratingSum / ratingCount; } /** * Gets a rating. */ public double getRating() { return averageRating; } /** * Sets book checked out */ public void checkOut() { checkedOut = true; } /** * Sets book not checked out. */ public void returnToLibrary() { checkedOut = false; } /** * Gets checkedOut value */ public Boolean isCheckedOut() { return checkedOut; } } Here is the class MyBookShelf: public class MyBookShelf { private Book[] catalog = new Book[10]; private int bookCount = 0; public void addBook(String title, String author) { if (bookCount < 10) { catalog[bookCount] = new Book(); bookCount++; } else { System.out.println("Sorry, the shelf is full"); } } public void checkOutBook(int index) { if (index >= 0 && index < bookCount) { catalog[index].checkOut(); } else { System.out.println("Invalid index"); } } public void returnBook(int index) { if (index >= 0 && index < bookCount) { catalog[index].returnToLibrary(); } else { System.out.println("Invalid index"); } } public void listInventory() { System.out.println("Book Inventory:"); for (int i = 0; i < bookCount; i++) { System.out.println(i + ". " + catalog[i].getTitle() + " by " + catalog[i].getAuthor()); } } } Here is the class ArrayShiftMulti: public class ArrayShiftMult { public int[] shiftOne(int[] inArray) { int[] outArray = new int[inArray.length]; outArray[0] = -1; System.arraycopy(inArray, 0, outArray, 1, inArray.length - 1); return outArray; } public int[] mult(int[] array1, int[] array2) { int minLen = Math.min(array1.length, array2.length); int[] longArray = array1.length > array2.length ? array1 : array2; int[] outArray = new int[longArray.length]; for (int i = 0; i < minLen; i++) { outArray[i] = array1[i] * array2[i]; } System.arraycopy(longArray, minLen, outArray, minLen, longArray.length - minLen); return outArray; } public static void main(String[] args) { ArrayShiftMult asm = new ArrayShiftMult(); int[] array1 = {4,7,21,3,4,5,18,4,6,2}; int[] array2 = {13,10,1,3,6,9}; int[] shiftedArray = asm.shiftOne(array2); for (int num : shiftedArray) { System.out.print(num + " "); } System.out.println(); int[] multipliedArray = asm.mult(array1, array2); for (int num : multipliedArray) { System.out.print(num + " "); } System.out.println(); } } I am getting syntax errors in the MyBookSheffTester class and cannot figure out how to fix it. It is linked with the Book class and MyBookShelf class as well.
Here is the class MyBookShelfTester
public class MyBookShelfTester {
public static void main(String[] args) {
MyBookShelf bookShelf = new MyBookShelf();
Book book1 = new Book("Diaspora", "Greg Egan");
Book book2 = new Book("What if?", "Randall Munroe");
Book book3 = new Book("Foundation", "Isaac Asimov");
Book book4 = new Book("Solaris", "Stanislaw Lem");
Book book5 = new Book("The Hitchhiker's Guide to the Galaxy", "Douglas Adams");
bookShelf.addCount(book1);
bookShelf.addBook(book2);
bookShelf.addBook(book3);
bookShelf.addBook(book4);
bookShelf.addBook(book5);
book1.addRating(5);
book1.addRating(5);
book1.addRating(4);
book1.addRating(4);
book5.addRating(5);
book5.addRating(5);
book5.addRating(4);
book4.addRating(5);
book4.addRating(5);
book1.checkOut();
book2.checkOut();
book3.checkOut();
book4.checkOut();
book5.checkOut();
book1.returnToLibrary();
book2.returnToLibrary();
book3.returnToLibrary();
book4.returnToLibrary();
book5.returnToLibrary();
Book diaspora = bookShelf.getBookByTitle("Diaspora");
Book hitchhiker = bookShelf.getBookByTitle("The Hitchhiker's Guide to the Galaxy");
System.out.println("Average Rating for the book #" + book1.indexOf(diaspora) + ": " + diaspora.getAverageRating());
System.out.println("Average Rating for the book #" + book.indexOf(hitchhiker) + ": " + hitchhiker.getAverageRating());
if (diaspora.getAverageRating() == hitchhiker.getAverageRating()) {
System.out.println("Diaspora and The Hitchhiker's Guide to the Galaxy have the same rating which is " + diaspora.getAverageRating() + ".");
} else {
System.out.println("Diaspora and The Hitchhiker's Guide to the Galaxy have different ratings, " + diaspora.getAverageRating() + " and " + hitchhiker.getAverageRating() + ", respectively.");
}
}
}
Here is the class Book:
public class Book {
private String title;
private String author;
private Boolean checkedOut = false;
private double averageRating;
private int ratingSum = 0;
private int ratingCount = 0;
/**
* Sets the title.
* @param String iTitle
* title of the book
* @return none
*/
public void setTitle(String iTitle) {
title = iTitle;
}
/**
* Gets the title.
* @param none
* @return title of the book
*/
public String getTitle() {
return title;
}
/**
* Sets author
* @param String iAuthor
* book author
* @return none
*/
public void setAuthor(String iAuthor){
author = iAuthor;
}
/**
* Gets the author.
* @param none
* @return String the author of the book
*/
public String getAuthor(){
return author;
}
/**
* Adds a rating.
*
* @param rating
* The user rating for this book.
*/
public void addRating(int rating) {
ratingSum = rating + ratingSum;
ratingCount = ratingCount + 1;
averageRating = ratingSum / ratingCount;
}
/**
* Gets a rating.
*/
public double getRating() {
return averageRating;
}
/**
* Sets book checked out
*/
public void checkOut() {
checkedOut = true;
}
/**
* Sets book not checked out.
*/
public void returnToLibrary() {
checkedOut = false;
}
/**
* Gets checkedOut value
*/
public Boolean isCheckedOut() {
return checkedOut;
}
}
Here is the class MyBookShelf:
public class MyBookShelf {
private Book[] catalog = new Book[10];
private int bookCount = 0;
public void addBook(String title, String author) {
if (bookCount < 10) {
catalog[bookCount] = new Book();
bookCount++;
} else {
System.out.println("Sorry, the shelf is full");
}
}
public void checkOutBook(int index) {
if (index >= 0 && index < bookCount) {
catalog[index].checkOut();
} else {
System.out.println("Invalid index");
}
}
public void returnBook(int index) {
if (index >= 0 && index < bookCount) {
catalog[index].returnToLibrary();
} else {
System.out.println("Invalid index");
}
}
public void listInventory() {
System.out.println("Book Inventory:");
for (int i = 0; i < bookCount; i++) {
System.out.println(i + ". " + catalog[i].getTitle() + " by " + catalog[i].getAuthor());
}
}
}
Here is the class ArrayShiftMulti:
public class ArrayShiftMult {
public int[] shiftOne(int[] inArray) {
int[] outArray = new int[inArray.length];
outArray[0] = -1;
System.arraycopy(inArray, 0, outArray, 1, inArray.length - 1);
return outArray;
}
public int[] mult(int[] array1, int[] array2) {
int minLen = Math.min(array1.length, array2.length);
int[] longArray = array1.length > array2.length ? array1 : array2;
int[] outArray = new int[longArray.length];
for (int i = 0; i < minLen; i++) {
outArray[i] = array1[i] * array2[i];
}
System.arraycopy(longArray, minLen, outArray, minLen, longArray.length - minLen);
return outArray;
}
public static void main(String[] args) {
ArrayShiftMult asm = new ArrayShiftMult();
int[] array1 = {4,7,21,3,4,5,18,4,6,2};
int[] array2 = {13,10,1,3,6,9};
int[] shiftedArray = asm.shiftOne(array2);
for (int num : shiftedArray) {
System.out.print(num + " ");
}
System.out.println();
int[] multipliedArray = asm.mult(array1, array2);
for (int num : multipliedArray) {
System.out.print(num + " ");
}
System.out.println();
}
}
I am getting syntax errors in the MyBookSheffTester class and cannot figure out how to fix it. It is linked with the Book class and MyBookShelf class as well.
Step by step
Solved in 5 steps with 7 images