Write a program in Java for computing abundant , deficient , perfect and prime n
ID: 3602275 • Letter: W
Question
Write a program in Java for computing abundant, deficient, perfect and prime numbers according to the following specifications:
The program first asks the user for an input in the form: condition = value, where condition is a word from the set {limit, deficient, abundant, perfect, prime}, and value is a positive integer.
Then it verifies the input. If the input is invalid it prints a message indicating that and terminates.
If the input is valid it prints a table with the number of abundant, deficient, perfect and prime numbers less than or equal to N, where N = 1, 2, 3, ..., limit.
The value of limit is determined by the user input. That is, the tabulation stops when the number in the corrsponding column reaches the value, specified in the input condition = value.
For example, if the user enters limit = 10 the progam should print:
N Abundand Deficient Perfect Prime
If the user enters abundant = 2 the progam should print:
N Abundand Deficient Perfect Prime
1 0 1 0 0
2 0 2 0 1
3 0 3 0 2
4 0 4 0 2
5 0 5 0 3
6 0 5 1 3
7 0 6 1 4
8 0 7 1 4
9 0 8 1 4
10 0 9 1 4
11 0 10 1 5
12 1 10 1 5
13 1 11 1 6
14 1 12 1 6
15 1 13 1 6
16 1 14 1 6
17 1 15 1 7
18 2 15 1 7
Requirements and restrictions:
Use the scanner class for input.
Use the Type class (Type.java) to check for the type of the value in the input.
Use proper names for the variables suggesting their purpose.
Format your code accordingly using indentation and spacing.
Use multiple line comment in the beginning of the code and write your name, e-mail address, class, and section.
For each line of code add a short comment to explain its meaning.
Extra credit (up to 2 points): After (or on the right of) the table the program should print a horizontal bar diagram with the numbers in the corresponding column specified in the input. Use star characters (*) to print the diagram
Explanation / Answer
NOte: Could u please tell me if u want me to do any modifications after running the program.
the input must be like limit = 10 (seperated by space)
______________
AbundantDeficientPerfectPrime.java
import java.util.Scanner;
public class AbundantDeficientPerfectPrime {
public static void main(String[] args) {
//Declaring variables
String condition;
char op;
int value;
/* This while loop continues to execute
* until the user enters a valid input
*/
while (true) {
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
System.out.print("Enter input :");
condition = sc.next();
op = sc.next(".").charAt(0);
value = sc.nextInt();
if (((condition.equalsIgnoreCase("limit")) || (condition.equalsIgnoreCase("deficient")) || (condition.equalsIgnoreCase("abundant")) || (condition.equalsIgnoreCase("perfect")) || (condition.equalsIgnoreCase("prime"))) && (op != '=') && (value < 0)) {
System.out.println("Invalid.");
} else {
break;
}
}
//Based on the user selection display the appropriate table
if (condition.equalsIgnoreCase("limit")) {
findLimit(value);
} else if (condition.equalsIgnoreCase("deficient")) {
findDeficient(value);
} else if (condition.equalsIgnoreCase("abundant")) {
findAbundant(value);
} else if (condition.equalsIgnoreCase("perfect")) {
findPerfect(value);
} else if (condition.equalsIgnoreCase("prime")) {
findPrime(value);
}
}
/* This function will display table until
* the user entered input limit value
*/
private static void findLimit(int value) {
int def = 0;
int abun = 0;
int perfect = 0;
int prime = 0;
System.out.println("N Abundand Deficient Perfect Prime");
System.out.println("**************************************************************");
for (int i = 1; i <= value; i++) {
if (isAbundantOrDeficient(i) == 1)
abun++;
else if (isAbundantOrDeficient(i) == 0)
def++;
if (isPerfect(i))
perfect++;
if (isPrime(i))
prime++;
System.out.println(i + " " + abun + " " + def + " " + perfect + " " + prime);
}
}
/* This function will display table until
* the user entered input deficient value
*/
private static void findDeficient(int value) {
int def = 0;
int abun = 0;
int perfect = 0;
int prime = 0;
int i = 1;
System.out.println("N Abundand Deficient Perfect Prime");
System.out.println("**************************************************************");
while (def < value) {
if (isAbundantOrDeficient(i) == 1)
abun++;
else if (isAbundantOrDeficient(i) == 0)
def++;
if (isPerfect(i))
perfect++;
if (isPrime(i))
prime++;
System.out.println(i + " " + abun + " " + def + " " + perfect + " " + prime);
i++;
}
}
/* This function will display table until
* the user entered input abundant value
*/
private static void findAbundant(int value) {
int def = 0;
int abun = 0;
int perfect = 0;
int prime = 0;
int i = 1;
System.out.println("N Abundand Deficient Perfect Prime");
System.out.println("**************************************************************");
while (abun < value) {
if (isAbundantOrDeficient(i) == 1)
abun++;
else if (isAbundantOrDeficient(i) == 0)
def++;
if (isPerfect(i))
perfect++;
if (isPrime(i))
prime++;
System.out.println(i + " " + abun + " " + def + " " + perfect + " " + prime);
i++;
}
}
/* This function will display table until
* the user entered input perfect value
*/
private static void findPerfect(int value) {
int def = 0;
int abun = 0;
int perfect = 0;
int prime = 0;
int i = 1;
System.out.println("N Abundand Deficient Perfect Prime");
System.out.println("**************************************************************");
while (perfect < value) {
if (isAbundantOrDeficient(i) == 1)
abun++;
else if (isAbundantOrDeficient(i) == 0)
def++;
if (isPerfect(i))
perfect++;
if (isPrime(i))
prime++;
System.out.println(i + " " + abun + " " + def + " " + perfect + " " + prime);
i++;
}
}
/* This function will display table until
* the user entered input prime value
*/
private static void findPrime(int value) {
int def = 0;
int abun = 0;
int perfect = 0;
int prime = 0;
int i = 1;
System.out.println("N Abundand Deficient Perfect Prime");
System.out.println("**************************************************************");
while (prime < value) {
if (isAbundantOrDeficient(i) == 1)
abun++;
else if (isAbundantOrDeficient(i) == 0)
def++;
if (isPerfect(i))
perfect++;
if (isPrime(i))
prime++;
System.out.println(i + " " + abun + " " + def + " " + perfect + " " + prime);
i++;
}
}
/* This function will check whether the number is
* abundant or deficient or not
*/
private static int isAbundantOrDeficient(int val) {
int i = 1, sum = 0;
while (i < val) {
if (val % i == 0) {
sum += i;
}
i++;
}
if (sum > val)
return 1;
else if (sum < val)
return 0;
else
return -1;
}
/* This function will check whether the number is
* perfect or not
*/
private static boolean isPerfect(int num) {
//Declaring the variable
int sum = 0;
for (int i = 1; i < num - 1; i++) {
if (num % i == 0)
sum += i;
}
/* if the user entered number is equal to the
* sum value the number is perfect number
*/
if (sum == num)
return true;
else
return false;
}
/* This function will check whether the number is
* prime or not
*/
private static boolean isPrime(int number) {
// If the user entered number is '2' return true
if (number == 1)
return false;
for (int i = 2; i * i <= number; i++) {
if (number % i == 0)
return false;
}
return true;
}
}
________________
Output:
Enter input :limit = 10
N Abundand Deficient Perfect Prime
**************************************************************
1 0 1 0 0
2 0 2 0 1
3 0 3 0 2
4 0 4 0 2
5 0 5 0 3
6 0 5 1 3
7 0 6 1 4
8 0 7 1 4
9 0 8 1 4
10 0 9 1 4
__________________
Output#2
Enter input :abundant = 2
N Abundand Deficient Perfect Prime
**************************************************************
1 0 1 0 0
2 0 2 0 1
3 0 3 0 2
4 0 4 0 2
5 0 5 0 3
6 0 5 1 3
7 0 6 1 4
8 0 7 1 4
9 0 8 1 4
10 0 9 1 4
11 0 10 1 5
12 1 10 1 5
13 1 11 1 6
14 1 12 1 6
15 1 13 1 6
16 1 14 1 6
17 1 15 1 7
18 2 15 1 7
________________Thank YOu
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.