I am writing a program that creates an object (Line) that contains a name and two nodes (x,y,z coordinates) which are then stored in a separate object (class LineModel). Within the class LineModel a method, getNode(), is created that should return the node. The nodes are constructed in a separate object (class Node).
My problem lies within the method getNode(), as I can't seem to return the node that I'm looking for.
public class LineModel {
// Object attributes
private String name;
private Line[] lines;
private int numLines;
// Constructor
public LineModel(String name, int maxLines) {
this.name = name;
lines = new Line[maxLines];
numLines = 0;
}
// Add lines
public void addLine(Line line) {
if (contains(line)) {
System.out.println("Line " + line.getName() + " already in model");
return;
}
if (numLines < lines.length) {
lines[numLines] = line;
numLines++;
} else {
System.out.println("Increase lines array size.");
System.exit(1);
}
}
public Node getNode(String name) {
for (int i = 0; i < numLines; i++) {
if (lines[i].getN1().getName().equals(name) || lines[i].getN2().getName().equals(name)) {
return lines[i].getN1();
} else {
return null;
}
}
}
Below are the classes Line and Node
public class Line {
// Object attributes
private String name;
private Node n1, n2;
// Constructor(s)
public Line(String name, Node n1, Node n2){
this.name = name;
this.n1 = n1;
this.n2 = n2;
}
public String getName(){ return name; }
// Object methods
public double length(){
double[] n1C = n1.getCoordinates();
double[] n2C = n2.getCoordinates();
if(n1C.length == n2C.length){
double pythagoras = 0;
for (int i = 0; i < n1C.length; i++) {
double dv = n2C[i] - n1C[i];
pythagoras += dv*dv;
}
return Math.sqrt(pythagoras);
}
return Double.NaN;
}
@Override
public String toString(){
return "Line "+name+" "+n1.getName()+"-->"+n2.getName()+" Length = "+length();
}
public Node getN1() { return n1;}
public Node getN2() { return n2;}
public class Node {
// Object attributes
private String name;
private double[] coordinates;
// Constructor(s)
public Node(String name, double x) {
this.name = name;
coordinates = new double[1];
coordinates[0] = x;
}
public Node(String name, double x, double y) {
this.name = name;
coordinates = new double[2];
coordinates[0] = x; coordinates[1] = y;
}
public Node(String name, double x, double y, double z) {
this.name = name;
coordinates = new double[3];
coordinates[0] = x; coordinates[1] = y; coordinates[2] = z;
}
// Object methods
public String getName(){
return name;
}
public double[] getCoordinates(){
return coordinates;
}
public double getX() {
if (coordinates.length > 0){
return coordinates[0];
} else {
return Double.NaN;
}
}
public double getY() {
if (coordinates.length > 1){
return coordinates[1];
} else {
return Double.NaN;
}
}
public double getZ() {
if (coordinates.length > 2){
return coordinates[2];
} else {
return Double.NaN;
}
}
public String toString() {
return "Node "+name+" "+Arrays.toString(coordinates);
}
}
The current error is that it has to return type Node, but I can't seem to figure out why it says that. Sorry for the large amount of code. I'm quite new to coding so I don't know if everthing is relevant.
The for-loop inside "getNode" always gets terminated after one iteration. If the first node has the matching name it returns the node, otherwise it returns null which always terminates the for-loop after the first iteration without checking any further nodes inside the array.
IMHO you should change the code to something like this:
public Node getNode(String name) {
for (int i = 0; i <= numLines; i++) {
if (lines[i].getN1().getName().equals(name) || lines[i].getN2().getName().equals(name)) {
return lines[i].getN1();
}
}
return null;
}
In this case the for-loop iterates over each element of the array until it finds the correct node or until there are no more elements left. If the requested node gets found it returns the node, otherwise it returns null.
You should also do
for (int i = 0; i <= numLines; i++)
instead of
for (int i = 0; i < numLines; i++)
because, in your implementation the last element of the array will always be ignored as if the array has one element "numLines" will be "1" and "1 < 1 == false". This means, that the loop even doesn't do any iteration and if "numLines" is "2" it will only perform one iteration as "2 < 2 == false".