011-001-012

Defining and Using Constructors

Medium

Problem Description

Defining and Using Constructors

In this problem, you will create a program that defines a Book class with a constructor to initialize title, author, and pages fields, reads two books from standard input, and displays their information.

Learning Objective: Understand the role and definition of constructors

Overview

Constructors are special methods automatically called when objects are created, used for field initialization.

Specifications

  • Define Book class with title(String), author(String), pages(int) fields
  • Initialize all three fields through constructor
  • info() method displays book information
  • Read two books from standard input, create Book objects, and call info

Input Format

title1
author1
pages1
title2
author2
pages2

Output Format

title1 by author1 (pages1 pages)
title2 by author2 (pages2 pages)

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up