Question

Problem 14)(Java) The following method contains a single error. Describe the error and show how to...

Problem 14)(Java) The following method contains a single error.
Describe the error and show how to fix it.

private static int findLastDigit(int n)
{
  if (n > 0)
    return n % 10;
  else if (n < 0)
    return -n % 10;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Error:
--------
method does not handle the case of when n is zero.
so, the code gives compilation error, because not all code paths are returning a value.

Fix:
------
return 0, if n is zero.

Fixed code:
------------
private static int findLastDigit(int n) {
    if (n > 0)
        return n % 10;
    else if (n < 0)
        return -n % 10;
    return 0;   // Fix here. return 0, if n is zero.
}
Add a comment
Know the answer?
Add Answer to:
Problem 14)(Java) The following method contains a single error. Describe the error and show how to...
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
  • Problem 5) Simplify the code for the following method as much as you can.(Java) public static...

    Problem 5) Simplify the code for the following method as much as you can.(Java) public static int inTheInterval(int n) { if (n < 0) return -1; else if (0 <= n && n <= 1) return 0; else if (n > 1) return 1; return 42; // we never get here }

  • (20 pts) Fill in the missing code: This recursive method returns “even” if the length of...

    (20 pts) Fill in the missing code: This recursive method returns “even” if the length of a give String is even, and “odd” if the length of the String is odd. public static String foo(String s) { if (s.length() ==0)    return “even”; else if (s.length() = = 1)    return “odd”; else     //your code goes here } (40 pts) You coded the following in the file Test.java : System.out.println( foo(5)); //more code here public static int foo(int n)...

  • I was presented with this problem: Create a Java class MonthNames with a single static method...

    I was presented with this problem: Create a Java class MonthNames with a single static method getMonthName which Takes a single integer corresponding to a month of the year, 1 representing January through 12 representing December, and Returns a string for the name of the month. This is what I have so far public class MonthNames {    public static String getMonthName (int numMonth) {        return "month"; }        public static void main (String [] args) {...

  • The following class contains several errors that violate the rules of Java: class Part ( private...

    The following class contains several errors that violate the rules of Java: class Part ( private int number: private String name: private int quantity: public Part (int number, String name, int quantity) { this.number = number: this.name = name: this quantity = quantity: } public Part (int number, string name) { this (number, name, 0): } public Part () { this (0, 'No name'): } public void decreaseQuantity(int amount) { quantity = -amount: } public int getName () { return...

  • JAVA getting the following errors: Project4.java:93: error: ']' expected arr[index] = newVal; // LEAVE THIS HERE....

    JAVA getting the following errors: Project4.java:93: error: ']' expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:93: error: ';' expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:93: error: <identifier> expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:94: error: illegal start of type return true; ^ Project4.java:98: error: class, interface, or enum expected static int bSearch(int[] a, int count, int key) ^ Project4.java:101: error: class, interface, or...

  • in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**...

    in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**      * @param args the command line arguments      */         public static void main(String[] args) {       Scanner input=new Scanner(System.in);          Scanner input2=new Scanner(System.in);                             UNOCard c=new UNOCard ();                UNOCard D=new UNOCard ();                 Queue Q=new Queue();                           listplayer ll=new listplayer();                           System.out.println("Enter Players Name :\n Click STOP To Start Game..");        String Name = input.nextLine();...

  • Please help to resolve my error. This Java code is supposed to evaluate one line equation...

    Please help to resolve my error. This Java code is supposed to evaluate one line equation of the form X Operator =Z where X is any name of a variable and Y and Z are integers By user in equation solver window. Currently the code executes expressions such as Y Operator(+,-,*,/) Z package Equations; /** * This program evaluates arithmetic expressions */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.StringTokenizer; public class EquationSolver extends JFrame implements ActionListener { private JTextField...

  • Java, how would i do this public static void main(String[] args) { int n = 3;...

    Java, how would i do this public static void main(String[] args) { int n = 3; int result; result = factorial(n); + public static int factorial(int n) public static int factorial(int n) n = 3 { returns 3* 2 * 1 { if (n == 1) return n; else return (n * factorial(n-1)); if (n == 1) return n; else return (3 * factorial(3-1)); ܢܟ } public static int factorial(int n) n = 2 public static int factorial(int n) returns...

  • JAVA: Excerpt B.A: Complete the implementation of the following method. The purpose of this method is...

    JAVA: Excerpt B.A: Complete the implementation of the following method. The purpose of this method is to return true if and only if all the elements in the array are x. In other words, all elements in the array are the value x (replace -a- with right answer). public static -a- allSame(int[] nums, int x){ for (int i = 0; i < -a-; i++){ if (nums[i] -a- x){ return -a-; } } return -a- } Excerpt B.B: What is printed...

  • Java Programming Language: Find the compiling error in the following code segment. Explain what the problem...

    Java Programming Language: Find the compiling error in the following code segment. Explain what the problem is and how to fix it. public class Mark { private float value;        public float Mark(float startValue) {        value = startValue; } }

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