Question

JAVA: Write a method with the following header to return a string format to represent the...

JAVA:

Write a method with the following header to return a string format to represent the reverse order of the integer:

public static String reverse(int number)

For example, reverse(3456) returns 6543 and reverse(809340) returns 043908. Write a test program that prompts the user to enter an integer then displays its reversal.

Convert integer to string and print reverse of integer as string. Do not use built-in toString. Use loop.

Homework Answers

Answer #1
import java.util.Scanner;
public class ReverseInt {
    public static String reverseInt(int n){
        String result = "";
        while (n!=0){
            result += n%10;
            n = n / 10;
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter number:");
        int n = scan.nextInt();

        System.out.println("Reverse of "+n+" is "+reverseInt(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
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of...
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of String values and returns the length, in characters, of that word. The method should use the following header. public static int minLength(String[] array) Write a test program that prompts the user to enter ten strings, store them in an array and invokes this method to return the shortest length, and displays that value. (A near complete program can be found attached to the dropbox)
Write Java program Lab51.java which takes in a string from the user, converts it to an...
Write Java program Lab51.java which takes in a string from the user, converts it to an array of characters (char[] word) and calls the method: public static int countVowels(char[]) which returns the number of vowels in word. (You have to write countVowels(char[]) ).
Write a Java program that counts the number of alphabetic ('A' to 'Z' in both upper...
Write a Java program that counts the number of alphabetic ('A' to 'Z' in both upper and lower case) and digit ('1' to '3') characters in a String. The method header is as follows: public static int countNumAlphabetic ( String str ) { } Sample Output: countNumAlphabetic("mA13Th9zB86") returns 8
Write a Hex Viewer method with the following header: private static void viewHex(String filename) The method...
Write a Hex Viewer method with the following header: private static void viewHex(String filename) The method reads bytes from filename and displays them in hex representation. The output should be formatted as in the example below, i.e., each line consists of 8 pairs of hex numbers, then ‘|’, then another 8 pairs. Use Integer.toHexString() to convert a byte into a string representing the equivalent hex. Use a try statement to handle IOException and display a simple error message if an...
Write a Java program with a method public String replaceChar(String p, int k, char c) {...
Write a Java program with a method public String replaceChar(String p, int k, char c) { } that given a String p, an int k, and a char c, returns a String with the kth character replaced by c. Of course, 0<=k<=p.length()-1, otherwise raise an exception or error.
Please code in Java Write a recursive method that takes a string and return a string...
Please code in Java Write a recursive method that takes a string and return a string where every character appears twice. For example, if the string is “HELLO”, it will return “HHEELLOO”. Write a program to test it.
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
Write a function (java static method) that computes the maximum of two integer. maxOfTwo(1, 2) →...
Write a function (java static method) that computes the maximum of two integer. maxOfTwo(1, 2) → 2 maxOfTwo(2, 1) → 2 maxOfTwo(1, 1) → 1 **(to start use): public int maxOfTwo(int a, int b) { And then complete the function (java static method)MaxOfThree so that it returns the maximum (highest) of the three int values passed as the parameters. maxOfThree(1, 2, 3) → 3 maxOfThree(0, 2, 1) → 2 maxOfThree(88, 4, 5) → 88 **(to start use): public int maxOfThree(int...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity (sit, walk, jog, bike, swim) and duration in minutes (integer), and returns the estimated calories expended (double). Calories per minute for a 150 lb person (source): sit: 1.4 walk: 5.4 run: 13.0 bike: 6.8 swim: 8.7 If the input is sit 2, the output is 2.8 Hints: Use an if-else statement to determine the calories per minute for the given activity. Return the calories...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...