Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

6. Have you used solid reasoning in your argument?

Answered: 1 week ago