The language is java. Please implement methods for the prompts with simple expla
ID: 3738268 • Letter: T
Question
The language is java. Please implement methods for the prompts with simple explainations. Thank you Section 1140 pts): Method writing (You are only required to implement methods) 1) Implement a method called reverse takes an integer parameter, say num, and returns the reversed num integer Example: reversel12345) returns 54321 reversel1100) returns 11 2) implement a method called composite that takes an integer, say n, determines if it is not divisible by any number between 2 and n/2 Example composite(10) returns true composite 11) returnsfalse 3) Implement method searchArray which takes String array, say A, and string K If K is in the array K it returns 1 and if K is not in the array, then it returns-1 Example: Assume A-l"wku ekunku "ukwofs searchArraylA,"abc") returns 1 saerchArray/A wku") return 1 4 public class Point //this class defines a point in two-dimensional space with x and y coordinate private int xCor private int yCo Class method distance: determines distance between two pints p-(xl.y1) and q (x2,y2). Note distance dis Vx1-x2)1-y2)Explanation / Answer
1.
// method to reverse the integer
public int reverse(int num)
{
String revStr="";
// loop that continues till num > 0
while(num > 0)
{
// append modulus 10 of num to revStr. Modulus of 10 returns the number which occurs at the end of num
revStr += num%10;
num = num/10; // divide the number by 10
}
return Integer.parseInt(revStr); // convert String to integer
}
2.
// method that checks if a number is divisible by any number between 2 and n/2
public boolean composite(int n)
{
// loop i over 2 to n/2
for(int i=2;i<=n/2;i++)
// check if n is divisible by i, then return true
if(n%i == 0)
return true;
return false; // if n id not divisible by any number between 2 and n/2
}
3.
// method to search for a String in an String array
public int searchArray(String A[],String K)
{
// loop i from 0 to length of A
for(int i=0;i<A.length;i++)
// check if A[i] = K, then return 1
if(A[i].equals(K))
return 1;
return -1; // if K is not present in A
}
4.
public class Point{
// this class defines a point in two-dimensional space with x and y coordinate
private int xCor;
private int yCor;
// method to return the distance between the invoking point and passed point
// since its a class method it can directly access the x and y coordinate of the invoking Point object, so only 1 argument is required
public double distance(Point q)
{
double d = Math.sqrt((Math.pow((xCor - q.xCor),2)) + (Math.pow((yCor - q.yCor),2)));
return d;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.