Question

In: Computer Science

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.

  1. 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;
    
  2. b) String[] names = {“Mina” , “George”};
    int totalLength = 0;
    for (int i = 0; i <= names.length() ; i++)

            totalLength += names[i].length;
    
  3. c) String[] words = “Hello”,“Goodbye”; System.out.println(words.toUpperCase());

  4. d) public class MyClass { private int x;

          private double y;
          public void MyClass (double a, double b) {
    

    x = a;

    y = b; }

    }

  5. e) public class MyClass { private int x;

          private double y;
          public static void myFuncion(int a, double b) {
    

    x = a;

    y = b; }

          public static int myFunction(int a, double b) {
              x = a;
    

    y = b;

    return 1; }

    }

Solutions

Expert Solution

a)

final int ARRAY_SIZE = 5;
int[] table = new int[ARRAY_SIZE]; for (int x = 0; x < ARRAY_SIZE; x++)

      table[x] = 99;

In the code, we cannot modify the value of a variable declared as final. Also, the loop will iterate to less than the value of ARRAY_SIZE.

b)

String[] names = {"Mina" , "George"};
int totalLength = 0;
for (int i = 0; i < names.length ; i++)

totalLength += names[i].length();
   }

In the for loop header, to access the length of the array we use names.length and run the loop to a value less than it.Inside the loop, we get the length of individual string using names[i].length() function.

c) String[] words = {"Hello","Goodbye"};
System.out.println(words[0].toUpperCase());

words will be declared as and array, and a value of words array will be converted to upper case since toUpperCase() method works on string objects.

d)

public class MyClass {

private int x;

private double y;

public MyClass (double a, double b) {

x = a;

y = b; }

}

The constructor does not have a return type in a class.

e)

public class MyClass {

private int x;

private double y;

public void myFuncion(int a, double b) {

x = a;

y = b;

}

public int myFunction(int a, double b) {

x = a;

y = b;

return 1;

}

}

Non-static variables cannot be reference from static context. We just make the methods not static.


Related Solutions

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. x = 1; while ( x <= 10 ); x++; } for ( y = .1; y != 1.0; y += .1 System.out.println( y ); 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; } The following code should print...
There are two errors in this code. Identify the errors and give the correct code that...
There are two errors in this code. Identify the errors and give the correct code that will make the program to display the following output: Rectangle: height 2.0 width 4.0 Area of the Rectangle is 8.0 ----- public interface Shape { public double getArea(); } class Rectangle implements Shape { double height; double width; public Rectangle(double height, double width) { this.height=height; this.width=width; } public double getArea() { return height*width; } public String toString() { return "Rectangle: height "+height+" width "+width;...
The following code segment which uses pointers may contain logic and syntax errors. Find and correct...
The following code segment which uses pointers may contain logic and syntax errors. Find and correct them. a) Returns the sum of all the elements in summands int sum(int* values) { int sum = 0; for (int i = 0; i < sizeof(values); i++) sum += *(values + i); return sum; } b) Overwrites an input string src with "61C is awesome!" if there's room. Does nothing if there is not. Assume that length correctly represents the length of src....
1.The below code has some errors, correct the errors and post the working code. Scanner console...
1.The below code has some errors, correct the errors and post the working code. Scanner console = new Scanner(System.in); System.out.print("Type your name: "); String name = console.nextString(); name = toUpperCase(); System.out.println(name + " has " + name.Length() + " letters"); Sample Ouptut: Type your name: John JOHN has 4 letters    2. Write a code that it reads the user's first and last name (read in the entire line as a single string), then print the last name   followed by...
Identify and correct the errors in each of the following statements: a) printf( "The value is...
Identify and correct the errors in each of the following statements: a) printf( "The value is %d\n", &number ); b) scanf( "%d%d", &number1, number2 ); c) if ( c < 7 );{ printf( "C is less than 7\n" ); } d) if ( c => 7 ) { printf( "C is greater than or equal to 7\n" );
12- Find the 5 errors and wtrite correct versions in the following text IN SEARCH OF...
12- Find the 5 errors and wtrite correct versions in the following text IN SEARCH OF PROFITS IN CHINA China has a population of about 1.4 billion; but that does not translate to as many customers; number of those that can afford the products of foreign companies would not reach 100 million. Still, foreign invested companies are quite profitable in China; they account for about 15% of the profits of industrial companies in the country. Supported by high income inequality...
C PROGRAMMING Identify and correct the errors in each of the following. [Note: There may be...
C PROGRAMMING Identify and correct the errors in each of the following. [Note: There may be more than one error in each piece of code.] a) if ( sales => 5000 ) puts( "Sales are greater than or equal to $5000" ) else puts( "Sales are less than $5000 ) b) int x = 1, product = 0; while ( x <= 10 ); { product *= x; ++x; } c) While ( x <= 100 ) total =+ x;...
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...
a. For each of the following, answer correct to two decimal places Find the area under...
a. For each of the following, answer correct to two decimal places Find the area under the curve y=3x^3+2 Find the area between the curve y=2/x and the x-axis for x between -5 and -4 Find the area between the curve y=x^2-3x and the x-axis for x between 2 and 7 Find the area bounded by the curves y=6x^3+1 and y=6x+1 b. The birth rate in a certain city is described by the following function . The city's death rate...
There are at least 10 errors in the following C program. For each error you can...
There are at least 10 errors in the following C program. For each error you can find you should list the location of the error, describe what the error is, and state how the error can be fixed (write updated code for that line if necessary). Each error you find is worth 1.5 marks. Note that missing brackets, braces, etc count as only one error, even though the missing brackets may occur at two places. The program is supposed to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT