Question

In: Computer Science

Challenge: Dog Description: Create a Dog class that contains specified properties and methods. Create an instance...

Challenge: Dog

Description: Create a Dog class that contains specified properties and methods. Create an instance of Dog and use its methods.

Purpose: This application provides experience with creating classes and instances of objects in C#.

Requirements:

Project Name: Dog
Target Platform: Console
Programming Language: C#

Documentation:

  • Types and variables (Links to an external site.) (Microsoft)
  • Classes and objects (Links to an external site.) (Microsoft)
  • Enums (Links to an external site.) (Microsoft)

Create a class called Dog.

Dog is to have the following public properties:

  • name (of type string)
  • owner (of type string)
  • age (of type int)
  • gender (of type Gender)

Gender is a enum type you need to create that contains Male and Female.

Create a constructor (Links to an external site.) in the class to initialize all of the properties when creating an instance.

Create a method (function in the class) called Bark that takes one parameter that is the number of times to bark and prints “Woof!” that many times to the Console.

Create a method called GetTag that takes no parameters and returns a string. The method returns “If lost, call [owner]. [‘Her’|‘His’] name is [name] and [‘she’|‘he’] is [age] [‘year’|‘years’] old.”

[owner] means replace with the name of the owner.

[‘Her’|‘His’] means choose to display “Her” or “His” based on the gender of the Dog.

[name] means replace with the name of the Dog.

[‘she’|‘he’] means choose to display “she” or “he” based on the gender of the Dog.

[‘year’|‘years’] means to choose to display “year” or “years” depending on the age of the Dog. If age is 1, use “year”. If any other age, use “years”.

In the Main function of the application, test that instances of Dog can be created and that the methods work. In the following, “Orion” is the name of the Dog instance, “Shawn” is the owner, 1 is the age, and Gender.Male is the gender.

Dog puppy = Dog("Orion", "Shawn", 1, Gender.Male);  // create object instance
puppy.Bark(3); // output: Woof!Woof!Woof!
Console.WriteLine(puppy.GetTag()); // output: If lost, call Shawn. His name is Orion and he is 1 year old.

Dog myDog = Dog("Lileu", "Dale", 4, Gender.Female);  // create object instance
myDog.Bark(1); // output: Woof!
Console.WriteLine(myDog.GetTag()); // output: If lost, call Dale. Her name is Lileu and she is 4 years old.

Submission

You are to submit a zip file of the project directory for the application you create. The entire project is to be submitted. Find the project folder that holds the Visual Studio project and zip it.

When you finish can you send pictures of the code with spacing

Solutions

Expert Solution

Screenshot

Program

using System;
namespace DogTest
{
    //Create an enum Gender
    public enum Gender { Male,Female};
    //Create a class Dog
    class Dog
    {
        //Instance variables
        private string name;
        private string owner;
        private int age;
        private Gender gender;
        //Constructor
        public Dog(string name,string owner,int age,Gender gender)
        {
            this.name = name;
            this.owner = owner;
            this.age = age;
            this.gender = gender;
        }
        //Method bark used to print the bark sound
        public void Bark(int times)
        {
            for(int i = 0; i < times; i++)
            {
                Console.Write("Woof!");
            }
            Console.WriteLine();
        }
        //Method to get a dog went missing, means tag generator for dog
        public string GetTag()
        {
            string tag = "If lost, call "+owner+". ";
            if (gender == 0)
            {
                tag += "His name is " + name + " he is " + age.ToString();
            }
            else
            {
                tag += "Her name is " + name + " she is " + age.ToString();
            }
            if (age > 1)
            {
                tag += " years old.";
            }
            else
            {
                tag += " year old.";
            }
            return tag;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //Class Dog Test
            Dog puppy = new Dog("Orion", "Shawn", 1, Gender.Male); // create object instance
            puppy.Bark(3); // output: Woof!Woof!Woof!
            Console.WriteLine(puppy.GetTag()); // output: If lost, call Shawn. His name is Orion and he is 1 year old.

            Dog myDog =new Dog("Lileu", "Dale", 4, Gender.Female); // create object instance
            myDog.Bark(1); // output: Woof!
            Console.WriteLine(myDog.GetTag()); // output: If lost, call Dale. Her name is Lileu and she is 4 years old.
        }
    }
}

----------------------------------------------------------------------------------------------------------

Output

Woof!Woof!Woof!
If lost, call Shawn. His name is Orion he is 1 year old.
Woof!
If lost, call Dale. Her name is Lileu she is 4 years old.


Related Solutions

Challenge: Documents Description: Create a class in Python 3 named Document that has specified attributes and...
Challenge: Documents Description: Create a class in Python 3 named Document that has specified attributes and methods for holding the information for a document and write a program to test the class. Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3. Requirements: Write a class in Python 3 named Document that has the following attributes and methods and is saved in the file Document.py. Attributes __title is a...
Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.”
Programming Problem 2 - Cycle[A] Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.” Create a constructor with two parameters, using the same variable names in the parameter list. Assign each variable to numberOfWheels” and “weight” respectively. Write a separate application to test the class and display its properties. Note: Do not change the names of the instance variables or the variables listed in the constructor’s parameter list.[B] Edit your class Cycle by...
Using C# Create a class called Artist that contains 4 pieces of information as instance variables:...
Using C# Create a class called Artist that contains 4 pieces of information as instance variables: name (datatype string), specialization – i.e., music, pottery, literature, paintings (datatype string), number of art items (datatype int), country of birth (datatype string). Create a constructor that initializes the 4 instance variables. The Artist class must contain a property for each instance variable with get and set accessors. The property for number should verify that the Artist contributions is greater than zero. If a...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
The Hole Class Description: For the Hole class, there are three instance variables: the par for...
The Hole Class Description: For the Hole class, there are three instance variables: the par for the hole, the length of the hole in yards, and a Boolean valued indicator of whether the hole is one in which the players’ full drives will be measured for distance. John clarifies: The length of the hole in yards determines par for the hole. The current standard is: No more than 250 yards Par 3 251 to 470 yards Par 4 471 to...
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
CS 209 Data Structure 3. a. Create a class named Point3D that contains 3 instance variables...
CS 209 Data Structure 3. a. Create a class named Point3D that contains 3 instance variables x, y, and z. b. Create a constructor that sets the variables. Also, create get and set methods for each variable. c. Create a toString() method. d. Make Point3D implement Comparable. Also, create a compareTo(Point3D other) method that compares based on the x-coordinate, then y-coordinate for tiebreakers, then z-coordinate for tiebreakers. For example, (1, 2, 5) comes before (2, 1, 4), which comes before...
1. Describe the difference between instance methods and class methods in Java and give an example...
1. Describe the difference between instance methods and class methods in Java and give an example of each. 2. A class variable is visible to and shared by all instances of a class. How would such a variable be used in an application? 3. Describe the difference between abstract classes and concrete classes, giving an example of each. 4. Explain how data are encapsulated and information is hidden in Java? 5. Explain the difference between a class and an interface...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
Create a class called Sphere. The class will contain the following    Instance data double radius...
Create a class called Sphere. The class will contain the following    Instance data double radius Methods Constructor with one parameter which will be used to set the radius instance data Getter and Setter for radius             Area - calculate the area of the sphere (4 * PI * radius * radius)             Volume - calculate and return the volume of the sphere (4/3 * PIE * radius * radius * radius) toString - returns a string with the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT