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
- Define
String namefield inDogclass - Accept the name via constructor
bark()method prints<name> says Woof!- In main, use
Scannerto read 2 names, create 2Dogobjects, 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