Question

In JAVA using Netbeans

Create a method called sumLastValues that will produce the sum of the last two values in an int variable. Your method should take as a parameter the integer to extract the last two values from. Your method should check to make sure that the int has at least two digits in it. If it does not your method should simply return-1 If it does extract the last two digits return their sum NOTE You are not allowed to convert the number to a string in any way. You must compute the last two values using the math operators. Example Given the number 123 as input your method should return 5 because the last two digits are 3+ 2 n,MSJC son, MsJC

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class SumLastValues {

    public static int sumLastValues(int n) {
        int n1 = n % 10;
        int n2 = (n / 10) % 10;
        if(n2 == 0) {
            return -1;
        } else {
            return n1 + n2;
        }
    }

    public static void main(String[] args) {
        System.out.println(sumLastValues(123));
    }

}
Add a comment
Know the answer?
Add Answer to:
In JAVA using Netbeans Create a method called sumLastValues that will produce the sum of the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT