007-003-005

Using Classes: Dog Class

Easy

Problem Description

Using Classes: Dog Class

In this problem, you will design and implement a Dog class according to the given specification.

Learning Objective: Learn how to define fields, constructors, and methods to create objects from a class

Specification

Create a Dog class that satisfies the following requirements:

  1. Field: String name (the dog's name)
  2. Constructor: Dog(String name) — receives and stores the name
  3. Methods:
    • String getName() — returns the dog's name
    • void bark() — prints "Woof!" to standard output on one line

Usage Example

Dog d = new Dog("Buddy");
System.out.println(d.getName()); // Buddy
d.bark();                         // 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