Question

18.12 What does the following program do? (Please explain briefly) 1 // Exercise 18.12: MysteryClass.java 2...

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 int result = mystery(array, array.length);

16 System.out.printf(“Result is: %d%n”, result);

17 }

18 }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

There are some errors in your code.

1) First the code didn't have curly brackets "{ }" over the method mystery().

2) There is an error in the print statement in main method.

After debugging the above errors the output of the code is " The sum of first 10 natural numbers."

Code tracing(Explanation):

  • First the program starts execution with main method, in main method there is an array declared with 10 elements.
  • From main method there is a method call to mystery method with values array and it's size i.e, mystery(array, array.length)
  • mystery() method returns first element of the array if size of the array is 1, else it adds all the elements recursively and return its sum.
  • The sum of the first 10 elements is 1+2+3+4+5+6+7+8+9+10 = 55.
  • And this sum is returned by mystery method to main method, and main method prints this value and the program is terminated.

​​​​​​​Modified code:

public class MysteryClass { //Mystery class
   public static int mystery(int[] array2, int size){ //mystery method
       if (size == 1) { //returns first elements if size is 1
           return array2[0];
       }
       else { //returns sum of the array
           return array2[size - 1] + mystery(array2, size - 1);
       }
   }
   public static void main(String[] args) { //starts execution from here
       int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //array declaration
       int result = mystery(array, array.length); //method calling
       System.out.println("Result is: "+result); //printing result
   }
}

Execution:

Add a comment
Know the answer?
Add Answer to:
18.12 What does the following program do? (Please explain briefly) 1 // Exercise 18.12: MysteryClass.java 2...
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
  • What does this program print? package javaapplication210; public class JavaApplication210 { public static void main(String[]args){ System.out.printf("Result...

    What does this program print? package javaapplication210; public class JavaApplication210 { public static void main(String[]args){ System.out.printf("Result is: % d/n", mystery (-5, -9));//System.out.printf("Result is: % d/n", mystery (-4, -8));//System.out.printf("Result is: % d/n", mystery (-6, -7));//} public static int mystery(int a, int b) { if(b - 1) returns a; else return a + mystery(a, b + 1); } }

  • 2. Write a counter controlled loop to solve the following problems. Each one will involve an...

    2. Write a counter controlled loop to solve the following problems. Each one will involve an array (MinMax.java) Read in 25 ints from the keyboard, and store them in an array. Then, find the maximum and minimum values in such an array, and display them on the screen. public class Array-Assignment { public static void main(String [] args) {     int [] x = new int[3];     int [] y = {3, 5, 9, 2};     x[2] = y[3];    ...

  • Can someone please explain this piece of java code line by line as to what they...

    Can someone please explain this piece of java code line by line as to what they are doing and the purpose of each line (you can put it in the code comments). Code: import java.util.*; public class Array { private Integer[] array; // NOTE: Integer is an Object. Array() {     super();     array = new Integer[0]; } Array(Array other) {     super();     array = other.array.clone(); // NOTE: All arrays can be cloned. } void add(int value) {    ...

  • Computer Science - Java Explain the code step by step (in detailed order). Also explain what...

    Computer Science - Java Explain the code step by step (in detailed order). Also explain what the program required. Thanks 7 import java.io.File; 8 import java.util.Scanner; 9 import java.util.Arrays; 10 import java.io.FileNotFoundException; 12 public class insertionSort 13 14 public static void insertionSort (double array]) 15 int n -array.length; for (int j-1; j < n; j++) 17 18 19 20 21 double key - array[j]; int i-_1 while ( (i > -1) && ( array [i] > key array [i+1] -...

  • I’m giving you code for a Class called GenericArray, which is an array that takes a...

    I’m giving you code for a Class called GenericArray, which is an array that takes a generic object type. As usual this is a C# program, make a new console application, and for now you can stick all of this in one big file. And a program that will do some basic stuff with it Generic Array List What I want you to do today: Create methods for the GenericArray, Easy(ish): public void Append (T, value) { // this should...

  • Question 2: Execute the following program. import java.util.Arrays; public class ArrayManipulations { public static void main(String[]...

    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...

  • 1. What is the height of a full binary search tree that contains 63 nodes 2....

    1. What is the height of a full binary search tree that contains 63 nodes 2. What is the output of this code? Describe what this recursive code does. public class Driver {    public static void main (String[ ] args)   {        String text = “what do I do”;        System.out.println(Mystery.task(text));   } } public class Mystery {    public static int task(String exp)   {         if(exp.equals(“”)           return 0;        else           return 1 + task(exp.substring(1));    } } //from the Java 7 API documentation: public String substring(int...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • Write java class “BinarySearch” that uses recursion to find the index of a target value in...

    Write java class “BinarySearch” that uses recursion to find the index of a target value in an ascending sorted array. If not found, the result is -1. The array has to be unsorted before you sort it. Use “ BinarySearchDriver.java” to drive the “BinarySearch” class /************************************************************* * BinarySearchDriver.java * Student Name * *. *************************************************************/ public class BinarySearchDriver { public static void main(String[] args) {     int[] array = new int[] {55, 88, 33, 5, 8, 12, 16, 23, 45}; /unsorted...

  • hi I need to understand how the out put become -4 in first question and out...

    hi I need to understand how the out put become -4 in first question and out put 310 for the second question please can you explain to me thanks a lot 1.(ch12. Sc16. P. 820. loop) Consider the following code: import java.util.*; public class MyLoopOneSGTest{    public static void main(String[] args){      int w = 3;      System.out.println(myLoop(w));    }    public static int myLoop(int n){       int x = 1;       for (int i = 1; i < n; i++){          x = x - i;       }...

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