Question

In: Computer Science

Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that...

Using Java:

Create a class called MyNumber with an integer private attribute.

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

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

Create a getter to return the private integer attribute value.

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.

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

Solutions

Expert Solution

Refer to code snippet below. Output is attached.

public class MyClass {
        private int n;
        public MyClass() {}
        public MyClass(int n) throws Exception //param constructor
        {
                if(n<2)
                {
                        throw new IllegalArgumentException("Number is less than 2");
                }
                else
                {
                        this.n = n;
                }
        }
        public void setNum(int n) throws Exception //setter method
        {
                if(n<2)
                {
                        throw new IllegalArgumentException("Number is less than 2");
                }
                else
                {
                        this.n = n;
                }
        }
        public int getNum()//getter method
        {
                return this.n;
        }
        public boolean isPrime() //O(root(n))
        {
                for(int i = 2; i<Math.sqrt(this.n); i++)
                {
                        if(this.n%i == 0)
                        {
                                return false;
                        }
                }
                return true;
        }
        public int numberOfPrimes() //implements SOE
    {  
        boolean prime[] = new boolean[n+1]; 
        int count = 0;
        for(int i=0;i<this.n;i++) 
            prime[i] = true; 
          
        for(int p = 2; p*p <=this.n; p++) 
        { 
            
            if(prime[p] == true) 
            { 
                for(int i = p*p; i <= this.n; i += p) 
                    prime[i] = false; 
            } 
        }  
        for(int i = 2; i <= this.n; i++) 
        { 
            if(prime[i] == true) 
                count++; 
        } 
        return count;
    }
        public static void main(String[] args) throws Exception{
                MyClass cl = new MyClass(20);
                System.out.println(cl.isPrime());
                System.out.println(cl.numberOfPrimes());
                System.out.println(cl.getNum());
        }
}

Output:

false
8
20

Related Solutions

Using java, create a class called MyString that has one String called word as its attribute...
Using java, create a class called MyString that has one String called word as its attribute and the following methods: Constructor that accepts a String argument and sets the attribute. Method permute that returns a permuted version of word. For this method, exchange random pairs of letters in the String. To get a good permutation, if the length of the String is n, then perform 2n swaps. Use this in an application called Jumble that prompts the user for a...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer array called nums that has 20 cells b. generate a random number between 5 and 30, and populate the array nums c. print the minimum and maximum number in the array nums d. print sum and average of numbers in the array nums Your output look like this: (Note: numbers shown below will be different in your program due to the random numbers) minimum...
in java Create a class called Customer in three steps: • (Step-1): • Add a constructor...
in java Create a class called Customer in three steps: • (Step-1): • Add a constructor of the class Customer that takes the name and purchase amount of the customer as inputs. • Write getter methods getName and getPrice to return the name and price of the customer. You can write a toString() method which returns printed string of Customer name and its purchase. • (Step-2): Create a class called Restaurant. • Write a method addsale that takes customer name...
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last...
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games, and Goals Have two constructors Constructor 1 – default constructor; all values to "NONE" or zero Constructor 2 – accepts input of first name, last name, games and goals. Create get and set methods for each of the four attributes Create a method the returns a double that calculates the average goals per game This method checks for zero games played: If...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
in java Create a class City with x and y as the class variables. The constructor...
in java Create a class City with x and y as the class variables. The constructor with argument will get x and y and will initialize the city. Add a member function getDistanceFrom() to the class that gets a city as the input and finds the distance between the two cities.
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT