Question

Define a public method that is called isPrime() that returns a boolean and implements the Sieve...

Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method.

Define a public method that is called numberOfPrimes() that returns the number of prime numbers between 2 and the private attribute value.

Show this object in a main method that allows the user to interact with all the public methods of your class.

Make a class called MyNumber with an integer private attribute.

Make a constructor that defines an integer parameter to set the private integer attribute.

Make a setter that validates the attribute does not accept a value lower than 2 or the method will throw a IllegalArgumentException.

Make a getter to return the private integer attribute value.

An example done in Java would help in comparing it to mine.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// MyNumber.java
public class MyNumber {
  
   // private field to store the value
   private int value;
  
   // constructor
   public MyNumber(int value)
   {
       // if argument value < 2 throw illegal argument exception
       if(value < 2)
           throw new IllegalArgumentException();
       this.value = value; // else set value to input value
   }
  
   // method to set the value
   public void setValue(int value)
   {
       // if argument value < 2 throw illegal argument exception
       if(value < 2)
           throw new IllegalArgumentException();
       this.value = value;
   }
  
   // method to return the value
   public int getValue()
   {
       return value;
   }
  
   // method to return if n is prime or not using Sieve of Eratosthenes method
   public boolean isPrime(int n)
   {
       // Create a list of consecutive integers from 2 through n: (2, 3, 4, ..., n).
       boolean mark[] = new boolean[n+1];
      
       // set them initially to true
       for(int i=0;i<=n;i++)
           mark[i] = true;
      
       // loop from p=2 till square root of n
       for(int p=2;p*p<=n;p++)
       {
           if(mark[p]) // if mark[p] is true(p is a prime number)
           {
               for(int j=p*2;j<=n;j=j+p) // update the multiples of p as false
                   mark[j]= false;
          
               // if n is not prime return false
               if(!mark[n])
                   return false;
           }
       }
      
       return mark[n]; // return the value stored in mark[n] for the input n
   }
  
   // method to count and return the number of prime numbers between 2 and value using isPrime method
   public int numberOfPrimes()
   {
       int count = 0;
      
       for(int i=2;i<=value;i++)
       {
           if(isPrime(i))
               count++;
       }
      
       return count;
   }

   public static void main(String[] args) {

       // test the class
       MyNumber num = new MyNumber(25);
       System.out.println("Number of prime numbers between 2 and "+num.getValue()+" : "+num.numberOfPrimes());
   }

}

//end of MyNumber.java

Output:


Add a comment
Know the answer?
Add Answer to:
Define a public method that is called isPrime() that returns a boolean and implements the Sieve...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Define an abstract class GeometricShape with a color (string), filled (boolean), and dateCreated (java.util.Date) GeometricShape has...

    Define an abstract class GeometricShape with a color (string), filled (boolean), and dateCreated (java.util.Date) GeometricShape has getter/setter methods for each attribute, along with a getArea, a getPerimeter, and toString method that returns the shape information in a formatted string including class name, color, filled, dimensions (if available), area, perimeter, and date created Additionally GeometricShape implements the Java Comparable interface and defines a static method max used to find the larger of two GeometricShapes based on their area

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • answer the questions throughout this program public class Day implements Comparable {    Private Boolean atWork;...

    answer the questions throughout this program public class Day implements Comparable {    Private Boolean atWork;    Private String Weather;    Private int fuNumber; } public Day ( boolean Atwork, String Weather, int funNumber) {    Atwork = Atwork;    Weather = Weather;    funNumber = funNumber; } //question 1: implement the getter and setter (mutatto of the funNumber data member. if the number is outside the range 0-10 make the fuNumebr=5; public boolean equals (Day rhs) {    //...

  • Consider the following class definition class FibSequence implements Iterablexnteger public Iterator<Integer> iterator) return new FibIterator); private class FibIterator implements Iterator&lt...

    Consider the following class definition class FibSequence implements Iterablexnteger public Iterator<Integer> iterator) return new FibIterator); private class FibIterator implements Iterator<Integer> f //complete this You are required to create an inner class in the FibSequence class. The inner class should be called FibIterator. It implements the Iterator<Integer> interface and implements the hasNext() and the next() methods. The iterator iterates through the Fibonacci sequence. The Fibonacci sequence is a series of numbers where a number is the sum of previous two numbers....

  • Consider the class public elass Box·xtenda 0bJeetE private String stuff』 Write two constructors for the class....

    Consider the class public elass Box·xtenda 0bJeetE private String stuff』 Write two constructors for the class. The first constructor will take zero input arguments and set the stuff attibute to null. The second will take a String as input and set the shul atbute to input string Getter and Setter Write a public getter method called getsauff that returns the string stored by the sthuff attribute If stulf is null, it should return the string "no stuff in here Write...

  • public class Fish { private String species; private int size; private boolean hungry; public Fish() {...

    public class Fish { private String species; private int size; private boolean hungry; public Fish() { } public Fish(String species, int size) { this.species = species; this.size = size; } public String getSpecies() { return species; } public int getSize() { return size; } public boolean isHungry() { return hungry; } public void setHungry(boolean hungry) { this.hungry = hungry; } public String toString() { return "A "+(hungry?"hungry":"full")+" "+size+"cm "+species; } }Define a class called Lake that defines the following private...

  • Java programming....Queues Create a method called "hasNodes". This returns true or false depending on if any...

    Java programming....Queues Create a method called "hasNodes". This returns true or false depending on if any nodes exist in the queue public class AQueue{ private AQueue head = null; private AQueue tail = null; public boolean hasNodes(){ ......}

  • cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher,...

    cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes,...

  • In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying...

    In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying nodes referenced by positions p and q to be exchanged for each other. Relink the existing nodes, do not create any new nodes. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- package lists; import java.util.Iterator; import java.util.NoSuchElementException; public class LinkedPositionalList implements PositionalList { //---------------- nested Node class ---------------- /** * Node of a doubly linked list, which stores a reference to its * element and to both the previous and next...

  • I need help with these Java programming assignements. public class Die { //here you declare your...

    I need help with these Java programming assignements. public class Die { //here you declare your attributes private int faceValue; //operations //constructor - public Die() { //body of constructor faceValue=(int)(Math.random()*6)+1;//instead of 1, use random approach to generate it } //roll operation public void roll() { faceValue=(int)(Math.random()*6)+1; }    //add a getter method public int getFaceValue() { return faceValue; }    //add a setter method public void setFaceValue(int value) { faceValue=value; } //add a toString() method public String toString() { String...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT