"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Brief Package Example

Brief Package Example

Published on 2024-11-08
Browse:603

Exemplo Breve de Pacote

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().

method

Code Example
Directory Structure:

src/
  bookpack/
    BookDemo.java

  1. Defining the Book Class within the bookpack Package (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; i 



Compilation and Execution

  1. Compiling the Code From the above bookpack directory, compile the file with:
javac bookpack/BookDemo.java

  1. Running the Program Run the program from the current working directory with:
java bookpack.BookDemo

Important Explanations:

  • Bookpack package: Both Book and BookDemo are part of the bookpack package. This means that to run the program, the package name must be specified.
  • Run Command: When running BookDemo, the full package name is required (bookpack.BookDemo). Running java BookDemo without specifying the package will not work.

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

Release Statement This article is reproduced at: https://dev.to/devsjavagirls/exemplo-breve-de-pacote-4khd?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

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