Question

In: Computer Science

Find the error in each of the following code segments and explain how to correct it....

Find the error in each of the following code segments and explain how to correct it.

  1. x = 1;

while ( x <= 10 );

x++;

}

  1. for ( y = .1; y != 1.0; y += .1

System.out.println( y );

  1. switch ( n ) {

case 1:

System.out.println( “The number is 1” );

case 2:

System.out.println( “The number is 2” );

break;

default:

System.out.println( “The number is not 1 or 2” );

break;

}

  1. The following code should print the values 1 to 10.

n = 1;  

while ( n < 10 )

System.out.println( n++ );

Solutions

Expert Solution

x = 1;

E1 - while ( x <= 10 ); //There shouldn't be semi colon after while loop
Correction - while (x<=10)

E2 - //there was no opening bracket for while loop
Correction - {   

  
x++;

}


E3 - for ( y = .1; y != 1.0; y += .1 //Here, initialize y as float, and close the for loop bracket

Correction - for (float y=.1; y!=1.0; y+=.1)

E4 - for loop flower bracket missing
Correction - {

System.out.println( y );

switch ( n ) {

case 1:

System.out.println( “The number is 1” );
E5 - //break statement is missing
Correction - break;

case 2:

System.out.println( “The number is 2” );

break;

default:

System.out.println( “The number is not 1 or 2” );

break;

}
}

//The following code should print the values 1 to 10.

n = 1;

while ( n < 10 )
E6 - Flower bracket missing
Correction - {

System.out.println( n++ );
}

Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

Related Solutions

Find and correct at least two errors in each of the following segments of code. a)...
Find and correct at least two errors in each of the following segments of code. a) final int ARRAY_SIZE = 5; ARRAY_SIZE = 10; int[] table = new int[ARRAY_SIZE]; for (int x = 0; x <= ARRAY_SIZE; x++) table[x] = 99; b) String[] names = {“Mina” , “George”}; int totalLength = 0; for (int i = 0; i <= names.length() ; i++) totalLength += names[i].length; c) String[] words = “Hello”,“Goodbye”; System.out.println(words.toUpperCase()); d) public class MyClass { private int x; private...
explain how to legally correct an error a nurse makes in documentation
explain how to legally correct an error a nurse makes in documentation
What is the output from each of the following segments of C++ code? Record the output...
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 8; j > 3; j -= 2)         cout << setw(5) << j;     Answer:      2. int sum = 5;    for (int k = -4; k <= 1; k++)         sum = sum...
Each of the following statements contains an error. Describe each error and explain why the statement...
Each of the following statements contains an error. Describe each error and explain why the statement is wrong. (a) If the residuals are all negative, this implies that there is a negative relationship between the response variable and the explanatory variable. (b) A strong negative relationship does not imply that there is an association between the explanatory variable and the response variable. (c) A lurking variable is always something that can be measured.
Explain and give an example of how to find the Margin of Error of a Confidence...
Explain and give an example of how to find the Margin of Error of a Confidence Interval for a population mean.
Problem with code. The fixed code would be greatly appreciated! Error messages: PlayerMoveDungeon2.java:21: error: cannot find...
Problem with code. The fixed code would be greatly appreciated! Error messages: PlayerMoveDungeon2.java:21: error: cannot find symbol for (i = 0; i < length; i++) { ^ symbol: variable i location: class PlayerMoveDungeon2 PlayerMoveDungeon2.java:21: error: cannot find symbol for (i = 0; i < length; i++) { ^ symbol: variable i location: class PlayerMoveDungeon2 PlayerMoveDungeon2.java:21: error: cannot find symbol for (i = 0; i < length; i++) { ^ symbol: variable i location: class PlayerMoveDungeon2 PlayerMoveDungeon2.java:22: error: cannot find symbol...
For each of the following Visual Basic code snippets, identify the syntax error.   If intX >...
For each of the following Visual Basic code snippets, identify the syntax error.   If intX > 100   lblResult.Text = "Invalid Data" End If   Dim str As String = "Hello" Dim intLength As Integer intLength = Length(str) If intZ < 10 Then   lblResult.Text = "Invalid Data"   Dim str As String = "123" If str.IsNumeric Then   lblResult.Text = "It is a number." End If   Select Case intX   Case < 0     lblResult.Text = "Value too low."   Case > 100     lblResult.Text = "Value too high."   Case Else     lblResult.Text = "Value...
How to find IRR in excel using trial-error method? Cash flows for each year are following:...
How to find IRR in excel using trial-error method? Cash flows for each year are following: Y0 = -55 Y1 = -30.62 Y2 = 4.14 Y3 = 6.68 Y4 = 8.74 Y5 = 9.54 Y6: tricky part is after Y5, cash flow growth is 3% indefinitly rate of return = 11%
What is the difference between random error and systematic errors ? How do you correct each...
What is the difference between random error and systematic errors ? How do you correct each one ?
please correct the error and fix this code: (i need this work and present 3 graphs):...
please correct the error and fix this code: (i need this work and present 3 graphs): Sampling_Rate = 0.00004; % which means one data point every 0.001 sec Total_Time = 0:Sampling_Rate:1; % An array for time from 0 to 1 sec with 0.01 sec increment Omega = 49.11; % in [rad/s] zeta=0.0542; %unitless Omega_d=49.03; % in [rad/s] Displacement_Amplitude = 6.009; % in [mm] Phase_Angle = 1.52; % in [rad] Total_No_of_Points = length(Total_Time); % equal to the number of points in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT