Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The digit sum of a positive integer can be found by summing the values of the individual digits in the number. For example the number

The digit sum of a positive integer can be found by summing the values of the individual digits in the number. For example the number 435 has a digit sum 4 + 3 + 5 = 12. In this question you will write two methods in the DigitSummarizer class shown below:

public class DigitSummarizer { /** Returns the single-digit "digital root" of n * Precondition: n is a positive number */ public static int digitalRoot(int n) { /* to be implemented in part (a) */ } /** Returns true if one of the digits of n is equal * to the digital root of n. * Precondition: n is a positive number */ public static boolean containsRoot(int n) { /* to be implemented in part (b) */ } /** Returns the sum of the digits in n * Precondition: n is a positive number */ public static int digitSum(int n) { /* implementation not shown */ } }

(a) Write the digitalRoot method which returns a single digit value named the digital root, which is calculated as described below.

The digital root of a positive integer is found by first calculating the digit sum of the number. If this value is a single digit (i.e. smaller than 10), then this is the digital root. If it is not, then the digit sum of this value should be calculated. This process should repeat until a single digit value is found.

For examples of digital root calculations consider the following table:

Value Digital Root Calculation
78 6 7 + 8 = 15 1 + 5 = 6
351 9 3 + 5 + 1 = 9
98875 1 9 + 8 + 8 + 7 + 5 = 37 3 + 7 = 10 1 + 0 = 1

A digitSum method has been provided which returns the digit sum of a positive integer parameter. Complete the digitalRoot method below. You must use the digitSum method appropriately to receive full credit.

public class DigitSummarizer {

/** Returns the single-digit "digital root" of n * Precondition: n is a positive number */ public static int digitalRoot(int n) { //CODE HERE }

(b) Write the containsRoot method. This method returns true if one of the digits of the positive integer parameter is equal to its digital root.

The following table shows some examples of the use of containsRoot.

n

digitalRoot(n)

containsRoot(n)

736

7

true

84343

4

true

212

5

false

Complete the containsRoot method below. Assume that digitalRoot works as specified, regardless of what you wrote in part (a). You must use the digitalRoot method appropriately to receive full credit.

public class DigitSummarizer { /** Returns true if one of the digits of n is equal * to the digital root of n. * Precondition: n is a positive number */ public static boolean containsRoot(int n) { //CODE HERE } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning VB.NET Databases

Authors: Thearon Willis

1st Edition

1594864217, 978-1594864216

More Books

Students also viewed these Databases questions