Question

Write a program in C++ that places a dog into a pen, which can hold up...

Write a program in C++ that places a dog into a pen, which can hold up to four dogs, each time a key (i.e. Enter ) is pressed. Each Dog is an object instantiated from class Dog defined in file Dog.h. The Pen is an object instantiated from class Pen defined in file Pen.h. Each Dog object, when instantiated, randomly chooses a fur color (i.e.” black”, “brown” or “white”), an eye color (“brown”, “black”, or “blue”), breed (Labrador, German Shepherd, Poodle, Boxer) and weight (between 10-110 lbs.).

Provide get and set member functions for these attributes. Each member function should be defined in Dog.cpp. Assign the random attribute values in the Dog Constructor functions. Each Pen object should store an array to hold five Dogs . Add member functions to the Pen class to add or remove a Dog object to/from the Pen along with a check member function to check the following:

• If the number of “brown” dogs exceeds the number of “black” dogs, then the black dogs will all hide.

• If there exist more dogs weighing more than 40 lbs. than dogs less or equal to 40 lbs., then the small dogs will ALL bark. Sample Output: A 70 lbs. brown German Shepherd dog with blue eyes has been added to the pen. Press a key to add the next dog. A 35 lbs. white Poodle dog with brown eyes has been added to the pen. Press a key to add the next dog. A 50 lbs. black Boxer with black eyes has been added to the pen. The 35 lbs. white poodle dog with brown eyes is barking. Press a key to add the next dog. A 100 lbs. brown Labrador with brown eyes has been added to the pen. The 50 lbs. black Boxer with black eyes is hiding The 35 lbs. white poodle dog with brown eyes is barking.

Note: The check function will also generate the messages. Call the check function each time a dog is added to the pen. NO POINTERS SHOULD BE USED IN THIS PROGRAM.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Dog.h
#ifndef __DOG_H
#define __DOG_H
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

string FUR_COLORS[3] = {"black","brown","white"};
string EYE_COLORS[3] = {"brown","black","blue"};
string BREEDS[4] = {"labrador", "german shepherd", "poodle", "boxer"};
class Dog
{
   private:
       string furCol;
       string eyeCol;
       string breed;
       float weight;
      
   public:
      
       Dog();
       void setFurCol(string);
       string getFurCol();
       void setEyeCol(string);
       string getEyeCol();
       void setBreed(string);
       string getBreed();
       void setWeight(float);
       float getWeight();
       void printDog();
};

#endif

//Dog.cpp
#include "Dog.h"

int getRand(int max)
{
   return rand() % max;
}
Dog::Dog()
{
   srand(time(NULL));
   this->furCol = FUR_COLORS[getRand(3)];
   this->eyeCol = EYE_COLORS[getRand(3)];
   this->breed = BREEDS[getRand(4)];
   this->weight = getRand(100);
}
string Dog::getBreed()
{
   return this->breed;
}
string Dog::getFurCol()
{
   return this->furCol;
}
string Dog:: getEyeCol()
{
   return this->eyeCol;
}
float Dog:: getWeight()
{
   return this->weight;
}

void Dog:: setBreed(string br)
{
   this->breed = br;
}

void Dog:: setEyeCol(string eyeCol)
{
   this->eyeCol = eyeCol;
}


void Dog:: setFurCol(string furCol)
{
   this->furCol = furCol;
}


void Dog:: setWeight(float weight)
{
   this->weight = weight;
}


void Dog:: printDog()
{
   cout<<weight<<" lbs. "<<furCol<<" "<<breed<<" with "<<eyeCol<<" eyes";
}
//Pen.h
#ifndef __PEN_H
#define __PEN_H
#include "Dog.cpp"
class Pen
{
   private:
       Dog dogs[5];
       int pos;
       int size;
   public:
      
       Pen()
       {
           pos = 0;
           size = 5;
       }
       void addDog(string furCol,string eyeCol,string breed,float weight)
       {
           //cout<<furCol<<" "<<eyeCol<<" "<<breed<<" "<<weight<<endl;
           if(valid(FUR_COLORS,furCol,3) && valid(EYE_COLORS,eyeCol,3) && valid(BREEDS,breed,4))
           {
               if(weight >=10 && weight <=100)
               {
                   if(pos < size)
                   {
                       Dog dog;
                       //set dog properties.
                       dog.setFurCol(furCol);
                       dog.setEyeCol(eyeCol);
                       dog.setBreed(breed);
                       dog.setWeight(weight);
                       dogs[pos] = dog;
                       cout<<"\nA ";
                       dogs[pos].printDog();
                       cout<<" is added to the Pen\n";
                       pos++;
                       check();
                   }
                   else
                   {
                       cout<<"\nInsertion Limit Exceeded\n";
                   }
                  
               }
               else
               {
                   cout<<"\nInvalid Weight passed\n";
               }
           }
           else
           {
               cout<<"\nInvalid arguments passed\n";
           }
       }
       //remove dog based on fur color and eyeColor
       void removeDog(string furCol)
       {
           int ind = getInd(furCol);
           if(ind == -1)
           {
               cout<<"\nNo Dog found with Fur Color: "<<furCol<<endl;
           }
           else
           {
               for(int i = ind + 1;i<pos;i++)
               {
                   dogs[i - 1] = dogs[i];
               }
               pos--;
               check();
           }
       }
       void check()
       {
           cout<<"\n";
           int browns =0;
           int blacks = 0;
           int less40 = 0;
           int great40 = 0;
          
           getDogsByColor(browns,blacks);
           getDogsByWeight(less40,great40);
          
          
           if(browns > blacks)
           {
               printDogsByColor(true);
           }
           /*else if(browns < blacks)
           {
               printDogsByColor(false);
           }*/
          
           if(great40 > less40)
           {
               printDogsByWeight(true);
           }
           else if(great40 < less40)
           {
               printDogsByWeight(false);
           }
           cout<<"\n";
       }
       void getDogsByColor(int &brown,int &black)
       {
          
           string furCol;
          
           for(int i = 0;i<pos;i++)
           {
              
               furCol = lower(dogs[i].getFurCol());
              
               if(furCol.compare("black") ==0)
               {
                   black++;
               }
               else if(furCol.compare("brown") == 0)
               {
                   brown++;
               }
           }
       }
       void getDogsByWeight(int &less40,int &great40)
       {
           for(int i = 0;i<pos;i++)
           {
               if(dogs[i].getWeight() <=40)
               {
                   less40 ++;
               }
               else
               {
                   great40++;
               }
           }
       }
       int getInd(string furCol)
       {
           furCol = lower(furCol);
           for(int i = 0;i<pos;i++)
           {
               if(furCol.compare(dogs[i].getFurCol()) == 0)
               {
                   return i;
               }
           }
           return -1;
       }
       bool valid(string array[],string find,int len)
       {
           string col;
           find = lower(find);
           for(int i = 0;i<len;i++)
           {
               col = lower(array[i]);
               if(find.compare(col) == 0)
               {
                   return true;
               }
           }
          
           return false;
       }
       string lower(string s)
       {
           int len = s.length();
           string res = "";
           for(int i = 0;i<len;i++)
           {
               res += tolower(s[i]);
           }
           return res;
       }
       void printDogs()
       {
           cout<<"\n******************************\n\n";
           cout<<"Dogs in Current Pen: \n";
           for(int i = 0;i<pos;i++)
           {
               dogs[i].printDog();
           }
          
       }
       void printDogsByColor(bool isBlack)
       {
          
           string furCol;
          
           for(int i = 0;i<pos;i++)
           {
              
               furCol = lower(dogs[i].getFurCol());
               if(isBlack)
               {
                   if(furCol.compare("black") ==0)
                   {
                       cout<<"\n\tA ";
                       dogs[i].printDog();
                       cout<<" is Hiding\n";
                   }
               }
               else
               {
                   if(furCol.compare("brown") ==0)
                   {
                       cout<<"\n\tA ";
                       dogs[i].printDog();
                       cout<<" is Hiding\n";
                   }
               }
           }
       }
       void printDogsByWeight(bool isLess)
       {
           for(int i = 0;i<pos;i++)
           {
               if(isLess)
               {
                   if(dogs[i].getWeight() <=40)
                   {
                       cout<<"\n\tA ";
                       dogs[i].printDog();
                       cout<<" is Barking\n";
                   }
               }
               else
               {
                   if(dogs[i].getWeight() > 40)
                   {
                       cout<<"\n\tA ";
                       dogs[i].printDog();
                       cout<<" is Barking\n";
                   }
               }
           }
       }
};
#endif

//main.cpp
#include "Pen.h"

void getDog(string &fur,string &eye,string &breed,float &weight)
{
  
   cout<<"Enter Fur Color(black,brown,white): "<<endl;
   getline(cin,fur);
  
  
   cout<<"Enter Eye Color(brown, black, blue): "<<endl;
   getline(cin,eye);
  
  
   cout<<"Enter Breed(labrador, german shepherd, poodle, boxer): "<<endl;
   getline(cin, breed);
   //input only float not strings
   cout<<"Enter Weight(10-100) : ";
   cin >> weight;
   cin.ignore();
   cin.clear();
}
int main()
{
   Pen pens;
   string fur;string eye;string breed;float weight;
  
   /*
   pens.addDog("brown","blue","German Shepherd",70);
   pens.addDog("white","brown", "Poodle",35);
   pens.addDog("black","black","Boxer",50);
   pens.addDog("brown","brown","Labrador",100);
   */
   string ch;
  
   while(1)
   {
       getDog(fur,eye,breed,weight);
       pens.addDog(fur,eye,breed,weight);
       cout<<"Press a key to add the next Dog. or q to quit: ";
       //input only integer
       getline(cin,ch);
       if(ch.compare("q") == 0 || ch.compare("Q") == 0)
       {
           cout<<"Exiting";
           break;
       }
   }
   return 0;
}

//sample input

brown
blue
German Shepherd
70
1
white
brown
Poodle
35
2
black
black
Boxer
50
3
brown
brown
Labrador
100
//sample output

CAWindows\SYSTEM32\cmd.exe Enter Fur olor<black, broun,uhite): Enter Eye I black. blue): lor(broun, Enter Breed< labrador, ge//at giving weight input please give only ints or floats not strings

//it will be cin error

Add a comment
Know the answer?
Add Answer to:
Write a program in C++ that places a dog into a pen, which can hold up...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The goal of this homework is to write a small database system for an Animal Shelter....

    The goal of this homework is to write a small database system for an Animal Shelter. This is a no-kill shelter like the one just west of the Addition Airport. You will accept animal “donations” to the shelter, but mostly only dogs and cats. (But yes, there may be the occasional hamster or other nondog, non-cat animal donation.) At present, all we need to do is write the skeleton of a database that will track all the animals in the...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create a "Hello C++! I love CS52" Program 10 points Create a program that simply outputs the text Hello C++!I love CS52" when you run it. This can be done by using cout object in the main function. 2. Create a Class and an Object In the same file as...

  • Write a program in C++ that simulates a soft drink machine. The program will need several...

    Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class. DrinkItem class The DrinkItem class will contains the following private data members: name: Drink name (type of drink – read in from a file). Type...

  • Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839....

    Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory allocation posted here under Pages. For sorting, you can refer to the textbook for Co Sci 839 or google "C++ sort functions". I've also included under files, a sample C++ source file named sort_binsearch.cpp which gives an example of both sorting and binary search. The Bubble sort is the simplest. For binary search too, you can refer to...

  • This is a C++ probelm, I am really struggling with that...... Can anyone help me?? Write...

    This is a C++ probelm, I am really struggling with that...... Can anyone help me?? Write a fraction class whose objects will represent fractions. For this assignment you aren't required to reduce your fractions. You should provide the following member functions: A set() operation that takes two integer arguments, a numerator and a denominator, and sets the calling object accordingly. Arithmetic operations that add, subtract, multiply, and divide fractions. These should be implemented as value returning functions that return a...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • Edit a C program based on the surface code(which is after the question's instruction.) that will...

    Edit a C program based on the surface code(which is after the question's instruction.) that will implement a customer waiting list that might be used by a restaurant. Use the base code to finish the project. When people want to be seated in the restaurant, they give their name and group size to the host/hostess and then wait until those in front of them have been seated. The program must use a linked list to implement the queue-like data structure....

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

  • C# - Inheritance exercise I could not firgure out why my code was not working. I...

    C# - Inheritance exercise I could not firgure out why my code was not working. I was hoping someone could do it so i can see where i went wrong. STEP 1: Start a new C# Console Application project and rename its main class Program to ZooPark. Along with the ZooPark class, you need to create an Animal class. The ZooPark class is where you will create the animal objects and print out the details to the console. Add the...

  • SCREENSHOTS OF CODE ONLY!! PLEASE DON'T POST TEXT!! C++ CODE! PLEASE DON'T REPOST OLD POSTS! Objective...

    SCREENSHOTS OF CODE ONLY!! PLEASE DON'T POST TEXT!! C++ CODE! PLEASE DON'T REPOST OLD POSTS! Objective To gain experience with the operations involving binary search trees. This data structure as linked list uses dynamic memory allocation to grow as the size of the data set grows. Unlike linked lists, a binary search tree is very fast to insert, delete and search. Project Description When an author produce an index for his or her book, the first step in this process...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT