Question

In: Computer Science

Complete the following using c++ (a) Declare a class called Capacitor representing the electronic component capacitor,...

Complete the following using c++

(a) Declare a class called Capacitor representing the electronic component capacitor, which has the following characteristics:

Capacitance in Farads (example value: 0.47 mF)

Voltage rating in Volts (example value: 25.0 V)

Temperature rating in Celcius (example value: 85.0 oC)

Type ("Paper", "Polyester", "Electrolytic", "Ceramic").

These attributes should be represented as the data members of the class and be hidden from the outside of the class.

The class should also have a constructor used to initialise the data members of the objects of this class when they are created. The default values for the data members will be set to 0 for all numerical data members and to “Paper” for the type data member.

The class should also have a void member function called displayCapacitor() with no parameters, which is designed to print the capacitor attributes on a single line, on the screen.

(b) Given this class definition, write the necessary statements in the main() function, which would create an array capable of storing 10 objects of the Capacitor class, then create two Capacitor objects c1 and c2, and store them to the array. The first capacitor object should have the default characteristics, and the second one with the following characteristics:

Capacitance: 0.47 mF

Voltage: 25.0 V

Temperature: 85.0 oC

Type: Ceramic

Once the array is populated with the capacitor object, use an appropriate loop structure to display the string representation of only the capacitor objects with temperature greater than 70 oC on the screen. Assume that a public member function named getTemperature() exists which returns the capacitor temperature.

Solutions

Expert Solution

C++ program for the provided problem statement

// include all required header files
#include <iostream>
#include <string>
#include <iomanip>
#define MAX_SIZE 10

using namespace std;

// Driver Class
class Capacitor{
    
    // public members    
    public:
        // create an enumeration for the types of the Capacitors
        enum Type {Paper, Polyester, Electrolytic, Ceramic};
        
        // default constructor
        Capacitor(){
            capacitance = 0;
            voltage = 0;
            temperature = 0;
            type = Paper;
        }
        
        // constructor with parameters
        Capacitor(float c, float v, float t, Type tp){
            capacitance = c;
            voltage = v;
            temperature = t;
            type = tp;
        }
        
        // this will return the temperature of the current Capacitor object
        float getTemperature(){
            return temperature;
        }
        
        // this will display the attributes of the current Capacitor object
        void displayCapacitor(){
            cout << "Capacitor attributes-\n";
            cout << "\nCapacitance: " << setprecision(2) << capacitance << " mF";
            cout << "\nVoltage: " << fixed << setprecision(1) << voltage << " V";
            cout << "\nTemperature: " << fixed << setprecision(1) << temperature << " oC";
            
            if( type == 0){
                cout << "\nType: Paper";
            }else if(type == 1){
                cout << "\nType: Polyester";
            }else if(type == 2){
                cout << "\nType: Electrolytic";
            }else{
                cout << "\nType: Ceramic";
            }
        }
     
    // private members   
    private:
        float capacitance;
        float voltage;
        float temperature;
        Type type;
};

// main function
int main()
{
    // create an array of objects with max size
    Capacitor objectsArray[MAX_SIZE];
    
    // create default object    
    Capacitor obj1;
    
    // create an object with attributes
    Capacitor obj2(0.47, 25.0, 85.0, Capacitor::Ceramic);
    
    // we, can create more objects here
    // now, store the objects to the array
    objectsArray[0] = obj1;
    objectsArray[1] = obj2;

    // now, run loop to display the attributes of those Capacitor objects
    // whose temperature > 70.0 oC  
    for(int i=0; i<MAX_SIZE; i++)
    {
        // get the temperature for the current Capacitor object
        float temp = objectsArray[i].getTemperature();
      
        // check condition
        if( temp > 70){
            // if true then call function
            objectsArray[i].displayCapacitor();
        }
    }
    return 0;
}

Program Output


Related Solutions

How does one declare a class variable in C#?
How does one declare a class variable in C#?
The time it takes to complete the assembly of an electronic component is normally distributed with...
The time it takes to complete the assembly of an electronic component is normally distributed with a standard deviation of 4.5 minutes. If a random of 20 components is selected, what is the probability that the standard deviation for the time of assembly of these units is less than 3.0 minutes?
CODE: C# using System; public static class Lab6 { public static void Main() { // declare...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double ratePay, taxRate, grossPay, netPay=0; string lastName; // enter the employee's last name Console.Write("Enter the last name of the employee => "); lastName = Console.ReadLine(); // enter (and validate) the number of hours worked (positive number) do { Console.Write("Enter the number of hours worked (> 0) => "); hrsWrked = Convert.ToInt32(Console.ReadLine()); } while (hrsWrked < 0); // enter (and validate) the...
Add a generic Node class to the Java project. In the class Declare the fields using...
Add a generic Node class to the Java project. In the class Declare the fields using the generic type parameter, follow the book specification Define the accessor method getLink( ) Define the constructor, follow the book implementation Note: at the definition the name of the constructor is Node, however every time you use it as a type must postfix the generic type like Node<T> Define the addNodeAfter( ) method as explained in the PP presentation, use the generic type as...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that...
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that has floating point data members length and width. The class has the following member functions: void setlength(float) to set the length data member void setwidth(float) to set the width data member float perimeter() to calculate and return the perimeter of the rectangle float area() to calculate and return the area of the rectangle void show() to display the length and width of the rectangle...
follow pseudo 1) Create class called Node Declare private integer called data (or any other name)...
follow pseudo 1) Create class called Node Declare private integer called data (or any other name) Declare private Node called link (or any other name) 2) Declare constructor Should be public (ex: Node) where: link is equal to null data is equal to zero 3) Declare another constructor public Node with parameters integer d, Node n where: data is equal to d link is equal to n 4) Declare function to set link to next Node link equal to n...
Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
Never declare a C++ global variable in this class. But global constants are ok. unsigned Size1...
Never declare a C++ global variable in this class. But global constants are ok. unsigned Size1 = 100; // never do this at global scope const unsigned Size2 = 120; // this is ok (but probably not very helpful) Don't use the string class and don't use system functions for math (like pow) or strings (like strlen). These functions are meant to be short and simple; if you're writing something long and complicated, look for a simpler solution. void countdown(...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT