Question

In: Computer Science

Design a class named Pet, which should have the following fields: Name – The name field...

Design a class named Pet, which should have the following fields:

Name – The name field holds the name of a pet.

Type – The type field holds the type of animal that is the pet. Example values are “Dog”, “Cat”, and “Bird”.

Age – The age field holds the pet’s age.

The Pet class should also have the following methods:

setName – The setName method stores a value in the name field.

setType – The setType method stores a value in the type field.

setAge – The setAge method stores a value in the age field.

getName – The getName method returns the value of the name field.

getType – The getType method returns the value of the type field.

getAge – The getAge method returns the value of the age field.

  • Draw a UML diagram of the class. Be sure to include notation showing each field and method’s access specification and data type. Also include notation showing any method parameters and their data types.
  • Write the code for the Pet class.

please write in c++ format

Solutions

Expert Solution

Code :-


#include <iostream> // this is for standerd input output

using namespace std; // to create seprate namespace and use cout cin directly

class pet { // pet class with private data Age,Name and Type
private:   // declration of data member
    int Age;
    string Name;
    string Type;

public: // this all method must be public as we are calling it from outside of this class
    // Setter
    void setAge(int a) { // setter method to set a Age of your pet it is void type as this return nothing
      Age = a;
    }
    // Getter
    int getAge() { // This is getter method to access or get the value of the Age of pet
      return Age;
    }
  
     void setName(string a) {// setter method to set a Name of your pet it is void type as this return nothing
      Name = a;
    }
    // Getter
    string getName() {// This is getter method to access or get the value of the name of pet
    }
  
     void setType(string a) { // setter method to set a Type of your pet it is void type as this return nothing
      Name = a;
      Type = a;
    }
    // Getter
   string getType() {// This is getter method to access or get the value of the Type of your pet
      return Type;
    }
  
};

int main() // our main function
{
    pet myObj;   // creating object of type pet
    myObj.setType("dog"); // assign value of Age ,Name and Type of pet using getter methods
    myObj.setAge(50000);
    myObj.setName("jony");
    cout <<" name of pet : "<< myObj.getName()<<endl; // get back value of Age ,Name and Type of pet using setter methods and print it
    cout <<" type of pet : "<< myObj.getType()<<endl;
    cout <<" age of pet : "<< myObj.getAge()<<endl;

    return 0;
}

OUTPUT:--

UML DIagram of class pet: -


Related Solutions

Term Project C++ Pet Class Problem Specification: Design a class named Pet, which should have the...
Term Project C++ Pet Class Problem Specification: Design a class named Pet, which should have the following fields: • name: The name field holds the name of a pet. • type: The type field holds the type of animal that a pet is. Example values are “Dog”, “Cat”, and “Bird”. • age: The age field holds the pet’s age. The Pet class should also have the following methods: • setName: The setName method stores a value in the name field....
Write a java program using the information given Design a class named Pet, which should have...
Write a java program using the information given Design a class named Pet, which should have the following fields (i.e. instance variables):  name - The name field holds the name of a pet (a String type)  type - The type field holds the type of animal that a pet is (a String type). Example values are “Dog”, “Cat”, and “Bird”.  age - The age field holds the pet’s age (an int type) Include accessor methods (i.e. get...
Design a class named Employee. The class should keep the following information in fields: ·         Employee...
Design a class named Employee. The class should keep the following information in fields: ·         Employee name ·         Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. ·         Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields...
Problem Statement Design a class named Car that has the following fields: year: The year field...
Problem Statement Design a class named Car that has the following fields: year: The year field is an integer that holds the car’s year. make: the make field is a String that holds the make of the car. speed: the speed field is an integer that holds the car’s current speed. In addition, the class should have the following constructor and other methods: Constructor: the constructor should accept the car’s year >model and make as parameters. These values should be...
Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
(Java) Design a class named Person with fields for holding a person’s name, address, and telephone...
(Java) Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class’s fields. Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator and...
Design a class named Person with fields for holding a person's name, address, and telephone number(all...
Design a class named Person with fields for holding a person's name, address, and telephone number(all as Strings). Write a constructor that initializes all of these values, and mutator and accessor methods for every field. Next, design a class named Customer, which inherits from the Person class. The Customer class should have a String field for the customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write a constructor that initializes these...
Design a class named Person with fields for holding a person's name, address, and telephone number(all...
Design a class named Person with fields for holding a person's name, address, and telephone number(all as Strings). Write a constructor that initializes all of these values, and mutator and accessor methods for every field. Next, design a class named Customer, which inherits from the Person class. The Customer class should have a String field for the customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write a constructor that initializes these...
JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone...
JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone number (all Strings) –Write a constructor that takes all the required information. –Write a constructor that only takes the name of the person and uses this to call the first constructor you wrote. –Implement the accessor and mutator methods. Make them final so subclasses cannot override them –Implement the toString() method •Design a class named Customer, which extends the Person class. It should have...
Create a class named “Car” which has the following fields. The fields correspond to the columns...
Create a class named “Car” which has the following fields. The fields correspond to the columns in the text file except the last one. i. Vehicle_Name : String ii. Engine_Number : String iii. Vehicle_Price : double iv. Profit : double v. Total_Price : double (Total_Price = Vehicle_Price + Vehicle_Price* Profit/100) 2. Write a Java program to read the content of the text file. Each row has the attributes of one Car Object (except Total_Price). 3. After reading the instances of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT