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): #...
For a project I have to implement a class called BigInteger, with a representative small set...
For a project I have to implement a class called BigInteger, with a representative small set of operations. (working with big integers that is far more digits that java's int data type will allow. Here's the code, including what I have so far. I would appreciate any help. thank you. i'm not sure if what i have is right (the only thing i wrote is under the public static BigInteger parse(String integer) we just have to implement that method. package...
C++ ONLY! Implement the find function for the List class. It takes a string as an...
C++ ONLY! Implement the find function for the List class. It takes a string as an argument and returns an iterator to a matching node. If no matching node, it returns a null iterator. #include <iostream> #include <cstddef> #include <string> using Item = std::string; class List { private: class ListNode { public: Item item; ListNode * next; ListNode(Item i, ListNode *n=nullptr) { item = i; next = n; } };    ListNode * head; ListNode * tail;    public: class...
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...
Please Use C++ to finish as the requirements. Implement a class called SinglyLinkedList. In the main...
Please Use C++ to finish as the requirements. Implement a class called SinglyLinkedList. In the main function, instantiate the SinglyLinkedList class. Your program should provide a user loop and a menu so that the user can access all the operators provided by the SinglyLinkedList class. DestroyList InitializeList GetFirst InsertFirst, InsertLast, Insert DeleteFirst, DeleteLast, Delete IsEmpty Length Print, ReversePrint
Please solve in C++ only class is not templated You need to write a class called...
Please solve in C++ only class is not templated You need to write a class called LinkedList that implements the following List operations: public void add(int index, Object item); // adds an item to the list at the given index, that index may be at start, end or after or before the // specific element 2.public void remove(int index); // removes the item from the list that has the given index 3.public void remove(Object item); // finds the item from...
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
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)
C++ coding functions Implement the following class using linked lists. Creating a simple linked list class...
C++ coding functions Implement the following class using linked lists. Creating a simple linked list class to use is probably your first step.(Please do not use collections like .push() . pop(), etc.) and instead create the implementation A templated stack class that stores type T with the following public functions: - void Push(T t) - adds t to the top of the stack - T Pop() - asserts that the stack is not empty then removes and returns the item...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT