008-002-012

Calling Object Methods

Easy

Problem Description

Calling Object Methods

In this problem, you will create a program that reads two dog names from standard input, instantiates Dog objects, and calls their bark() method to display each dog's sound to standard output.

Learning Objective: Understand the basics of creating objects and calling their methods

Overview

Define a Dog class with a bark method, read names using Scanner, create objects, and call the method.

Specifications

  1. Define String name field in Dog class
  2. Accept the name via constructor
  3. bark() method prints <name> says Woof!
  4. In main, use Scanner to read 2 names, create 2 Dog objects, and call bark()

Input Format

dog name 1
dog name 2

Output Format

<name1> says Woof!
<name2> says Woof!

Sample I/O

Input:

Max
Buddy

Output:

Max says Woof!
Buddy says Woof!

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