Hello I need help finishing this problem. In my assignment I am given a test cod
ID: 3551032 • Letter: H
Question
Hello I need help finishing this problem.
In my assignment I am given a test code.
I have to develop a class called "Number" that represents an integer value. The class should have a constructor that takes the integer value as its only argument. The constructor should store this value in a private member variable. Here is what I am able to come up with so far.
Class Number
{
public:
Number();
private:
};
What do I have to add to the class code?
The second part of my question is that I have to define a public member function called "isPrime" that takes no argument and returns a bool. If the number is a prime number, then isPrime returns true, otherwise it returns false.
I also have to define another member function called isDivisibleBy that takes a single integer argument and returns a bool. If the integer is passed into "isDivisibleBy" is a divisor for the number, then the function returns true, otherwise it returns false. Here's what I came up with
#include "Number.h"
Number::Number()
{
}
bool Product::isPrime()
{
}
bool Product::isDivisibleBy()
{
}
Explanation / Answer
/* formatted code at : https://gist.github.com/bitwiser/9206761 */
/* Add this code in Number.h */
class Number
{
private:
int val;
public:
Number(int);
bool isPrime();
bool isDivisibleBy(int);
};
/* Add this code in Number.cpp */
#include "Number.cpp"
#include <cmath>
Number::Number(int a){
this->val = a;
}
bool Number::isPrime(){
int a = this->val;
if(a==1)
return false;
if(a==2)
return true;
int sq = sqrt(a);
for(int i=2;i<sq;i++){
if(a%i==0)
return false;
}
return true;
}
bool Number::isDivisibleBy(int d){
if(this->val % d == 0)
return true;
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.