Question

In: Computer Science

Develop a C++ class called PrimeNumberGenerator. An object of this class will generate successive prime numbers...

Develop a C++ class called PrimeNumberGenerator. An object of this class will generate successive prime numbers on request. Think of the object as being similar to the digital tasbeeh counter. It has a reset button which makes the counter return to 0. There is also a button to get the next prime number. On the tasbeeh, pressing this button increments the current value of the counter. In your object, pressing this button would generate the next prime number for display. You’ll implement the reset button as a member function reset() and the other button as a function getNextPrime(). When an object is first created and the getNextPrime() function is called, it will return the first prime number, i.e., 2. Upon subsequent calls to the getNextPrime() function, the subsequent prime numbers will be returned. Define any others functions such as constructor and attributes that you need. According to Wikipedia: “a prime number is a natural number greater than 1, that has no positive divisors other than 1 and itself.”

Solutions

Expert Solution

#include<iostream>
#include<conio.h>
using namespace std;

class PrimeNumberGenerator
{
   public:
       int num; //variable to store current prime


   PrimeNumberGenerator()
   {
       num=0; //initialising counter
   }


   void reset() //reset counter to 0
   {
       num=0;
       cout << "\nCounter reset";
   }


   int checkPrime(int n) //check if a number is prime or not
   {
       int i;
       if(n==1)
       return 0;
       for(i=2;i<n;i++)
       {
           if(n%i==0) //if number is divisible by any number between 2 and itself, it's not prime
           return 0;
       }
       return 1;
   }


   void getNextPrime()
   {
       num=num+1;
       while(checkPrime(num)==0) //increment counter till a prime is encontered
       num=num+1;
       cout << "\nPrime: " << num;
   }
};


int main()
{
   char ch;
   cout << "Enter 1 to get next prime, 0 to reset counter\n";
   PrimeNumberGenerator pr; //creating object of class PrimeNumberGenerator
   pr.getNextPrime(); //displaying first prime
   while(1)
   {
       ch=getch(); //getch() takes input a character without waiting for pressing enter
       if(ch == '0') //if input is 0, reset
       {
           pr.reset();
           pr.getNextPrime();
       }
       else if(ch == '1')
       pr.getNextPrime();
   }
}

Sample execution:

NOTE: I've created an infinite loop. If you want the program to quit for any character, say 'q', add another if block and add a break statement.

In case of any doubt drop a comment and I'll surely get back to you.

Please give a like if you're satisfied with the answer. Thank you.


Related Solutions

In the following questions you will be designing a class called SuperDraw to generate lotto numbers...
In the following questions you will be designing a class called SuperDraw to generate lotto numbers and manage the process of verifying whether a ticket is a winner. Every lotto ticket has 6 numbers generated randomly between 1 and 49. A number cannot be repeated in the same ticket. The main SuperDraw class structure should look like the following: struct ticket { unsigned int numbers[6]; ticket* next; }; class SuperDraw { private: ticket* ticketListHead; ticket* ticketListTail; public: SuperDraw(/* args */);...
In object C Define a class called XYPoint that will hold a Cartesian coordinate (x, y),...
In object C Define a class called XYPoint that will hold a Cartesian coordinate (x, y), where x and y are integers. Define methods to individually set the x and y coordinates of your new point and retrieve their values. Write an Objective-C program to implement your new class and test it (main section of the file). Your test program needs to create two instances of your class. Make sure your program test prints out the values that you have...
C# Prime factors are the combination of the smallest prime numbers, that, when multiplied together, will...
C# Prime factors are the combination of the smallest prime numbers, that, when multiplied together, will produce the original number. Consider the following example: Prime factors of 4 are: 2 x 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 Prime factors of 50 are: 2 x 5 x 5 Create a console application with a method named PrimeFactors that,...
Problem Description: SavingsAccount Develop a C++ class declaration called SavingsAccount class that allows user to input...
Problem Description: SavingsAccount Develop a C++ class declaration called SavingsAccount class that allows user to input initial values of dollars and cents and then asks for deposits and withdrawals. The class should include the following information: Operations (Member Functions) 1. Default constructor that sets both dollars and cents to 0. 2. The constructor has 2 parameters that set dollars and cents to the indicated values. 3. Open account (with an initial deposit). This is called to put initial values in...
Write a smallest_gap(start_num, end_num) function that finds smallest gap between successive primes, considering prime numbers in...
Write a smallest_gap(start_num, end_num) function that finds smallest gap between successive primes, considering prime numbers in the range from start_num to end_num (inclusive). For example, start_num = 5 and end_num = 12, the prime numbers in that range are: [5, 7, 11]. The smallest gap between any two prime numbers in this list is 2, so the function would return 2. You may want to modify your solution from Problem 1 on Assignment 4, or you can use this starter...
You will generate a People class of object and load an ArrayList with person objects, then...
You will generate a People class of object and load an ArrayList with person objects, then report the contents of that ArrayList. To do so, you must perform the following: (30 pts.) A) (15/30 pts. (line break, 11 pt) ) - Create a class file “People.java” (which will generate People.class upon compile). People.java will have eight(8) methods to 1) read a .txt file ‘people.txt’ 2) generate: ▪ List of all students AND teachers ▪ List of all students OR teachers...
Create a C# Application. Create a class object called “Employee” which includes the following private variables:...
Create a C# Application. Create a class object called “Employee” which includes the following private variables: firstN lastN idNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods: constructor properties CalcPay(): Calculate the regular pay and overtime pay. Create...
Must be coded in C#. 10.8 (Rational Numbers) Create a class called Rational for performing arithmetic...
Must be coded in C#. 10.8 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write an app to test your class. Use integer variables to represent the private instance variables of the class—the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 1/2 and would be stored in the object...
Let's try to develop a C++ Reverse Polish Notation (RPN) calculator! Create a base class called...
Let's try to develop a C++ Reverse Polish Notation (RPN) calculator! Create a base class called Operand Give it a virtual destructor to avoid any weird problems later on! Derive a class called Number from Operand Maintain a double member variable in class Number For simplicity, you may make the member variable public if you would like Derive a class called Operator from Operand Derive a class called Add from Operator (2 + 3 = 5) Derive a class called...
The assignment is to write a class called data. A Date object is intented to represent...
The assignment is to write a class called data. A Date object is intented to represent a particuar date's month, day and year. It should be represented as an int. -- write a method called earlier_date. The method should return True or False, depending on whether or not one date is earlier than another. Keep in mind that a method is called using the "dot" syntax. Therefore, assuming that d1 and d2 are Date objects, a valid method called to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT