Question

Copy the program DigitPlay.java and implement the TBI (To Be Implemented) method named play(Result r). Your...

Copy the program DigitPlay.java and implement the TBI (To Be Implemented) method named play(Result r).

Your program cannot contain any import statements.

Your play(Result r) method cannot call any other methods.

The output of your program must match the following.

number: 5
      # of digits: 1
   smallest digit: 5
    largest digit: 5
      digit range: 0
        digit sum: 5
    digit average: 5.0
    digit product: 5

number: 42
      # of digits: 2
   smallest digit: 2
    largest digit: 4
      digit range: 2
        digit sum: 6
    digit average: 3.0
    digit product: 8

number: 127
      # of digits: 3
   smallest digit: 1
    largest digit: 7
      digit range: 6
        digit sum: 10
    digit average: 3.3333333333333335
    digit product: 14

number: 1024
      # of digits: 4
   smallest digit: 0
    largest digit: 4
      digit range: 4
        digit sum: 7
    digit average: 1.75
    digit product: 8 (no multiply by 0)

number: 32767
      # of digits: 5
   smallest digit: 2
    largest digit: 7
      digit range: 5
        digit sum: 25
    digit average: 5.0
    digit product: 1764

number: 2147483647
      # of digits: 10
   smallest digit: 1
    largest digit: 8
      digit range: 7
        digit sum: 46
    digit average: 4.6
    digit product: 903168

number: 9223372036854775807
      # of digits: 19
   smallest digit: 0
    largest digit: 9
      digit range: 9
        digit sum: 88
    digit average: 4.631578947368421
    digit product: 179233689600 (no multiply by 0)

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

Digitplay program:

/*
 * This Java program takes a positive integer of type long
 * and prints the following about its digits:
 *
 *     + the number of digits
 *     + the smallest digit
 *     + the largest digit
 *     + the range of the digits
 *     + the sum of the digits
 *     + the average of the digits (digit 0 is included)
 *     + the product of the digits (digit 0 is excluded)
 */

public class DigitPlay {

   public static final String INDENT = "\n   ";

   public static void main(String[] argv) {
      long[] data = { 5, 42, Byte.MAX_VALUE, 1024, Short.MAX_VALUE,
                      Integer.MAX_VALUE, Long.MAX_VALUE };
      for (int i = 0; i < data.length; i++)
         print(play(new Result(data[i])));
   }

   public static void print(Result r) {
      System.out.print("number: " + r.number +
                        INDENT + "   # of digits: " + r.ndigits +
                        INDENT + "smallest digit: " + r.min + 
                        INDENT + " largest digit: " + r.max + 
                        INDENT + "   digit range: " + r.range + 
                        INDENT + "     digit sum: " + r.sum + 
                        INDENT + " digit average: " + r.avg + 
                        INDENT + " digit product: " + r.product);
      if (r.has0) System.out.print(" (no multiply by 0)");
      System.out.println("\n");
   }

   static class Result {
      public long number;
      public int ndigits;
      public int min = 10;
      public int max = -1;
      public int range;
      public int sum;
      public long product = 1;
      public double avg;
      public boolean has0;
      public Result(long n) { number = n; }
   }

   /** 
    * TBI (To Be Implemented)...
    *
    * Using a number stored a Result object, this method fills in 
    * the following information about the digits of the number: 
    * # of digits; smallest digit; largest digit; digit range; 
    * digit sum; digit average; flag if number contains a 0;
    * digit product that excludes multiplying by 0 digits
    *
    * @param Result object r to fill in for r.number
    * @return the filled in Result object r
    */
   public static Result play(Result r) {
      return r;
   }

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

Solution:

/*
 * This Java program takes a positive integer of type long
 * and prints the following about its digits:
 *
 *     + the number of digits
 *     + the smallest digit
 *     + the largest digit
 *     + the range of the digits
 *     + the sum of the digits
 *     + the average of the digits (digit 0 is included)
 *     + the product of the digits (digit 0 is excluded)
 */
public class DigitPlay {

    public static final String INDENT = "
   ";

    public static void main(String[] argv) {
        long[] data = {5, 42, Byte.MAX_VALUE, 1024, Short.MAX_VALUE,
                Integer.MAX_VALUE, Long.MAX_VALUE};
        for (int i = 0; i < data.length; i++)
            print(play(new Result(data[i])));
    }

