0% found this document useful (0 votes)
24 views

LabSheet#2 - Arrays - of - Objects

The document describes a Book class with attributes like title, publisher, and price. It provides getter and setter methods to manipulate these attributes. It also has a printInfo() method to display book details. It then presents two exercises: 1) Write a method to read details of multiple books into a Book array and return the number of books. Print the details of each book using printInfo(). 2) Add a method to calculate the average price of books in a Book array. Test it by reading book details using the method from Ex1 and printing the average price.

Uploaded by

Layan Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

LabSheet#2 - Arrays - of - Objects

The document describes a Book class with attributes like title, publisher, and price. It provides getter and setter methods to manipulate these attributes. It also has a printInfo() method to display book details. It then presents two exercises: 1) Write a method to read details of multiple books into a Book array and return the number of books. Print the details of each book using printInfo(). 2) Add a method to calculate the average price of books in a Book array. Test it by reading book details using the method from Ex1 and printing the average price.

Uploaded by

Layan Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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