Solution: Build a Book Class

  • public class Book – defines a class named Book.

  • String title; String author; int pages; – declare fields (variables) to store a book’s title, author, and number of pages.

  • public static void main(String[] args) – main method where program execution starts.

  • Book myBook = new Book(); – creates a new Book object named myBook.

  • myBook.title = "The Great Gatsby"; – assigns a value to the title field.

  • myBook.author = "F. Scott Fitzgerald"; – assigns a value to the author field.

  • myBook.pages = 180; – assigns a value to the pages field.

Solution: Build a Book Class

  • public class Book – defines a class named Book.

  • String title; String author; int pages; – declare fields (variables) to store a book’s title, author, and number of pages.

  • public static void main(String[] args) – main method where program execution starts.

  • Book myBook = new Book(); – creates a new Book object named myBook.

  • myBook.title = "The Great Gatsby"; – assigns a value to the title field.

  • myBook.author = "F. Scott Fitzgerald"; – assigns a value to the author field.

  • myBook.pages = 180; – assigns a value to the pages field.