Create An Application Using Three Programs: The First Of The ✓ Solved

Create an application using three programs: The first of the three programs have been written for you. I will refer to this one as ClassA. You have to write the second one, which I will call ClassB. You also need to write a driver program which I will refer to as ClassDriver 2

This homework requires you to create an application consisting of three classes: ClassA (already provided), ClassB (which you will implement), and a driver program (which you will also implement). The ClassA is given as an existing class with specific attributes and methods. Your task is to create ClassB, which models a library book, and a driver program to test these classes by accepting user input for three library books, displaying each book's details after creation, and then showing the total inventory value of the library.

Sample Paper For Above instruction

Introduction

This assignment involves designing and implementing classes to model library books using Java programming language, following object-oriented principles. The core components include developing the LibraryBook class (ClassB) with proper constructors, accessors, mutators, and additional methods to encapsulate book details and inventory calculations. Additionally, a driver class is required to interactively create multiple book objects based on user input and to display relevant information. This comprehensive task helps reinforce understanding of class design, object interaction, and user input handling in Java.

Understanding the Provided Class (ClassA)

The author class (ClassA) is already provided with the following features:

  • A constructor that takes two string parameters: author’s name and type.
  • A toString() method that returns a string describing the author.

This class forms a component of the library book class, representing the author of each book.

Designing the LibraryBook Class (ClassB)

The LibraryBook class should encapsulate details about a library book, including ID, name, author object, price, and inventory total. The design includes the following attributes:

  • bookID: int, unique identifier for each book
  • bookName: String, title of the book
  • bookAuthor: Author object, encapsulating author information
  • bookPrice: double, price of the book
  • totalInvValue: static double, total inventory value across all books

The class requires constructors, accessor, mutator methods, and additional methods:

  • toString(): returns the name, author’s name, and price.
  • getTotValue(): static method, returns total inventory value.

Implementing the LibraryBook Class

The implementation involves the following points:

  1. Declare the attributes as private, including static totalInvValue.
  2. Constructors should initialize attributes and update totalInvValue by adding the book’s price.
  3. Accessors and mutators for each attribute, ensuring encapsulation.
  4. The toString() method creates a summary including book name, author, and price.
  5. The getTotValue() method returns totalInvValue.

Below is an example implementation:

public class LibraryBook {

private int bookID;

private String bookName;

private Author bookAuthor;

private double bookPrice;

private static double totalInvValue = 0;

public LibraryBook(int id, String name, Author author, double price) {

this.bookID = id;

this.bookName = name;

this.bookAuthor = author;

this.bookPrice = price;

totalInvValue += price;

}

public String toString() {

return "Book Name: " + bookName + "\nAuthor: " + bookAuthor.toString() + "\nPrice: " + bookPrice;

}

public static double getTotValue() {

return totalInvValue;

}

// Additional accessors and mutators omitted for brevity

}

Designing the Driver Program (ClassDriver)

This program facilitates interaction with users to input details for three books, create their objects, display their data, and finally show the total inventory value. The structure includes:

  1. Using Scanner for input collection.
  2. Looping three times to accept book details: name, author name and type, price, ID.
  3. Creating Author and LibraryBook objects accordingly.
  4. Printing each book’s details immediately after creation.
  5. After processing all three books, displaying the total inventory value using LibraryBook.getTotValue().

import java.util.*;

public class LibraryDemo {

public static void main(String[] args) {

Scanner kb = new Scanner(System.in);

String bName, aName, aType;

int bID;

double bPrice;

Author auth = null;

LibraryBook lBook = null;

for (int i = 1; i

System.out.print("Enter the name of the book: ");

bName = kb.nextLine();

System.out.print("Enter the name of the author: ");

aName = kb.nextLine();

System.out.print("Enter the type of books the author writes (Fiction/Non-Fiction): ");

aType = kb.nextLine();

System.out.print("Enter the price of the book: ");

bPrice = kb.nextDouble();

System.out.print("Enter the id of the book: ");

bID = kb.nextInt();

kb.nextLine(); // consume newline

auth = new Author(aName, aType);

lBook = new LibraryBook(bID, bName, auth, bPrice);

System.out.println("\n" + lBook);

System.out.println("The total inventory value after " + i + " books: " + LibraryBook.getTotValue());

System.out.println("\n");

}

kb.close();

}

}

Conclusion

This assignment encapsulates critical aspects of object-oriented programming such as class design, static attributes, object interaction, and user input handling in Java. It also illustrates the integration of provided classes with user-defined classes to model real-world entities efficiently. Proper implementation of these classes will demonstrate understanding of encapsulation, constructor chaining, static data management, and output formatting in Java.

References

  • deitel, P., & Deitel, H. (2014). Java: How to Program (10th ed.). Pearson.
  • Gaddis, T. (2018). Starting Out with Java: Early Objects (7th Edition). Pearson.
  • Horstmann, C. (2018). Core Java Volume I—Fundamentals (11th Edition). Pearson.
  • .Object Oriented Programming in Java | Oracle Documentation. Retrieved from https://docs.oracle.com/javase/tutorial/java/concepts/
  • Shield, M. (2019). Java Programming for Beginners. Technology Publishing.
  • Java Programming and Software Engineering Fundamentals | Coursera. Retrieved from https://www.coursera.org
  • Java API Documentation | Oracle. Retrieved from https://docs.oracle.com/en/java/javase/
  • Effective Java by Joshua Bloch, 3rd Edition. (2018). Addison-Wesley.
  • Java SE Documentation | Oracle. Retrieved from https://docs.oracle.com/javase/8/docs/api/
  • Effective Object-Oriented Design in Java | Journal of Software Engineering. (2020). Elsevier.