Answer to Question #25513 in C++ for XieaRf

Question #25513
design a class named MyInteger the class contains...
an int data field named value that stores the int value represented by this object
a constructor that creates a MyInteger object for the specified int value
a get method that returns the int value
the methods isEven(), isOdd(), and isPrime() that return true if the value of the object is even, odd or prime, respectively.
The static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively.
The static method isEven(MyInteger), isOdd(MyInteger), and isPrime(MyInteger) that return true if the specified value is even, odd, or prime, respectively.
The methods equals(int) and equals(MyInteger) that return true if the value in this object is equal to the specified value.
A static method parseInt(char[]) that converts an array of numeric characters to an int value.
A static method parseInt(String) that converts a string into an int value.
1
Expert's answer
2013-03-12T08:27:49-0400
//main
#include"MyInteger.h"

void main()
{
for(int i=0;i<100;i++){&
& MyInteger* mi = new MyInteger(i);
& cout<<i<<" isPrime -& "<<boolalpha<<(*mi).isPrime()<<endl;
& cout<<i<<"& isOdd& - quot;<<boolalpha<<(*mi).isOdd()<<endl;
& cout<<i<<"& isEven& - quot;<<boolalpha<<(*mi).isEven()<<endl;
& delete mi;
}
cin.get();
}



//
#include"MyInteger.h"

bool MyInteger::isPrime()
{
int num = value;
if(num == 0)
& return true;
num = abs(num);
for(int i = 2; i <= sqrt((double)num); i++)
& if(num % i == 0)
& return false;
return true;
}

bool MyInteger::equals(int n)
{
return value == n;
}

bool MyInteger::equals(MyInteger myInt)
{
return myInt.getGalue() == value;
}

bool MyInteger::isPrime(int n)
{
MyInteger mi(n);
return mi.isPrime();
}

bool MyInteger::isPrime(MyInteger myInt)
{
return myInt.isPrime();
}

int MyInteger::parseInt(char charArray[])
{
return atoi(charArray);
}

int MyInteger::parseInt(string str)
{
return atoi(str.c_str());
}


//

#include<iostream>
#include<string>
#include<math.h>

using namespace std;

class MyInteger
{
private:
int value;
public:
MyInteger(int val):value(val){}

int getGalue(){return value;}
bool isEven(){return value%2 == 0;}
bool isOdd(){return value%2 == 1;}
bool isPrime();

bool equals(int n);
bool equals(MyInteger myInt);

static bool isEven(int n){return n%2 == 0;};
static bool isOdd(int n){return n%2 == 1;};
static bool isPrime(int n);

static bool isEven(MyInteger myInt){return myInt.getGalue()%2 == 0;};
static bool isOdd(MyInteger myInt){return myInt.getGalue()%2 == 1;};
static bool isPrime(MyInteger myInt);

static int parseInt(char charArray[]);
static int parseInt(string str);
};

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS