I am beginner in Java programming. I came across this while working on a program based on prime checker. It was specified already that;
The locked code in the editor will call the checkPrime method with one or more integer arguments. You should write the checkPrime method in such a way that the code prints only the prime numbers. Please read the code given in the editor carefully. Also please do not use method overloading!
I want to know what was the role of the statements following the comment line w.r.t. the below Java program?
What was their use in the locked code?
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.lang.reflect.*;
import static java.lang.System.in;
class Prime
{
void checkPrime(int ... num)
{
for(int n:num)
{
int i,k=0;
for(i=1;i<=n;i )
{
if(n%i==0)
k;
}
if(k==2)
System.out.print(n " ");
}
System.out.println();
}
}
public class Solution
{
public static void main(String[] args)
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(in));
int n1=Integer.parseInt(br.readLine());
int n2=Integer.parseInt(br.readLine());
int n3=Integer.parseInt(br.readLine());
int n4=Integer.parseInt(br.readLine());
int n5=Integer.parseInt(br.readLine());
Prime ob=new Prime();
ob.checkPrime(n1);
ob.checkPrime(n1,n2);
ob.checkPrime(n1,n2,n3);
ob.checkPrime(n1,n2,n3,n4,n5);
//What do the following statements from below till the end mean?
Method[] methods=Prime.class.getDeclaredMethods();
Set<String> set=new HashSet<>();
boolean overload=false;
for(int i=0;i<methods.length;i )
{
if(set.contains(methods[i].getName()))
{
overload=true;
break;
}
set.add(methods[i].getName());
}
if(overload)
{
throw new Exception("Overloading not allowed");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
CodePudding user response:
Basically, It is checking that have you done overloading in Prime class. It is getting all method which are declared in class Prime. then storing their names in set, so that, if any name already exist in set. It will throw exception that, you have overloaded function, which is restriction in your case.
//getting all method in Prime
Method[] methods=Prime.class.getDeclaredMethods();
Set<String> set=new HashSet<>();
boolean overload=false;
//looping through all method array
for(int i=0;i<methods.length;i )
{
//checking if function already exist in set means, method is overloaded.
if(set.contains(methods[i].getName()))
{
overload=true;
break;
}
//adding method name in set. (set is used because, it does not hold
mutiple enteries for one value)
set.add(methods[i].getName());
}
//if overload has been done in class then will throw exception
if(overload)
{
throw new Exception("Overloading not allowed");
}
}
catch(Exception e)
{
System.out.println(e);
}
CodePudding user response:
That code is just extracting the method names from class Prime and ensuring there are no duplicate names. It does this iteratively by checking a set of method names for the current method name being evaluated. After each evaluation, the method name is added to the set so that the next iteration can detect if the next method name is already present.
So that 'locked' code is checking that you have complied with the request of not overloading checkPrime by writing 4 versions of it. I assume you will fail if you remove the 'locked' code also.
To summarise, since there are 4 examples of calling checkPrime with varying number of integer arguments, they want you to demonstrate writing a single checkPrime function that handles a varying number of integer arguments.
ob.checkPrime(n1);
ob.checkPrime(n1,n2);
ob.checkPrime(n1,n2,n3);
ob.checkPrime(n1,n2,n3,n4,n5);
There is a Java way to do this. See: https://www.baeldung.com/java-varargs
