JAVA basic programing Find-the-error #1) { private int x; private double y; publ
ID: 3564479 • Letter: J
Question
JAVA basic programing
Find-the-error
#1)
{
private int x;
private double y;
public void MyClass(int a, double b)
{
x = a;
y = b;
}
}
#2)
public void total(int valuel, value2, value3)
{
return valuel + value2 + value3;
}
------------------
#1)in-pseudocode!
Consider the following class declaration:
public class Square
{
private double sideLength;
public double getArea()
{
return sideLength * sideLength;
public double getSideLength()
{
return sideLength;
}
}
a. Write a no-arg constructor for this class. It should assign the sideLength field the
value 0.0.
b. Write an overloaded constructor for this class. It should accept an argument that is
copied into the sideLength field.
Explanation / Answer
#1)
{
private int x;
private double y;
public void MyClass(int a, double b)
{
x = a;
y = b;
}
}
No error in this program... it works fine..
#2)
public void total(int valuel, value2, value3)
{
return valuel + value2 + value3;
}
You cannot declare variables like this in a method definition (int valuel, value2, value3) -->it gives compilation error.
you have to declare all the variables as (int value1, int value2, int value3)--->it will work with no errors.
public void total(int value1,int value2,int value3)
{
return valuel + value2 + value3;
}
#1)in-pseudocode!
public class Square
{
private double sideLength;
public double getArea()
{
return sideLength * sideLength;
}
public double getSideLength()
{
return sideLength;
}
}
a) no - arg constructor
public Square()
{
sideLength = 0.0;
}
b)overloaded constructor
public Square(double len)
{
sideLength = len;
}
Place the constructors in the class so that it will work fine..
public class Square
{
private double sideLength;
public Square()
{
sideLength = 0.0;
}
public Square(double len)
{
sideLength = len;
}
public double getArea()
{
return sideLength * sideLength;
}
public double getSideLength()
{
return sideLength;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.