Question

PunNumber class returns interesting (or fun) information about the integer representation of itself The PunNumber object can return the number of digits it has, the sum of its digits, whether or not its prime number, and whether or not its a perfect number. An incomplete definition of the FunNumbe class appears below. creates and manipulates a number, which is funl public class FunNumber Constructs a FunNumber representation of the integer n. Assumes n0 @param n the FunNumber value public FunNumber (int n) f. . ) Constructs a FunNumber representation of the String n. Assumesno. oparam n the string to be represented by this FunNumber public FunMumber (String n) f. .. Returns the number of digits in this FunNumber ereturn the number of digits in this FunNumber public int numDigits) / Code goes here
media%2Fe33%2Fe33a43d1-b461-4fe3-9bfc-a1
media%2F3b2%2F3b28fce9-7508-43e1-8137-01
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//I have combined both class and main in one file ,, u can separate that ..

public class FunNumber

{

public FunNumber(int n)

{

myNum = n;

}

public FunNumber(String n)

{

myNum=Integer.parseInt(n);

}

public int numDigits()

{

int rem ,n = myNum,count=0;

do

{

rem = myNum/10;

++count;

n /=10;

}while(n>0);

return count;

}

public int sumDigits()

{

int rem ,n = myNum,sum=0;

do

{

rem = myNum/10;

sum+=rem;

n /=10;

}while(n>1);

return sum;

}

public boolean isPerfect()

{

int sum = 0;

for(int i = 1; i < myNum;i++)

{

if(myNum%i == 0)

{

sum+=i;

}

}

if(sum == myNum)

return true;

else

return false;

}

public FunNumber reverseNum()

{

int num = myNum,reverse_no=0,rem;

  

//System.out.printf("Num in reverse fun= %d\n",myNum);

do

{

rem = num%10;

reverse_no = reverse_no*10+rem;

//System.out.printf("reverse_no = %d\n",reverse_no);

num/=10;

}while(num>0);

myNum = reverse_no;

return this;

}

public String toString()

{

int rem,num = myNum;

//System.out.printf("myNum= %d\n",num);

String str = new String();

do

{

rem = num%10;

str+=rem;

num/=10;

}while(num>0);

//reverse a string as number 123 will be extracted as 321 from above code

String revStr = new String();

for(int i = str.length()-1; i >=0 ;i--)

{

revStr +=str.charAt(i);

}

//System.out.printf("toStr= %s\n",revStr);

return revStr;

}

private int myNum;

public static void main(String[] args)

{

//test number of digits

FunNumber funny = new FunNumber(123);

System.out.printf("Number of digits in number %s = %d\n",funny.toString(),funny.numDigits());

//test perfect number

FunNumber funny1 = new FunNumber(496);

if(funny1.isPerfect())

System.out.printf("Number %s is perfect number\n",funny1.toString());

else

System.out.printf("Number %s is not perfect number\n",funny1.toString());

//reverse a number funny

String s = funny.toString();

FunNumber funnyRev = funny.reverseNum();

System.out.printf("Reverse of a number %s is %s\n",s,funnyRev.toString());

//test for assigning string to number and print number using toString()

  

FunNumber funnyStr = new FunNumber("23456");

s = funnyStr.toString();

System.out.printf("Number of digits in number %s is = %d\n",s,funnyStr.numDigits());

  

}

}

----------------------------------------------------

//output1
Number of digits in number 8976 = 4
Number 12 is not perfect number
Reverse of a number 8976 is 6798
Number of digits in number 1234789 is = 7
//output2:
Number of digits in number 123 = 3
Number 496 is perfect number
Reverse of a number 123 is 321
Number of digits in number 23456 is = 5

Add a comment
Know the answer?
Add Answer to:
PunNumber class returns interesting (or fun) information about the integer representation of itself The PunNumber object...
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
  • create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s)...

    create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s) to return the max element in the queu. min(SinglyLinkedQueue<Integer> s) to return the min element in the queue. sum(SinglyLInkedQueue<Integer> s) to return the sum of elements in the queu. median(SinglyLinkedQueue<Integer> s) to return the median of elements in the queue. split(SinglyLinkedQueue<Integer> s) to separate the SinglyLinkedQueue into two ArrayQueues based on whether the element values are even or odd. package Stack_and_Queue; import java.util.Iterator; import...

  • Course.java import java.io.Serializable; /** * Represents a course that might be taken by a student. *...

    Course.java import java.io.Serializable; /** * Represents a course that might be taken by a student. * */ public class Course implements Serializable { private String prefix; private int number; private String title; private String grade; /** * Constructs the course with the specified information. * * @param prefix the prefix of the course designation * @param number the number of the course designation * @param title the title of the course * @param grade the grade received for the course...

  • 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...

  • Restrictions: You may not change any of the fields, nor the constructor, nor the insertAtFront method....

    Restrictions: You may not change any of the fields, nor the constructor, nor the insertAtFront method. As usual, you may not modify the method headers given to you in any way nor may you change the name of the class or the package. You must use recursion to solve the problems. Your code may not contain any loops. Functions that have loops will receive 0 points package hw8; import java.util.NoSuchElementException; public class MyList<Item> { private class MyListNode { public Item...

  • Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program...

    Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program named GetIDAndAge that continually prompts the user for an ID number and an age until a terminal 0 is entered for both. If the ID and age are both valid, display the message ID and Age OK. Throw a DataEntryException if the ID is not in the range of valid ID numbers (0 through 999), or if the age is not in the range...

  • Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of...

    Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of the peek, isEmpty, size, and toString methods. See Base_A06Q1.java for a starting place and a description of these methods. Here is the base given: /** * Write a description of the program here. * * @author Lewis et al., (your name) * @version (program version) */ import java.util.Iterator; public class Base_A06Q1 { /** * Program entry point for stack testing. * @param args Argument...

  • There is a data structure called a drop-out stack that behaves like a stack in every...

    There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...

  • DESCRIPTION: The range of integers that can be represented in Java using a primitive data type is...

    DESCRIPTION: The range of integers that can be represented in Java using a primitive data type is only from-263 to 263-1. What if we need to manipulate integer values beyond this range? In this assignment you will write a HugeInteger class which is able to represent arbitrar- ily large integer numbers. This class must implement arithmetic operations on integers such as addition, subtraction, multiplication, division and comparison. You have to implement this class without using Java predefined classes, unless specified...

  • Classes A Customer Points class maintains information about a customer. This information is the customer name...

    Classes A Customer Points class maintains information about a customer. This information is the customer name (entire name as a String) and the amount of points (as an int) the customer has accrued. Methods: Constructor: Receives a name and sets the name field to the received name and sets the points field to 0 (zero). getName: Returns the customer's name getPoints: Returns the customer's current points toString: Returns a String representation of the current state in the form-name points, i.e....

  • Given code: /** * Provides some methods to manipulate text * */ public class LoopyText {...

    Given code: /** * Provides some methods to manipulate text * */ public class LoopyText { private String text;    /** * Creates a LoopyText object with the given text * @param theText the text for this LoopyText */ public LoopyText(String theText) { text = theText; }    //Your methods here } Given tester code: /** * Tests the methods of LoopyText. * @author Kathleen O'Brien */ public class LoopyTextTester { public static void main(String[] args) { LoopyText loopy =...

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