Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MUST BE WRITTEN IN JAVA For each of the following code snippets there are multip

ID: 3589736 • Letter: M

Question

MUST BE WRITTEN IN JAVA

For each of the following code snippets there are multiple things wrong with the code, whether they be errorspreventing the code from compiling or errors preventing it from doing what is desired. Identify the errors andstate how they would be corrected.

(a)

Assume you have this method

public static void cubify(double x, int y)

{

double result = x*x*x + y*y*y; return result;

}

And in your main method you have the following:

int a = 3; double b = 2;

double c = cubify(a,b);

There are two errors in these snippets. What are they and how would they be fixed?

(b)

The following code snippet is supposed to print out the value of j every iteration of the inner loop, and the valueof i every iteration of the outer loop.

1. for (int i = 0; i < 10; i++)

2. {

3.    for (int j = 1; j < 10; j++)

4.    {

5.   System.out.printf("j is %d ",j);

6. System.out.printf("i is %i ",i);

7.    }

8.   }

9. System.out.println("Final value of i is " + i);

Please identify 3 errors and how they can be fixed

Next Question

This code is supposed to get two double values from the user and do the following:

-If x is in the range [5,10] set BASEBALL to the String starting with “Buy me ...”

-Otherwise, if x is in range (10,20) OR y equals 50, set BASEBALL to the String starting with “I actuallydo …”

-Otherwise, set BASEBALL to the String starting with “Heroes are …”

Remember that [x,y] means the range of values from x to y including x and y (inclusive), and (x,y) means therange of values from x to y not including x and y (exclusive).

1. final String BASEBALL = "Take me out to the ball game!";

2. Scanner kb = new Scanner(System.in);

3. double x = kb.nextInt();

4. double y = kb.nextDouble();

5. if (x > 5 && x < 10)

6.    BASEBALL = "Buy me some peanuts and cracker jacks";

7. else if ((x > 10 && x < 20) || y != 50)

8.    BASEBALL = "I actually do care if I never come back";

9. else

10.   BASEBALL = "Heroes are remembered, but legends never die";

11. }

There are five errors in this code. Identify them and explain how they can be fixed.

Next Question

Consider the program segment given below. Its output is:

(a)

int x=0;

for(int i=0; i<3; i++) { for( int j=i;j<3; j++) {

x = x + j;

}

}

System.out.println( x);

(b)

int x = 2;

int y = 5;

int result = 0;

result += x+++y;

System.out.println( x + " " + y + " " + result);

   (c)

                                 int x = 1357;

                                 int z = 0;

                                 for( int i=x; i>0; i=i/10) {

                                      z = z + (i%10);

                                 }

                               System.out.println(z);

Explanation / Answer

(a)

1)To The method cubify we are passing first value as integer and second value as double

but in method implementation we written first argument as double and second argument as integer which is wrong.The first argument must be integer and the second argument must be double

2)The method cubify will have to return the result to the caller (main method) but we declared as void.

code after correction

int a = 3; double b = 2;

double c = cubify(a,b);

  

}

public static double cubify(int x,double y)

{

double result = x*x*x + y*y*y;

return result;

}

________________

(b)

1)System.out.printf("i is %i ",i); we have to write this statement outside the inner for loop.

2)System.out.printf("i is %i ",i); Here in this statement to print the i value which is out integer type we have to use %d but nit %i

3)System.out.println("Final value of i is " + i); we wrote this statement after the outer fopr loop.But the scope of the i value is limited to the loop.So this is wrong.

After correction of code:

for (int i = 0; i < 10; i++)

{

for (int j = 1; j < 10; j++)

{

System.out.printf("j is %d ",j);

}

System.out.printf("i is %d ",i);

}

____________________

1)we cant declare the Sting as final because.If we declared the variable as final.we cant change the value.

final String BASEBALL = "Take me out to the ball game!";

2) else if ((x > 10 && x < 20) || y != 50)

In the program description it was mentioned that -- if x is in range (10,20) OR y equals 50, set BASEBALL to the String starting with “I actuallydo …

but we write as y!= 50 which is wrong

3)  [x,y] means the range of values from x to y including

but we declared as if (x > 5 && x < 10) which is wrong

4) double x = kb.nextInt(); In the description we mentioned that the two variables are of type double.

5) The variable BASEBALL is not final.So the variable name starts with small letter.

After correcting errors the code is

String baseball = "Take me out to the ball game!";

Scanner kb = new Scanner(System.in);

double x = kb.nextDouble();

double y = kb.nextDouble();

if (x >= 5 && x <= 10)

baseball = "Buy me some peanuts and cracker jacks";

else if ((x > 10 && x < 20) || y == 50)

baseball = "I actually do care if I never come back";

else

baseball = "Heroes are remembered, but legends never die";

________________

(a)

(a)

int x=0;

for(int i=0; i<3; i++) { for( int j=i;j<3; j++) {

x = x + j;

}

}

System.out.println( x);

Output:

8

________________

(b)

int x = 2;

int y = 5;

int result = 0;

result += x+++y;

System.out.println( x + " " + y + " " + result);

Output:

3 5 7

_________________

(c)

int x = 1357;

int z = 0;

for( int i=x; i>0; i=i/10) {

z = z + (i%10);

}

System.out.println(z);

Output:

16

_________________Thankk YOu

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote