java - Fish Pond Simulation: Homework, where am I going wrong? -
this multipart question.
restrictions:
- can't use
.clone()
, orcollections
, orsystem
. - can't change different object type (i mean can't change
arraylist
list
or not).
i think problem might haven't grasped how throw exceptions. not sure though.
here part of assignment i'm stuck on.
adds fish f fish list, if possible.
first checks if landscape in fish's location equal rock. if is, fish not added list. instead, throws illegalfishpositionexception, passing
illegalfishpositionexception.fish_over_rock
constructor.next checks fish (distinct parameter) in same location parameter. if 1 found, fish not added list. instead throws illegalfishpositionexception, passing
illegalfishpositionexception.two_fish_in_one_place
constructor.otherwise, adds parameter fish list.
public void addfish(fish f) { arraylist <fish> addfish = new arraylist <fish>( fish ); if ( landscape[ f.getrow() ][ f.getcol() ] == rock ) { throw new illegalfishpositionexception( illegalfishpositionexception.fish_over_rock ); } ( fish f1 : addfish ) { if ( ( f1.getrow() == f.getrow() && f1.getcol() == f.getcol() ) || f1 == f ) { throw new illegalfishpositionexception( illegalfishpositionexception.two_fish_in_one_place ); } } ( fish f2 : addfish ) { if ( ( f2.getrow() != f.getrow() && f2.getcol() != f.getcol() ) && landscape[ f.getrow() ][ f.getcol() ] != rock ) { fish.add( f ); } } }
and here second method seems logical me, failing on tests.
/* checks specified location see if has rock, fish, or plant * in it. if so, returns false; if water, returns true. */ public boolean isspaceavailable(int r, int c) { if ( landscape[r][c] == rock ) { return false; } ( fish f : fish ) { if ( ( f.getrow() == r ) && ( f.getcol() == c ) ) { return false; } } ( plant p : plants ) { if ( ( p.getrow() == r ) && ( p.getcol() == c ) ) { return false; } } return true; }
i have couple of other methods dependent on addfish, if can right, it'll have cascading effect.
here junit test need pass isspaceavailable method.
@test public void testisspaceavailable() { model m = new model(10,10,0,0,0); fish f = new fish(1, 7, 100, fish.up); plant p = new plant(2, 8, 100); m.addfish(f); m.addplant(p); assertfalse(m.isspaceavailable(1, 7)); assertfalse(m.isspaceavailable(2, 8)); assertfalse(m.isspaceavailable(0, 0)); (int = 1; < 9; i++) { (int j = 1; j < 9; j++) { if ((i != 1 || j != 7) && (i != 2 || j != 8)) { asserttrue(m.isspaceavailable(i, j)); } } } }
here javadoc project if guys need through it. http://www.cs.umd.edu/class/spring2013/cmsc131-23/projects/p7/doc/index.html
i've been staring @ while , i'm throughly stuck.
addfish
in bad state. remove local arraylist
, if worried thread safety/concurrency use locking strategy or synchronise method. final loop wrong , redundant, time hit have satisfied conditions adding fish. can add fish plant is?
public void addfish(fish f) { if ( landscape[ f.getrow() ][ f.getcol() ] == rock ) { throw new illegalfishpositionexception( illegalfishpositionexception.fish_over_rock ); } ( fish f1 : this.fish ) { // fish.equals implemented if use that. if ( f1 == f){ // fish in list nothing // bad practice have return in middle of method return; } else if ( ( f1.getrow() == f.getrow() && f1.getcol() == f.getcol() )) { throw new illegalfishpositionexception( illegalfishpositionexception.two_fish_in_one_place ); } } fish.add( f ); }
Comments
Post a Comment