java - Placing an Object into and Object Array, which is inside another Object Array -
so i'm new coding/java , i'm working on project. assignment create 3 different classes (which have here) , make them cooperate. i've made fishtankmanagerapp
class retrieve new fish
object , i'm trying figure out how put in fishtank
object.
my fishtank
class only there create array object can hold 5 fish (i think i've done correctly). in fishtankmanagerapp
class, i've created array of 10 of these fishtank
objects.
my question cant figure out life of me how place fish
objects specfic fishtank
object after they've been created (i've made note @ end of code i've ran problem).
essentially know i'm trying put object i've created inside of , array contains array fish objects can stored... think....
any appreciated! thank you!
import java.util.scanner; public class fish { private static scanner stdin = new scanner(system.in); private string userinput; private int userinput2; public boolean mean; public string name; public fish(){ system.out.println("what fishes name?"); userinput = stdin.next(); this.name = userinput; system.out.println("is fish aggressive?\n"+ "(1)yes\n(2)no"); userinput2 = stdin.nextint(); if (userinput2 == 1) this.mean = true; else this.mean = false; } }
public class fishtank { public fishtank(){ fish[] tank = new fish[5]; } }
import java.util.scanner; public class fishtankmanager { private static scanner stdin = new scanner(system.in); private static int userinput; private static fishtank[] tanks = new fishtank[10]; public static void main (string[] args) { while (true){ system.out.println("welcome new fish tank manager!\n"+ "what do?\n"+ "(1)add fish\n(2)move fish\n"+ "(3)check tank"); userinput = stdin.nextint(); if (userinput == 1){ fish fish = new fish(); system.out.println(fish.name); system.out.println(fish.mean); changetank(fish); } else if(userinput ==2){ } else{ } } } private static void changetank(fish fish){ system.out.println("which tank put fish in? (1-10)"); userinput = stdin.nextint(); tanks[userinput] = fish; // last line i'm confused on how put fish() object tank i've created. } }
i'd recommend adding method fishtank
make adding fish easy. maybe this:
public fishtank(){ private fish[] tank = new fish[5]; public boolean addfish(fish fish) { // ... add code here put fish tank array // return true if there room in tank, false otherwise } }
note tank
variable private. it's idea keep member variables private in java, , use methods access them.
then, you've got comment mentioned, can call new method:
boolean addedsuccessfully = tanks[userinput].addfish(fish);
you may not need return boolean i'm showing, might handy if need check whether array (i.e. tank) had room new fish.
Comments
Post a Comment