Bookpack:
The example creates a package called bookpack, which contains a simple class for managing a database of books.
Book Class:
It has private attributes title, author and pubDate (title, author and publication date).
Constructor method initializes attributes.
show() method displays the book details.
BookDemo Class:
Creates an array of 5 Book objects.
Populates the array with book information and displays the details using the show().
Code Example
Directory Structure:
src/ bookpack/ BookDemo.java
// Demonstração breve dos pacotes. package bookpack; class Book { private String title; private String author; private int pubDate; // Construtor Book(String t, String a, int d) { title = t; author = a; pubDate = d; } // Método para exibir os detalhes do livro void show() { System.out.println(title); System.out.println(author); System.out.println(pubDate); System.out.println(); } } // Classe para demonstrar o uso de Book class BookDemo { public static void main(String args[]) { Book books[] = new Book[5]; // Cria uma matriz de objetos Book // Preenche a matriz com diferentes livros books[0] = new Book("Java: A Beginner's Guide", "Schildt", 2014); books[1] = new Book("Java: The Complete Reference", "Schildt", 2014); books[2] = new Book("The Art of Java", "Schildt and Holmes", 2003); books[3] = new Book("Red Storm Rising", "Clancy", 1986); books[4] = new Book("On the Road", "Kerouac", 1955); // Exibe os detalhes de cada livro for (int i = 0; iCompilation and Execution
- Compiling the Code From the above bookpack directory, compile the file with:
javac bookpack/BookDemo.java
- Running the Program Run the program from the current working directory with:
java bookpack.BookDemoImportant Explanations:
Expected Output:
Java: A Beginner's Guide Schildt 2014 Java: The Complete Reference Schildt 2014 The Art of Java Schildt and Holmes 2003 Red Storm Rising Clancy 1986 On the Road Kerouac 1955
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3