Question

In: Computer Science

In C++, implement a class called setOper that provides several simple set operations. The class only...

In C++, implement a class called setOper that provides several simple set operations. The class only needs to deal with sets that are closed intervals specified by two real numbers; for example, the pair (2.5, 4.5) represent the interval [2.5, 4.5].

The following operations should be supported:

- Check if the value x belongs to the given interval.

- Check if the value x belongs to the intersection of two intervals.

- Check if the value x belongs to the union of two intervals.

Test this class in the main program.

Hand in a zip file containing:

- Header and implementation files for the setOper class

- The main program source code

Solutions

Expert Solution

#include<iostream>
using namespace std;
class setOper
{
    public:
    
    bool belong(double*a,double n) //check if n belongs in the interval
    {
        if(a[0]<=n && a[1]>=n)
            return true;
        return false;
    }
    
    bool unify(double*a,double*b,double n) //check if n belongs in union
    {
        double ret[2] = {0,0};
        if(a[0]<b[0])
            ret[0] = a[0];
        else
            ret[0] = b[0];
        if(a[1]>b[1])
            ret[1] = a[1];
        else
            ret[1] = b[1];
        return belong(ret,n);
    }
    
    bool inter(double*a,double*b,double n) //check if n belongs in intersection
    {
        double ret[2] = {0,0};
        if(a[0]>b[0])
            ret[0] = a[0];
        else
            ret[0] = b[0];
        if(a[1]<b[1])
            ret[1] = a[1];
        else
            ret[1] = b[1];
        return belong(ret,n);
    }
};

int main()
{
    setOper so;
    double a[] = {-2,1};
    double b[] = {-1,2};
    double n = 1.5;
    cout << so.belong(a,n);
    cout << so.belong(b,n);
    cout << so.unify(a,b,n);
    cout << so.inter(a,b,n);
}

Related Solutions

Implement the stack class (called Stack2) using queue, meaning that the only operations that can be...
Implement the stack class (called Stack2) using queue, meaning that the only operations that can be used are the ones defined in the Queue class. class Queue: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def enqueue(self, item): self.items.insert(0, item) def dequeue(self): return self.items.pop() def size(self): return len(self.items) The codes to fill: """ 1. Stack2 class Implement stack data structure using queue """ class Stack2: def __init__(self): # Write your definition for __init__ here def isEmpty(self): #...
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...
in JAVA: implement a class called tree (from scratch) please be simple so i can understand...
in JAVA: implement a class called tree (from scratch) please be simple so i can understand thanks! tree(root) node(value, leftchild,rightchild) method: insert(value)
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main method. In the main method you should read in all of the rows of data from the Instructors Table in the Registration Database, then print out this data. Follow the steps outlined in class. Remember to use try-catch blocks for all database access code. The example in the book does not use Try-catch blocks, but you should
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class...
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class with public and private members and multiple constructors.  Gain a better understanding of the building and using of classes and objects.  Practice problem solving using OOP. Overview You will implement a date and day of week calculator for the SELECTED calendar year. The calculator repeatedly reads in three numbers from the standard input that are interpreted as month, day of month, days...
ROCK, PAPER, SCISSORS Using C++, you will implement a class called Tool. It should have an...
ROCK, PAPER, SCISSORS Using C++, you will implement a class called Tool. It should have an int field called strength and a char field called type. You may make them either private or protected. The Tool class should also contain the function void setStrength(int), which sets the strength for the Tool. Create 3 more classes called Rock, Paper, and Scissors, which inherit from Tool. Each of these classes will need a default constructor that sets the strength to 1 and...
C++ program homework question 1 1. Create and implement a class called clockType with the following...
C++ program homework question 1 1. Create and implement a class called clockType with the following data and methods (60 Points.): Data: Hours, minutes, seconds Methods: Set and get hours Set and get minutes Set and get seconds printTime(…) to display time in the form of hh:mm:ss default and overloading constructor Overloading Operators: << (extraction) operator to display time in the form of hh:mm:ss >> (insertion) operator to get input for hours, minutes, and seconds operator+=(int x) (increment operator) to...
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module.
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module. Think of all the things you would want to do with such a class and write corresponding member functions for your Module class. Your class declaration should be well-documented so that users will know how to use it.Write a main program that does the following: Declare an array of all your modules. The elements of the array must be of type Module....
C++ Using an appropriate definition of ListNode, design a simple linked list class with only two...
C++ Using an appropriate definition of ListNode, design a simple linked list class with only two member functions and a default constructor: void add(double x); boolean isMember(double x); LinkedList( ); The add function adds a new node containing x to the front (head) of the list, while the isMember function tests to see if the list contains a node with the value x. Test your linked list class by adding various numbers to the list and then testing for membership....
Implement a VotingMachine class that can be used for a simple election. Include the methods to...
Implement a VotingMachine class that can be used for a simple election. Include the methods to voteForDemocrat and voteForRepublican. Add a method to clear out all votes. Additionally, add a method to print the results. Write the driver code to simulate the voting. in java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT