Write a method int largestDigit (int n) that computes and returns the largest digit of its parameter integer n. For example, when called with the argument 6307362, this method would return 7, and when called with the argument -302110132, return 3. Your method has to work correctly for any integer between minus one billion and one billion, inclusive. (Hint: remember that for given positive integer n, the expression n % 10 gives its last digit, and n / 10 gives all its other digits.)

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

public class LargestDigit {

public static int largestDigit(int n) {
    // Convert number to a String
    String s = String.valueOf(Math.abs(n));
    int largest = 0;
    // Iterate over the individual elements of the String
    for (int i = 0; i < s.length(); i++) {
      // Check each String element to identify if it is the largest integer
      int current = Integer.parseInt(s.substring(i,i+1));...

By purchasing this solution you'll be able to access the following files:

for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats