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

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...
(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...
a) Design a Java Class which represents a Retail Item. It is to have the following fields
 a) Design a Java Class which represents a Retail Item. It is to have the following fields Item Name Item Serial No Item Unit Price Item Stock Level Item Reorder Level It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level. b) Extend the Retail Item class of part a) above...
a) Design a Java Class which represents a Retail Item. It is to have the following fields
 a) Design a Java Class which represents a Retail Item. It is to have the following fields  Item Name  Item Serial No  Item Unit Price  Item Stock Level  Item Reorder Level  It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level.    b) Extend...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT