You are on page 1of 1

INT209 Data Structures

Lab Sheet #2
Week Starting Monday, February 6, 2023
Arrays of Objects
Consider the following Book class:
public class Book {
private String bookTitle;
private String bookPublisher;
private double bookPrice;
public Book(){
bookTitle = "";
bookPublisher = "";
bookPrice = 0;
}
public void setBookInfo(String title, String publisher, double price){
bookTitle = title;
bookPublisher = publisher;
bookPrice = price;
}
public void setBookTitle(String title) {
bookTitle = title;
}
public void setBookPublisher(String publisher){
bookPublisher = publisher;
}
public void setBookPrice(double price){
bookPrice = price;
}
public void printInfo(){
System.out.println("Title: " + bookTitle);
System.out.println("Publisher: " + bookPublisher);
System.out.printf("Price: $%.2f%n", bookPrice);
}
public String getBookTitle() {
return bookTitle;
}
public String getBookPublisher() {
return bookTitle;
}
public double getBookPrice() {
return bookPrice;
}
}// End of class bookEX#1

EX#1
Write a program with a method (not part of the Book class) that takes one-dimensional array of type Book and
reads the values of the data members for a number of books and store them in the array. The method should return
the number of books read. Your program should then display the information read for each book using the
printInfo() method of the Book class.

EX#2
Add to the program another method that takes a one-dimensional array of type Book and returns the average price
of all books. Test your method by reading data for a number of Books using the method of EX1 and print the
average price.

You might also like