1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
densk [106]
4 years ago
11

write a JAVA program that has the following methods. a. int firstDigit(int n) , returning the first digit of the argument b. int

lastDigit(int n) , returning the last digit of the argument c. int digits(int n) , returning the number of digits of the argument For example, firstDigit(1729) is 1, last digit(1729) is 9, and digits(1729) is 4. d. a main method that
Computers and Technology
1 answer:
ladessa [460]4 years ago
7 0

Answer:

  1. public class Main {
  2.    public static void main(String[] args) {
  3.        System.out.println(firstDigit(1729));
  4.        System.out.println(lastDigit(1729));
  5.        System.out.println(digits(1729));
  6.    }
  7.    public static int firstDigit(int n){
  8.        String n_str = Integer.toString(n);
  9.        char firstChar = n_str.charAt(0);
  10.        String firstDigit = Character.toString(firstChar);
  11.        return Integer.parseInt(firstDigit);
  12.    }
  13.    public static int lastDigit(int n){
  14.        String n_str = Integer.toString(n);
  15.        char lastChar = n_str.charAt(n_str.length() - 1);
  16.        String lastDigit = Character.toString(lastChar);
  17.        return Integer.parseInt(lastDigit);
  18.    }
  19.    public static int digits(int n){
  20.        String n_str = Integer.toString(n);
  21.        return n_str.length();
  22.    }
  23. }

Explanation:

Firstly we create a method firstDigit that will return the first digit of the input number (Line 8 - 13). This method will convert the input number, n, to string using Integer toString method (Line 9) and assign it to n_stry variable. Next, use charAt method with 0 index to get the first character from n_str and assign it to firstChar variable (Line 10). Next, use Character toString method to convert the firstChar to string type (Line 11) because the parseInt method which is used in Line 12 will only work on String type but not char type. The function return the firstDigit with integer type (Line 12). The parseInt method has converted the string type firstDigit to integer.

The logical steps defined for lastDigit method are similar to firstDigit except that we need to use .length() method to get the length of the digit string and calculate the last index to get the last digit (Line 17).

The digits method (Line 22 -25) will convert the input number to string and then use .length() method to return the length of the number string which is equivalent of number of digit as output.  

You might be interested in
The font size on presentation slides should not be smaller than _____ -point.
fredd [130]
The font size should not be smaller than 24 points.
3 0
4 years ago
Read 2 more answers
An uniterruptible power supply
velikii [3]

Answer:

An uninterruptible power supply  is an electrical apparatus that provides emergency power to a load when the input power source or mains power fails.

Explanation:

Your welcome :) PLZ mark brainliest

4 0
3 years ago
Discuss about hard disk system​
aleksley [76]
The hard disk system
(Or most commonly known as hard drive) is the primary storage device on the computer.

Extra unneeded info: Heck, you can even have two computers in one depending on how you use the hard drive, and set the parameters and stuff like that
7 0
3 years ago
Jake is photographing his pet puppy. He wants to preview the image the camera will capture. What part of the camera that is insi
german

A. i think that is right

3 0
3 years ago
Read 2 more answers
Which of these identifies the intersection of row 16 and column D?
Alchen [17]
Your answer is C. the cell D16
6 0
3 years ago
Read 2 more answers
Other questions:
  • Brock wants to use several images on his hiking blog. However, the images are making his pages load very slowly. Explain to Broc
    6·1 answer
  • Before you start creating a database, you should first use paper to plan, test, and revise. True False
    7·2 answers
  • A(n) ______ notification is a message that initiates from the sending location without a request from the receiver. With a(n) __
    5·2 answers
  • Answer these two if u can❤️
    5·2 answers
  • The most important network device is a ____.
    5·1 answer
  • Problem 3. Consider the following recurrence, defined for n a power of 4 (for the time of some algorithm): T(n) = 3 if n = 1 2T(
    5·1 answer
  • Availability is an essential part of ________ security, and user behavior analysis and application analysis provide the data nee
    14·1 answer
  • Answer ASAP and I'll give brainliest.
    10·2 answers
  • someone please tell me if you watch drag race (rupauls drag race) I need someone to talk to about it ​
    12·1 answer
  • an early type of GUI is created, which includes windows, pop-ups, icons, and menus. What year did this happen? Please help!!! qu
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!