011-003-010

Using Constructor Chaining

Hard

Problem Description

Using Constructor Chaining

In this problem, you will create a program that implements a Book class with constructor chaining using this(), reads values from standard input, instantiates it three ways, and displays the results to standard output.

Learning Objective: Understand design patterns for chaining multiple constructors

Overview

Implement constructor chaining in a class with three constructors.

Specifications

  • Define three constructors in Book class
  • Book() : title "Unknown", author "Unknown", pages 0
  • Book(String title) : author "Unknown", pages 0
  • Book(String title, String author, int pages) : all specified
  • Each constructor calls the one with more arguments

Input Format

title1 (title for book2)
title2 (title for book3)
author (author for book3)
pages (page count for book3)

Output Format

Unknown by Unknown (0 pages)
<title1> by Unknown (0 pages)
<title2> by <author> (<pages> 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