Question

Exercise #1 write a method extractdigitcount().which is specified as below. the method extractdigitcount()will receive an int...

Exercise #1

  1. write a method extractdigitcount().which is specified as below.
  • the method extractdigitcount()will receive an int as argument; and
  • the method will return the count of all digit found in the given argument.
  • Sample outcome:

Example #1 given an int of 1200

Return value: 4

           Example #2 given an int of -1200

Return value: 4

           Example #3 given an int of -359

                Return value: 3

In java program

Homework Answers

Answer #1
//DigitCount.java
import java.util.Scanner;
public class DigitCount {
    static int extractdigitcount(int n){
        int count = 0;
        if(n<0){
            n = -1 * n;
        }
        while(n>0){
            count++;
            n = n/10;
        }
        return count;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n;
        System.out.print("Enter an integer: ");
        n = scanner.nextInt();
        System.out.println(extractdigitcount(n));
    }
}

Know the answer?
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
Similar Questions
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an argument and returns the sum of every other int from n down to 1. For example, the call sumEveryOther(10) should return 30, since 10 + 8 + 6 + 4 + 2 = 30. The call sumEveryOther(9) should return 25 since 9 + 7 + 5 + 3 + 1 = 25. Your method must use recursion.
Please code C# Write a method with an int return type that has two int parameters....
Please code C# Write a method with an int return type that has two int parameters. The method returns the larger parameter as an int. If neither is larger, the program returns -1. Call this method three times, once with the first argument larger, once with the second argument larger, and once with both arguments equal
Write a public static method name productAll that takes in 2 arguments int a, int b,...
Write a public static method name productAll that takes in 2 arguments int a, int b, and returns the product of all values between those two numbers inclusive. Remember a can be less than b, b might be less than a, or they may be equal. They can also be positive or negative. Example: productAll(2, 5); int returned by method: 120 Example: productAll(5, 2); int returned by method: 120 Example: productAll(3, 3); int returned by method: 9 Example: productAll(3, 5);...
Write a public static method name productAll that takes in 2 arguments int a, int b,...
Write a public static method name productAll that takes in 2 arguments int a, int b, and returns the product of all values between those two numbers inclusive. Remember a can be less than b, b might be less than a, or they may be equal. They can also be positive or negative. Example: productAll(2, 5); int returned by method: 120 Example: productAll(5, 2); int returned by method: 120 Example: productAll(3, 3); int returned by method: 9 Example: productAll(3, 5);...
(in java) a. Include a good comment when you write the method described below: Write a...
(in java) a. Include a good comment when you write the method described below: Write a method factorial which receives a positive integer n and calculates n! (n factorial), defined as follows: n!=1*2*…*(n-1)*n. For example, 5! = 1*2*3*4*5 = 120. The method should return this value to the calling program. b. Write a driver program (main method) which will read an integer from the console, and pass it to the method to calculate its factorial. The main method then prints...
Java Programing Exercise #3: Write a Java class that implements a static method – SortNumbers(int… numbers)...
Java Programing Exercise #3: Write a Java class that implements a static method – SortNumbers(int… numbers) with variable number of arguments. The method should be called with different numbers of parameters and does arrange the numbers in descending order. Call the method within main method of the driver classand display the results.
Write a recursive method body for the method whose contract is given below. Note that n...
Write a recursive method body for the method whose contract is given below. Note that n is a restores-mode parameter. As an example, if n = 314, the method should return 1. You can only use the NaturalNumber kernel methods multiplyBy10, divideBy10, and isZero. /** * Reports the minimum (i.e., smallest) digit of n when it is * written in ordinary decimal notation. * ... * @ensures * minDigit = [the minimum digit of n] */ private static int minDigit(NaturalNumber...
/** * 1. Write the method plusTwo(). * * Your method will take 2 int arrays...
/** * 1. Write the method plusTwo(). * * Your method will take 2 int arrays as parameters. * Each will have a length of 2. Your method must * create and return a new array with a length of 4, * containing all their elements. * * Here are some examples: * plusTwo({1, 2}, {3, 4}) returns {1, 2, 3, 4} * plusTwo({4, 4}, {2, 2}) returns {4, 4, 2, 2} * plusTwo({9, 2}, {3, 4}) returns {9, 2,...
java •Write a method product(int x, int y) which returns the product of multiplying x *...
java •Write a method product(int x, int y) which returns the product of multiplying x * y but doing it recursively. Recall that multiplication can be implemented as repeated addition. For example, 4 * 7 is the same as adding 7, 4 times: 7 + 7 + 7 + 7. •Write a main method that asks for two numbers, and returns their product by calling the product method. Compile and test your code in NetBeans and then on Hackerrank
Write a method name maxElement, which returns the largest value in an array that is passed...
Write a method name maxElement, which returns the largest value in an array that is passed as an argument. The method must use recursion to find the largest element. Demonstrate the method in a program. Write the JAVA CODE on the paper that is provided. (Put your name on every piece of paper) Use the following array to test the method. Int [] numbers = {2,12,1999,99,100,4,7,300} PROGRAMMING LANGUAGE- JAVA. Please answer the question ASAP. Thanks in advance!