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

single

bookChallenge: Basic Class Usage

Svep för att visa menyn

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.

Uppgift

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 desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 2
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt