Answer the question
In order to leave comments, you need to log in
What's wrong with Java code?
My task is:
1. Create a class shape
2. Create an array of points
3. Create a method that adds a point to an array of points (addPoint)
4. Create a method that calculates the perimeter of the shape
5. Test the program
I wrote like this:
import java.util.ArrayList;
public class Main
{
public static void main(String[] args) {
arrayList.addPoint(5.0,5.0,5.0);
console.log(arrayList.perShape());
}
}
class Shape {
ArrayList<Double> arrayList = new ArrayList<>();
public void addPoint(double x, double y, double z) {
arrayList.add(x);
arrayList.add(y);
arrayList.add(z);
}
public double perShape() {
double per = arrayList.get(0) + arrayList.get(1) + arrayList.get(2);
return per;
}
}
Answer the question
In order to leave comments, you need to log in
Learn to read compiler messages. And so everything is wrong with you, there are practically no correct lines in the code.
The point must contain its coordinates.
The array must contain dots, not doubles.
Its array must not be inside a point.
The perimeter of a single point is an oxymoron, and of course, the perimeter is not calculated by adding the coordinates.
In the Main class, the main method accesses the undeclared arrayList variable.
Further, in addition to a gross syntactic error, there is a logical one. When calculating the perimeter, you simply add up all the coordinates of the first point. It's definitely not the perimeter!
To determine the distance between two points, you need to take the square root of the sum of the squared differences of all coordinates.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question