Question

Write java code to create 3 methods that accomplish the below concepts according to simple number...

Write java code to create 3 methods that accomplish the below concepts according to simple number theory.

1. Create a file PositiveInteger.java that houses a class called PositiveInteger

2. This class should have one single instance variable called num

This is what I have for the constructor: public PositiveInteger(int number) { num = number; }

The three methods should start with the italicized sections shown below to accomplish the corresponding tasks:

1. public boolean isPerfect () - A number is said to be perfect if it is equal to the sum of all its factors (for obvious reasons the list of factors being considered does not include the number itself). 6 = 3 + 2 + 1, hence 6 is perfect.

2. public boolean isAbundant() - A number is considered to be abundant if the sum of its factors (aside from the number) is greater than the number itself. For example, 12 is abundant since 1+2+3+4+6 = 16 > 12. However, a number like 15, where the sum of the factors is 1 + 3 + 5 = 9, is not abundant

3. public boolean isNarcissistic() - A positive integer is called a narcissistic number if it is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is narcissistic because 13 + 53 + 33 = 1 + 125 + 27 = 153.

Any java code help would be appreciated!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:-

//import for pow()
import java.lang.Math.*;
  
//create class
public class PositiveInteger{
int num;
//create constructor
public PositiveInteger(int number)
{
num = number;
}
//isPerfect() method find all multiples add them all except the number then
//compare with number
public boolean isPerfect()
{ int sum=0;
for(int i=1;i*i<num;i++){
if(num==i*i)
sum+=i;
else if(num%i==0)
{
sum += i+(num/i);
}
}

if(num==(sum-num))
return true;

return false;

}

//isAbundant() method find all multiples add them all except the number then
//compare with number

public boolean isAbundant()
{ int sum=0;
for(int i=1;i*i<num;i++){
if(num==i*i)
sum+=i;
else if(num%i==0)
{
sum += i+(num/i);
}
}

if(num<(sum-num))
return true;

return false;

}


//isPerfect() method count the number of digits then
//take digit one by one calculate power and to a variable
//then compare variable with number
public boolean isNarcissitic()
{int sum=0;
int temp=num,rem,cnt=0;
while(temp>=1){
temp=temp/10;
cnt++;
}
temp=num;
while(temp>=1){
rem=temp%10;
temp=temp/10;
sum=sum + (int)Math.pow(rem,cnt);
}

if(num==sum)
return true;

return false;

}

//for calling the above functions
public static void main(String []args){
  
PositiveInteger a = new PositiveInteger(6);
PositiveInteger b = new PositiveInteger(12);
PositiveInteger c = new PositiveInteger(153);
//print the output
System.out.println(a.isPerfect());
System.out.println(b.isAbundant());
System.out.println(c.isNarcissitic());
}
}

Please like the answer if it is helpful to you.

Add a comment
Know the answer?
Add Answer to:
Write java code to create 3 methods that accomplish the below concepts according to simple number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm having trouble making a code for this problem, my class is working with java in...

    I'm having trouble making a code for this problem, my class is working with java in while loops and for loops. Write a method that returns true if an integer parameter n is a perfect number, false otherwise. (recall a perfect number is a number whose sum of its proper factors is equal to itself) (Precondition: n >= 1 .. means you are to presume n is positive) : EXAMPLE: 6 is a perfect number 1 2 3 equals 6...

  • Question 3 (10 marks) An integer number is said to be a perfect number if its...

    Question 3 (10 marks) An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because its factors, except itself, are 3, 2, and 1, and 6 = 3+2+1, but 12 is not a perfect number because its factors, except itself, are 6, 4, 3, 2, and 1, but 12 # 6+4+3+2+1. First. Write a C function, called isperfect, that...

  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • Write a JAVA code to do the following: create a class and add methods to count...

    Write a JAVA code to do the following: create a class and add methods to count the number of primitive fields, number of methods with primitive return types, a method that will also return the number of primitive in a given parameter. they are: public int getNumberOfPrimitiveMethods(Class host) public int getNumberOfPrimitiveFields(Class host) public int getNumberOfPrimitiveParameters(Method host) also public int getNumberOfPrivateMethods (Class host) public int getNumberOfPublicMethods (Class host)

  • I am given an input file, P1input.txt and I have to write code to find the...

    I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 {    public static...

  • Please do both parts (in Java); thanks! An Armstrong number of three digits is an integer...

    Please do both parts (in Java); thanks! An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3^3 + 7^3 + 1^3 = 371. Draw the flowchart and write a Java code to find ALL Armstrong number in the range of 0 and 999. You should write your code in two ways: (Yes as if you are...

  • Please paste your code and a screenshot of your output! 1. An integer n is divisible...

    Please paste your code and a screenshot of your output! 1. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program to determine whether or not the following numbers are divisible by 9: n= 154368 n 621594 n-123456 2. A number is said to be perfect if the sum of its divisors (except for itself) is equal to itself. For example, 6 is a perfect number because the sum of...

  • Bottom section is the what they expect in the code in java Create a class in...

    Bottom section is the what they expect in the code in java Create a class in JobApplicant.java that holds data about a job applicant. Include a name, a phone number, and four Boolean fields that represent whether the applicant is skilled in each of the following areas: word processing, spreadsheets, databases, and graphics. Include a constructor that accepts values for each of the fields. Also include a get method for each field. The get method should be the field name...

  • I need this code in java. Loops do just what they sound like they should -...

    I need this code in java. Loops do just what they sound like they should - they perform a statement again and again until a condition is fulfilled. For this lab you will be practicing using loops, specifically a for loop. The for loop takes the form of: for(/*variable initialization*/;/*stop condition*/; /*increment*/){ //statements to be done multiple times } Task Write an application that displays every perfect number from 2 through 1,000. A perfect number is one that equals the...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT