Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem #4: (20 pts] A library includes a list of books and has many copies of every book. Assume having an array named bookstore. Each

image text in transcribed

Problem #4: (20 pts] A library includes a list of books and has many copies of every book. Assume having an array named bookstore. Each element in the array represents a book and contains the number of copies of that book (each book code is recognized by the array index). For example, if the array is A 500 13 123 0 15 17 A[0]=500 means the book with code 0 has 500 copies. A[1]=13 means the book with code 1 has 13 copies, and so on. a) Write a method boolean borrow (int[] A, int bookIndex) that takes an array of integers A and a book's code then decreases the number of copies of that book by 1 (if there are available copies). The method returns true if the book is successfully borrowed. If there are no available copies (i.e value is zero), the method returns false, b) Write a program in which you create an array A of size 6 and fill it with numbers of your choice (you can use the shorthand array initialization). Assume all inputs are positive (do NOT check them in your program) Your program that asks the user to enter the book code (a positive integer), then invokes the above method. Check sample run below. I For example, if the array is A (declared above) Sample run 1: Enter a book's code: 1 Borrow succeeded. The remaining number of copies is 12 Sample run 2: Enter a book's code: 10 The book does not exist Sample run 3 : Enter a book's code: 3 You can't borrow the book, there are no available copies. Problem #4: (20 pts] A library includes a list of books and has many copies of every book. Assume having an array named bookstore. Each element in the array represents a book and contains the number of copies of that book (each book code is recognized by the array index). For example, if the array is A 500 13 123 0 15 17 A[0]=500 means the book with code 0 has 500 copies. A[1]=13 means the book with code 1 has 13 copies, and so on. a) Write a method boolean borrow (int[] A, int bookIndex) that takes an array of integers A and a book's code then decreases the number of copies of that book by 1 (if there are available copies). The method returns true if the book is successfully borrowed. If there are no available copies (i.e value is zero), the method returns false, b) Write a program in which you create an array A of size 6 and fill it with numbers of your choice (you can use the shorthand array initialization). Assume all inputs are positive (do NOT check them in your program) Your program that asks the user to enter the book code (a positive integer), then invokes the above method. Check sample run below. I For example, if the array is A (declared above) Sample run 1: Enter a book's code: 1 Borrow succeeded. The remaining number of copies is 12 Sample run 2: Enter a book's code: 10 The book does not exist Sample run 3 : Enter a book's code: 3 You can't borrow the book, there are no available copies

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases In Action

Authors: Dave Bechberger, Josh Perryman

1st Edition

1617296376, 978-1617296376

More Books

Students also viewed these Databases questions