Write a complete java program, called MyProgram, that has the following properti
ID: 3684134 • Letter: W
Question
Write a complete java program, called MyProgram, that has the following properties: A method, called computeSum, that receives 2 integer values, say A and B, and returns the sum of all values between A and B (including A and B). A must be less than B for the method to return the sum. Otherwise the method returns 0. No printing inside the method. A method, called isPrime, that receives the sum and determines if the sum is a prime number by returning true if the number is prime and false if the number is not prime. No printing inside the method. Sample Runs Enter 2 numbers: 2 4 Output is 9 9 is not a prime numberExplanation / Answer
import java.util.Scanner;
public class MyProgram {
//function to add number between a and b
public int computeSum(int a, int b) {
int total=0;
if(b<a) {
return 0;
}else {
for(int i=a;i<=b;i++) {
total = total + i;
}
return total;
}
}
//function to check prime number
public boolean isPrime(int num) {
if(num==1||num==2) {
return true;
}else {
for(int i=2;i<num/2;i++) {
if(num%i==0) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
int num1, num2;
MyProgram mp = new MyProgram(); //create object
System.out.print("Enter 2 Numbers : ");
Scanner scan = new Scanner(System.in);
num1 = scan.nextInt();//scan first number
num2 = scan.nextInt(); //scan second number
int total = mp.computeSum(num1, num2); //call computeSum method
System.out.println("Output is "+total); //call isprime method
boolean bool = mp.isPrime(total);
if(bool) {
System.out.println(total+" is prime number");
}else {
System.out.println(total+" is not prime number");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.