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

Write a Java program that includes predicates (functions which return a logical

ID: 3854921 • Letter: W

Question

Write a Java program that includes predicates (functions which return a logical value) to find special integers called K-numbers. A positive integer X is a K-number if:

          1.           X is an odd integer, and

          2.           X is a square (X = N*N), and

3.       X is symmetric (if the digits are reversed, the number is unchanged). A symmetric integer is called a palindrome.

Your program should include the following predicates:

a.       boolean OddInt(long X): return value is true if X is an odd integer, and false otherwise.

b.       boolean SquareInt(long X): return value is true if X is a perfect square, and false otherwise.

c.       boolean SymmetricInt(long X): return value is true if X is symmetric, and false otherwise.

d.       boolean KNumber(long X): return value is true if X is a K-number, and false otherwise. This function should be defined using the above three functions in the following logical rule:

KNumber(X) if OddInt(X) and SquareInt(X) and SymmetricInt(X)

In a Java program, the boolean datatype with values true and false can be used to represent logical variables.

Have your program receive two integer values N1 and N2 as command-line input. Then check all integers from N1 to N2 to find and display any K-numbers within that range.

Run your program for the following input ranges:

1.           N1 = 1                         N2 = 1,000

2.           N1 = 1,000                              N2 = 1,000,000

            3.         N1 = 1,000,000                       N2 = 10,000,000

Can you find any larger K-Numbers?

Explanation / Answer

The answer is as follows:

The code is as follows:

import java.io.*;
import java.util.*;
import java.lang.*;


public class Number {

    public boolean OddInt(long a){
         if (a % 2 != 0)
            return true;
         else
            return false;
    }
    public boolean SquareInt(long a){
         int sqrt = (int) Math.sqrt(a);
         if(sqrt*sqrt == a)
             return true;
         else
            return false;
    }
    public boolean SymmetricInt(long a){
      long n;
      long rev = 0;
      n = a;
      while( n != 0 )
      {
          rev = rev * 10;
          rev = rev + n%10;
          n = n/10;
      }
      if (rev == a)
         return true;
      else
         return false;

    }
    public boolean KNumber(long a){
      if (OddInt(a) && SquareInt(a) && SymmetricInt(a))
         return true;
      elses
         return false;

    }
}

public class Demo {

   public static void main (String[] args) {
         long N1,N2,i;
         Number check = new Number();
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter N1");
         N1 = sc.nextLong();
         System.out.println("Enter N2");
         N2 = sc.nextLong();
         for (i=N1; i<=N2; i++){
             if (check.KNumber(i))
                System.out.println(i);
         }
   }
}

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