You must find and correct all bugs in the following code, Put a comment in for e
ID: 3569289 • Letter: Y
Question
You must find and correct all bugs in the following code, Put a comment in for each bug you find and then correct the error. The program must be fully debugged so that it compiles and has NO runtime or logic errors.
// Arrays.java // Debugging - put a comment in for each error found and // explain what is wrong. You must fix all errors so that // the program compiles and runs without any errors. public class Arrays { public static void main( String args[] ) { int number; int array[][] = { { 1, 2, 3 }, { 4, 5 }, ( 15 19 21 ) }; // loop through array's rows for ( row = 1; row <= array.length(); row++ ) { // loop through columns of current row for ( column = 1; column < array.length; column++ ); array[ row ][ column ] += number; System.out.print( array ) System.out.println(); // start new line of output } // end outer for String Names = { Bart, Lisa, Maggie, Marge, Homer }; for( index = 0, index, < names, index++ ) System.out.println( names[ index1 ] ); int number1 = 0, number2 = 2, number3 = 4; double result = 2.0 * number2 / number1; number += number3; System.out.println( result ); System.out.println( number ); } // end main } // end class Arrays
Explanation / Answer
Here you go :)
please comment if you have any doubts
public class Arrays
{
public static void main( String args[] )
{
int number;
int array[][] = { { 1, 2, 3 }, { 4, 5 },{15,19,21} }; // wrong initilization
number=5;
// loop through array's rows
for ( int row = 0; row < array.length; row++ ) // there is no method length() for array objects, its length field that returns the no of elements.
{
// loop through columns of current row
for ( int column = 0; column < array[row].length; column++ ) // ; closes the for loop
{ array[ row ][ column ] += number;
System.out.print( array[row][column]+" " );
}
System.out.println(); // start new line of output
} // end outer for
String[] Names = { "Bart", "Lisa", "Maggie", "Marge", "Homer" };//string objects needs to be in "" and its an array so []
for( int index = 0; index< Names.length; index++ ) // inappropriately placed , in place of ;
//its Names not names
//and length to get the size of array
System.out.println( Names[ index ] ); //index not index1
int number1 = 0, number2 = 2, number3 = 4;
double result = 2.0 * number2 / number1;
number=0;
number += number3; //need to initialize number before using it
System.out.println( result );
System.out.println( number );
} // end main
} // end class Arrays
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.