    public static void print(Result r) {
        System.out.print("number: " + r.number +
                INDENT + "   # of digits: " + r.ndigits +
                INDENT + "smallest digit: " + r.min +
                INDENT + " largest digit: " + r.max +
                INDENT + "   digit range: " + r.range +
                INDENT + "     digit sum: " + r.sum +
                INDENT + " digit average: " + r.avg +
                INDENT + " digit product: " + r.product);
        if (r.has0) System.out.print(" (no multiply by 0)");
        System.out.println("
");
    }

    static class Result {
        public long number;
        public int ndigits;
        public int min = 10;
        public int max = -1;
        public int range;
        public int sum;
        public long product = 1;
        public double avg;
        public boolean has0;

        public Result(long n) {
            number = n;
        }
    }

    /**
     * TBI (To Be Implemented)...
     * <p>
     * Using a number stored a Result object, this method fills in
     * the following information about the digits of the number:
     * # of digits; smallest digit; largest digit; digit range;
     * digit sum; digit average; flag if number contains a 0;
     * digit product that excludes multiplying by 0 digits
     * <p>
     * param Result object r to fill in for r.number
     * return the filled in Result object r
     */
    public static Result play(Result r) {

        long temp = r.number;
        int d1 = 0;
        long d2, sum = 0, product = 1;

        //To find number of digits
        while (temp != 0) {
            temp = temp / 10;
            d1 = d1 + 1;
        }
        r.ndigits = d1;
        temp = r.number;
        //To find minimum valued digit
        while (temp != 0) {
            d2 = temp % 10;
            if (d2 < r.min)
                r.min = (int) d2;
            temp = temp / 10;
        }
        temp = r.number;
        //To find maximum valued digit
        while (temp != 0) {
            d2 = temp % 10;
            if (d2 > r.max)
                r.max = (int) d2;
            temp = temp / 10;
        }
        //To find range
        r.range = r.max - r.min;
        temp = r.number;
        //To find sum of digits
        while (temp != 0) {
            d2 = temp % 10;
            sum = sum + d2;
            temp = temp / 10;
        }
        r.sum = (int) sum;
        temp = r.number;
        //To find product of digits
        while (temp != 0) {
            d2 = temp % 10;
            if (d2 != 0)
                product = product * d2;
            //To check if a particular digit is 0
            if (d2 == 0)
                r.has0 = true;
            temp = temp / 10;
        }
        r.product = product;
        //To find average of digits
        r.avg = (double) r.sum / r.ndigits;

        return r;
    }

}

Output:

Add a comment
Know the answer?
Add Answer to:
Copy the program DigitPlay.java and implement the TBI (To Be Implemented) method named play(Result r). Your...
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
  • Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit...

    Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit card number and determines whether it is valid or not. (Much of this assignment is taken from exercise 6.31 in the book) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits, and must start with: 4 for Visa cards 5 for Master cards 6 for Discover cards 37 for American Express cards The algorithm for determining...

  • Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program...

    Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program that works as described in the following scenario: The user enters a credit card number. The program displays whether the credit card number is valid or invalid. Here are two sample runs: Enter a credit card number as a long integer: 4388576018410707 4388576018410707 is valid Enter a credit card number as a long integer: 4388576018402626 4388576018402626 is invalid To check the validity of the...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

  • In this assignment, you are asked to implement a Java class named HugeInt for storing huge...

    In this assignment, you are asked to implement a Java class named HugeInt for storing huge integers and performing mathematical operations on them. This class must have a private variable of type LinkedList〈Byte〉 which stores digits of a huge integer and ten of the following public methods (choose ten methods out of 12): public void setValue (int value): gets an integer (like 1234) as input parameter and stores its digits in the linked list in the same order as they...

  • 3. (Dynamic Array) In C++, the largest int value is 2147483647. So. an integer larger than this cannot be stored and pr...

    3. (Dynamic Array) In C++, the largest int value is 2147483647. So. an integer larger than this cannot be stored and processed as an integer. Similarly, if the sum or product of two positive integers is greater than 2147483647, the result will be incorrect. One way to store and manipulate large integers is to store each individual digit of the number in an array. Your program must, at least, contain a function to read and store a number into an...

  • 1. (Sum the digits in an integer) Write a method that computes the sum of the...

    1. (Sum the digits in an integer) Write a method that computes the sum of the digits in an integer. Use the following method header: public static int sumDigits(long n) For example, sumDigits (234) returns 9 (2 + 3 + 4). (Hint: Use the % operator to extract digits, and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10(= 4). To remove 4 from 234, use 234 / 10(= 23)....

  • The main method of your calculator program has started to get a little messy. In this...

    The main method of your calculator program has started to get a little messy. In this assignment, you will clean it up some by moving some of your code into new methods. Methods allow you to organize your code, avoid repetition, and make aspects of your code easier to modify. While the calculator program is very simple, this assignment attempts to show you how larger, real world programs are structured. As a new programmer in a job, you will likely...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • This is the assignment..... Write a class DataSet that stores a number of values of type...

    This is the assignment..... Write a class DataSet that stores a number of values of type double. Provide a constructor public DataSet(int maxNumberOfValues) and a method public void addValue(double value) that add a value provided there is still room. Provide methods to compute the sum, average, maximum and minimum value. ​This is what I have, its suppose to be using arrays also the double smallest = Double.MAX_VALUE; and double largest = Double.MIN_VALUE;​ are there so I don't need to create...

  • Credit card numbers follow certain patterns. A credit card number must have between 13 and 16...

    Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. It must start with: 4 for Visa cards 5 for Master cards 37 for American Express cards 6 for Discover cards IBM proposed an algorithm for validating credit card numbers. The algorithm is used to determine if a card number is entered correctly or if a credit card is scanned correctly by a scanner. Almost all credit card numbers are generated following this...

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