Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Basic Class Usage | Introduction to Objects
/
Java Fundamentals: An Introductory Course
Sektion 5. Kapitel 2
single

single

bookChallenge: Basic Class Usage

Stryg for at vise menuen

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.

Opgave

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.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 5. Kapitel 2
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt