Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class Int with the following components: a. A field to store an Int val

ID: 3569563 • Letter: C

Question

Create a class Int with the following components:

a. A field to store an Int value.

b. A constructor so that new Int (x) creates an Int object that stores the int value x.

c. An instance method toString so that x.toString () returns the value of the Int object x in String form.

d. An instance method plus so that x.plus(y) returns a new Int object whose value is the value of the Int xplus the value of the Int y. Thereshould be no side effects.

e. Instance methods minus, times, and div, similar to the plus method described above. (The div method should perform integer division, like the operator on int values.)

f. An instance method isPrime so that x.isPrime () returns true if the value of the Int x is a prime number.

In some object-oriented languages, like smalltalk, absolutely everything is an object- intergers, booleans, characters, strings, everything.In such languages, arithmetic really worls a bit like this excerise, though the language may provide a more natural syntax for operators.

Explanation / Answer

class Int
{
   int x;
   Int(int x)
   {
       this.x = x;
   }
   String toString(Int y)
   {
       return y.x.toString();
   }
   Int plus(Int y)
   {
       Int temp;
       temp.x = this.x + y.x;
       return temp;
   }
   Int minus(Int y)
   {
       Int temp;
       temp.x = this.x - y.x;
       return temp;
   }
   Int times(Int y)
   {
       Int temp;
       temp.x = this.x * y.x;
       return temp;
   }
   Int div(Int y)
   {
       Int temp;
       if(y.x!=0)
       {
           temp.x = this.x/y.x;
           return temp;
       }
       return null;
   }
   bool isPrime(Int y)
   {
       for(int i=2;i<=sqrt(y.x);i++)
       {
           if(y.x%i==0)return false;
       }
       return true;
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote