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

Code must be in java. Create a class Int with the following components: a. A fie

ID: 3823639 • Letter: C

Question

Code must be in java.

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 x plus the value of the Int y. There should 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-integers, booleans, characters, strings, everything. In such languages, arithmetic really works a bit like this exercise, though the language may provide a more natural syntax for operators.

Explanation / Answer

Int.java

public class Int {
   //Declaring instance variable
   private int x;

   //Parameterized constructor
   public Int(int x) {
       super();
       this.x = x;
   }

   //getters and setters
   public int getX() {
       return x;
   }

   public void setX(int x) {
       this.x = x;
   }

   //This method will add two numbers
   public Int plus(Int y) {
       x = x + y.getX();
       Int z = new Int(x);
       return z;
   }

   //This method will subtract two numbers
   public Int minus(Int y) {
       x = x - y.getX();
       Int z = new Int(x);
       return z;
   }

   //This method will divide two numbers
   public Int div(Int y) {
       x = x / y.getX();
       Int z = new Int(x);
       return z;
   }

   //This method will multiply two numbers
   public Int times(Int y) {
       x = x * y.getX();
       Int z = new Int(x);
       return z;
   }

   //This method will check whether the number is prime or not
   public boolean isPrime() {
       // If the user entered number is '2' return true
       if (x == 2)
           return true;
       for (int i = 2; i * i <= x; i++) {
           if (x % i == 0)
               return false;
       }
       return true;

   }

   //toString method is used to display the contents of an object inside it
   @Override
   public String toString() {
       return "Int [x=" + x + "]";
   }

}

__________________

Test.java

import java.util.Scanner;

public class Test {

   public static void main(String[] args) {

       int x,y;
      
       //Scanner object is used to get the inputs entered by the user
               Scanner sc=new Scanner(System.in);
              
               //Getting the inputs entered by the user
               System.out.print("Enter an Inter value of x :");
               x=sc.nextInt();
              
               System.out.print("Enter an Inter value of y :");
               y=sc.nextInt();
              
               //Creating objects by passing the input values
               Int int1=new Int(x);
               Int int2=new Int(y);
              
               //Performing addition operation
               Int int3=int1.plus(int2);
               System.out.println(int3.toString());

               //Performing subtraction operation
               Int int4=int1.minus(int2);
               System.out.println(int4.toString());
              
               //Performing division operation
               Int int5=int1.div(int2);
               System.out.println(int5.toString());
              
               //Performing multiplication operation
               Int int6=int1.times(int2);
               System.out.println(int6.toString());
              
               //checking the number is prime or not
               boolean bool=int2.isPrime();
               if(bool)
               System.out.println(y+" is a Prime Number");
               else
               System.out.println(y+" is not a Prime Number");                  
              
   }

}

_____________________

Output:

Enter an Inter value of x :45
Enter an Inter value of y :5
Int [x=50]
Int [x=45]
Int [x=9]
Int [x=45]
5 is a Prime Number

___________Thank You