Секція 5. Розділ 2
single
Challenge: Basic Class Usage
Свайпніть щоб показати меню
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
Stringcalledtitle; - An
intcalledpages.
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.
Завдання
Swipe to start coding
Define a class named Book with the following requirements:
- Add two fields: a
Stringcalledtitleand anintcalledpages; - Implement a method named
printDetailsthat prints the book's title and number of pages in the format:Title: <title>, Pages: <pages>; - In the
mainmethod of theMainclass, create aBookobject, assign values to its fields (for example,"Java Basics"and200), and call itsprintDetailsmethod.
Use the provided code template for the Main class. Do not change the package declaration.
Рішення
Все було зрозуміло?
Дякуємо за ваш відгук!
Секція 5. Розділ 2
single
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат