The program shows the run time error :
Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%d'
Modified code :
public class HelloWorld{
public static void main(String []args){
System.out.printf("Result is:"+mystery(-5,-9)+"\n"); // calling
function
System.out.printf("Result is:"+mystery(-4,-8)+"\n"); //
calling function
System.out.printf("Result is:"+mystery(-6,-7)+"\n"); //
calling function
}
public static int mystery(int a,int b)
{
if(b==1)
return a;
else
return a+mystery(a,b+1);
}
}
Outptput :

If the arguments are (-5,-9).
if(b==1)
It runs from b value -9 to 1, the value of b incremented until b==1
Therefore it runs for 11 times.
So the value is -5 is added to the varible, thus the values are like -5, -10, ........-55 (Here 10 times it calls)
return a+mystery(a,b+1);
Similarly
(-4,-8)
It runs for 10 times. The values are -4, -8, -12......
(-6,-7)
It runs for 9 times. The values are -4, -8, -12......
What does this program print? package javaapplication210; public class JavaApplication210 { public static void main(String[]args){ System.out.printf("Result...
import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...
Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a word:"); String word = kb.next(); String reverse = ""; for (int i=word.length()-1; i>=0; i--) reverse += word.charAt(i); boolean result = reverse.equalsIgnoreCase(word); if (result) System.out.println("The word " +word+ " is a Palindrome."); else System.out.println("The word " +word+ " is not a Palindrome."); } } Rewrite the program so that the main method is: public static void...
Fix this program
package chapter8_Test;
import java.util.Scanner;
public class Chapter8
{
public static void main(String[] args)
{
int[] matrix = {{1,2},{3,4},{5,6},{7,8}};
int columnChoice;
int columnTotal = 0;
double columnAverage = 0;
Scanner input = new Scanner(System.in);
System.out.print("Which column would you like to average (1 or
2)? ");
columnChoice = input.nextInt();
for(int row = 0;row < matrix.length;++row)
{
columnTotal += matrix[row][columnChoice];
}
columnAverage = columnTotal / (float) matrix.length;
System.out.printf("\nThe average of column %d is %.2f\n",
columnAverage, columnAverage);
}
}
This program...
18.12 What does the following program do? (Please explain briefly) 1 // Exercise 18.12: MysteryClass.java 2 public class MysteryClass { 3 public static int mystery(int[] array2, int size) 4 if (size == 1) { 5 return array2[0]; 6 } 7 else { 8 return array2[size - 1] + mystery(array2, size - 1); 9 } 10 } 11 12 public static void main(String[] args) { 13 int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 14 15...
What is the Java output? Part One: class Driver { public static void main(String[] args) { int a = 5; int b = 3; if (a < b || a * 2 < b) System.out.print(a - b); System.out.print(b + a); } } Part Two: class Driver { public static void main(String[] args) { int a = 5; int b = 8; if (a < b) if (a * 2 < b) System.out.print("foo"); else System.out.print("bar"); else System.out.print("buz"); } }
Question 2: Execute the following program. import java.util.Arrays; public class ArrayManipulations { public static void main(String[] args) { // sort doubleArray into ascending order char [] A = {'g', ', 'y', 'a', 'e','d' }; Arrays.sort( A); for (char value: A) System.out.printf("%2C", value); System.out.printf("\n"); char [] A Copy = new char[ 3 ]; System.arraycopy( A, 2, A Copy, 0, A Copy.length); for(char value: A Copy) System.out.printf("%2C", value); System.out.printf("\n"); int location = Arrays.binary Search(A Copy, 'y'); if(location >=0) System.out.printf("y Found at element...
What output is produced by the following program? public class MysteryNums public static void main(String[] args) - int x = 12; int y = x - 3; 3, sentence (y, x + y); . public static void sentence (int numi, int num2) { 4: System.out.println(num1 + " + num2);
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...
What are the errors in this ? public class Mystery { public static void main(String[] args) { double initialSavings = 10000; double interestRate = 0.05; double currSavings = 0; int i; System.out.println("\nAnnual savings 5 years: "); currSavings = initialSavings; for (i = 0, i < 5, ++i); currSavings = (currSavings * interestRate); System.out.println("$" + currSavings); } }
mystery (numi, num2) ? public static void main(String[] args) int numl = 7; int num2 = 13; int result = mystery (numi, num2); } public static int mystery (int firstNum, int secondNum) { firstNum = firstnym * 3; secondNum = secondnum * 2; return firstNum + secondNum; } numl: A num2: A int secondNum) firstNum = firstNum * 3; secondNum = secondNum * 2; return firstNum + secondNum; numl: A/ num2: result: A Previous Page Next Page