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

single

bookChallenge: Basic Class Usage

Scorri per mostrare il 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.

Compito

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.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 5. Capitolo 2
single

single

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

some-alt