Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Basic Class Usage | Introduction to Objects
Java Fundamentals: An Introductory Course
Osio 5. Luku 2
single

single

bookChallenge: Basic Class Usage

Pyyhkäise näyttääksesi valikon

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.

Tehtävä

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 5. Luku 2
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt