Answer to Question #293997 in Java | JSP | JSF for jeffy

Question #293997

Question 1

Write down the method isPrime(int number), which accepts an positive integer and return a boolean

value if the number is Prime or not.

Question 2

Write a method called printUnique that accepts one integer array as argument, and print all unique elements

form the array. E.g. {5,6,7,8,9,8,7,6,5} (9 is unique).

Question 3

Given two arrays of same size and type, arr1={1,2,3,4,5} and arr2={6,7,8,9,10} calculate the

difference of all values in the array and your operation would be like (Arr2 - Arr1)


1
Expert's answer
2022-02-04T14:49:39-0500
Question 1


public class App {


	/**
	 * Question 1
	 * 
	 * Write down the method isPrime(int number), which accepts an positive integer
	 * and return a boolean value if the number is Prime or not.
	 * 
	 * @param number
	 * @return
	 */
	static boolean isPrime(int number) {
		if (number < 2) {
			return false;
		}
		if (number == 2 || number == 3) {
			return true;
		}
		for (int i = 2; i <= Math.sqrt(number * 1.0); i++) {
			if (number % i == 0) {
				return false;
			}
		}
		return true;
	}


	/**
	 * The start point of the program
	 * 
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
		if (isPrime(5)) {
			System.out.println("The the number is Prime");
		} else {
			System.out.println("The the number is NOT Prime");
		}


	}
}


Question 2


public class App {


	/**
	 * Question 2 Write a method called printUnique that accepts one integer array
	 * as argument, and print all unique elements form the array. E.g.
	 * {5,6,7,8,9,8,7,6,5} (9 is unique).
	 * 
	 * @param numbers
	 */
	static void printUnique(int numbers[]) {
		System.out.println("All unique elements form the array");
		for (int j = 0; j < numbers.length; j++) {
			int count1 = 0;
			for (int i = 0; i < numbers.length; i++) {
				if (numbers[i] == numbers[j]) {
					count1++;
				}
			}
			if (count1 == 1) {
				System.out.print(numbers[j] + " ");
			}
		}


	}


	/**
	 * The start point of the program
	 * 
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
		int numbers[] = { 5, 6, 7, 8, 9, 8, 7, 6, 5 };
		printUnique(numbers);


	}
}


Question 3



public class App {


	/**
	 * The start point of the program
	 * 
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
		int arr1[] = { 1, 2, 3, 4, 5 };
		int arr2[] = { 6, 7, 8, 9, 10 };


		for (int i = 0; i < arr2.length; i++) {
			System.out.println(arr2[i] + " - " + arr1[i] + " = " + (arr2[i] - arr1[i]));
		}


	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS