The main objective was to have a lake and add fish objects to it, which have details about their weight etc. But however, every time a new fish is created, the previous object appears as null instead of its location, which I could use my getName to get the name.
package com. company;
public class Main {
public static void main(String[] args) {
Lake l = new Lake("jersey", 1, 2, 3,4 );
l.addFish("carp", 20, 1);
l.addFish("josh",20,1);
Lake n = new Lake("york",1,2,4,20);
n.addFish("catfish",20,30);
n.addFish("tot",20,30);
n.addFish("salmon",20,30);
n.addFish("shrimp",20,30);
n.addFish("swordfish",20,30);
}
}
package com. company;
import java.lang.reflect.Array;
import java.util.Arrays;
public class Lake {
private String name;
private double length;
private double width;
private double depth;
private final int amountOfFish;
private int currentFishAmount = 0 ;
Lake(String name, double length, double width, double depth, int amountOfFish)
{
this.name = name;
this.length = length;
this.width = width;
this.depth = depth;
this.amountOfFish = amountOfFish;
}
public void addFish (String name, double length, double weight)
{
Fish fishInLake [] = new Fish[amountOfFish];
fishInLake[currentFishAmount]= new Fish(name, length, weight);
System.out.println("Lake: " + this.name + "|" + "Fish:" + ""+ fishInLake[currentFishAmount].getName() + fishInLake[currentFishAmount].getLength());
System.out.println(currentFishAmount);
currentFishAmount++;
System.out.println(Arrays.toString(fishInLake));
}
}
package com.company;
import java.lang.reflect.Array;
public class Fish {
private String name;
private double length;
private double weight;
Fish (String name, double length, double weight){
this.name = name;
this.length = length;
this.weight = weight;
}
public String getName(){
return this.name;
}
public double getLength(){
return this.length;
}
public double getWeight(){
return this.weight;
}
}
The main objective was to have a lake and add fish objects to it, which have details about their weight etc. But however, every time a new fish is created, the previous object appears as null instead of its location, which I could use my getName to get the name.