Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Basic Class Usage | Introduction to Objects
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Java Fundamentals: An Introductory Course
SectionΒ 5. ChapterΒ 2
single

single

bookChallenge: Basic Class Usage

Swipe to show menu

In this challenge, you will practice creating your own class and using it in a Java program. Your goal is to define a class named Book with two fields:

  • A String called title;
  • An int called pages.

Add a method called printDetails that prints the book's title and number of pages. Then, in the main method, create a Book object, set its fields, and call the printDetails method to display the information.

Task

Swipe to start coding

Define a class named Book with the following requirements:

  • Add two fields: a String called title and an int called pages;
  • Implement a method named printDetails that prints the book's title and number of pages in the format: Title: <title>, Pages: <pages>;
  • In the main method of the Main class, create a Book object, assign values to its fields (for example, "Java Basics" and 200), and call its printDetails method.

Use the provided code template for the Main class. Do not change the package declaration.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 2
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt