Exceptions • When the string parameter digits contains a character that does not represent a valid digit in the provided

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Exceptions • When the string parameter digits contains a character that does not represent a valid digit in the provided

Post by answerhappygod »

Exceptions When The String Parameter Digits Contains A Character That Does Not Represent A Valid Digit In The Provided 1
Exceptions When The String Parameter Digits Contains A Character That Does Not Represent A Valid Digit In The Provided 1 (167.06 KiB) Viewed 34 times
Exceptions • When the string parameter digits contains a character that does not represent a valid digit in the provided base, the code in this method should fail. • Copy/paste (into the provided answer field!) and rewrite the code to throw a RuntimeException, with appropriate text, for an error message when the code encounters an illegal digit. • For example, if base is 7 and digits is "10238401", then the code should fail because 8 is not a valid base-7 digit. • Assume that (1) the base itself is between 2 and 10 and (2) every character in digits is between 'o' and '9' • Note: DON'T use System.out.println() (Keep in mind, also, that you will be switching between numerical vs. character data types. For example, do not confuse the number 7 with the character '7' -- and so forth.) Code: public double basexConvert (String digits, int base) { double result = 0.0; int exponent = digits.length() - 1; for (char digit : digits.toCharArray ( ) ) result += (digit - '0') * Math.pow (base, exponent--); return result; }
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply