Time To Code
Get hands-on practice by coding a few programming tasks.
Task I: Total number of books by an author
Our task is to find the total number of books written by specific authors.
✏️ Note: Below is the same code we saw in the previous lesson to read the dataset as objects of book class. Write the piece of code to answer the questions below.
It takes the name of an author and dataset as input and returns the total number of books written by the author.
Input: J.K. Rowling
Expected output:
Total number of books by J.K. Rowling: 6
Just for reference, Author
is the second column in the dataset
.
import java.util.ArrayList;public class driver {// Write your code here}}
If you’re unsure how to do this, click the “Show Hint” button.
Task II: All the authors in the dataset
Our task is to find the names of all the authors present in the dataset. Remember: many records may have the same author, so don’t consider the same author multiple times.
Expected output: List name of all authors in the dataset.
Just for reference, Author
is the second column in the dataset
.
public class Book {private String title;private String author;private String userRating;private String reviews;private String price;private String year;private String genre;// Constructorpublic Book(String title, String author, String userRating, String reviews, String price, String year, String genre) {this.title = title;this.author = author;this.userRating = userRating;this.reviews = reviews;this.price = price;this.year = year;this.genre = genre;}// Getterspublic String getTitle() {return title;}public String getAuthor() {return author;}public String getUserRating() {return userRating;}public String getReviews() {return reviews;}public String getPrice() {return price;}public String getYear() {return year;}public String getGenre() {return genre;}// Setterspublic void setTitle(String title) {this.title = title;}public void setAuthor(String author) {this.author = author;}public void setUserRating(String userRating) {this.userRating = userRating;}public void setReviews(String reviews) {this.reviews = reviews;}public void setPrice(String price) {this.price = price;}public void setYear(String year) {this.year = year;}public void setGenre(String genre) {this.genre = genre;}}