Question: Can you fix the errors stated in the methods in this class? /** * Silly loop games! * @author Merlin * */ public class PaperClass
Can you fix the errors stated in the methods in this class?
/**
* Silly loop games!
* @author Merlin
*
*/
public class PaperClass
{
private int x;
/**
* Just remember the parameter so we can use it later
* @param x
*/
public PaperClass(int x)
{
this.x = x;
}
/**
* Create an array that contains the positive integers up
* through x in sequential order (1, 2, 3, ..., x)
* - one logic error and one syntax error
* @return the array
*/
public int[] buildSequential()
{
int[] y = new int[x];
for (int i = 0; i < y.length; i++);
{
y[i] = i;
}
return y;
}
/**
* Create an array that contains even negative numbers down
* to -x in sequential order (-2, -4, ..., -x). Assumes that
* x is an even number.
* - two logic errors
* @return the array
*/
public int[] buildDecreasingByTwo()
{
int[] y = new int[x];
int number = -1;
for (int i = 0; i < y.length; i++)
{
y[i] = number;
number = number - 2;
}
return y;
}
/**
* -two logic errors (on the same line) and one syntax error
* @return A string with the first x characters of the
* alphabet
*/
public String buildAlphabet()
{
String result;
for (int i = 0; i < x; i++)
{
result = result + 'a' + i;
}
return result;
}
/**
* Find the first power of x that is
* greater than the * target
* - two logic errors
* @param target what we're shooting to exceed
* @return the power of x
*/
public int firstPowerOver(int target)
{
int power = 0;
while (power < target)
{
power=power +x*x;
}
return power;
}
/**
* Find the exponent of the first power of x that is * greater than the target
* - two logic errors and one syntax error
* @param target what we're shooting to exceed
* @return the exponent of the power of x
*/
public void exponentOfFirstPowerOver(int target)
{
int power = 1;
int exponent = 0;
while (power < target);
{
power = power * x;
exponent++;
}
return target;
}
/**
* definition of prime number: A whole number greater than * 1 that has only two whole number factors, itself and 1. * - two logic errors and one syntax error
* @return the number of prime numbers that are less than x */
public int numberOfPrimesBelow()
{
int count = 0;
for (int possiblyPrime = 1; possiblyPrime < x;
possiblyPrime++)
{
int i = 2;
int mightBePrime = true;
while
((!mightBePrime) &&
(i <= Math.sqrt(possiblyPrime)))
{
if (possiblyPrime % i == 0)
mightBePrime = false;
i = i + 1;
}
if (mightBePrime)
{
count++;
}
}
return count;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
