COMP2243 - Programming and Problem Solving

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

Arrays ArrayProcessing.java BookRating.java BookRatingWithFileIO.java Book Rating OOP Program
Book.java
BookRatingClient.java
Array of Objects Program
City.java
CityClient.java




BookRating Program


Description

This program uses two arrays to store title and rating for 5 books, and uses several methods to do the following:

Fill in the arrays with user input data

Calculate the average rating

Find the the book with the highest rating (assume there is no tie)

Print all 5 books' title and rating

Associated Concepts:

Array

Algorithm:

main method:

Create two arrays to store 5 books information, title and rating

Array of String to store titles

Array of double to store ratings

Call methods to:

Populate the arrays with user input data

Print the array elements

Calculate the total rating

Find the index of the book with the highest rating and print the result

Print the average rating

populateArray method definition:

For i = 0 to last index of array cell

Input title and rating from the keyboard

Assign them to the array cells at i

printArray method definition:

For i = 0 to last index of array cell

Print array elements at i

sumArray method definition:

Declare variable sum and initialize it to 0

For i = 0 to last index of array cell

Add the current rating at i to sum

return sum

indexOfHighestRating method definition:

Declare variable maxIndex and assign it to 0 (Assume the first element is the highest)

For i = 1 to last index of array cell

If the current element at i is greater than the current highest element

    Update maxIndex to the current index i

return maxIndex

Source Code



Sample Run



Video