<p>We can post solutions now right? Here’s my RetroBug</p>
<p>public class RetroBug extends Bug {</p>
<pre><code> private Location prevLoc;
private int prevDir;
public RetroBug() {
super();
prevLoc = getLocation(); //not sure if necessary but it ensures restore works if
prevDir = getDirection(); //called before acting
}
public void restore() {
Grid gr = getGrid();
if(gr.get(prevLoc) == null || gr.get(prevLoc) instanceof Flower) {
moveTo(prevLoc);
}
setDirection(prevDir);
}
public void act() {
prevDir = getDirection();
prevLoc = getLocation();
super.act();
}
</code></pre>
<p>}</p>
<p>EDIT: call me out on mistakes please</p>
<p>Here is my #1</p>
<p>a)</p>
<p>public void addClimb(String peakName, int climbTime){
ClimbInfo obj = new ClimbInfo(peakName, climbTime);
climbList.add(obj);
}</p>
<p>b)</p>
<p>public void addClimb(String peakName, int climbTime){
ClimbInfo obj = new ClimbInfo(peakName, climbTime);
for(int x=0; x<climbList.size(); x++){
int num = obj.getName().compareTo(climbList.get(x).getName());
if(num<=0){
climbList.add(x, obj);
return;
}
}
if(climbList.size()==0)
climbList.add(obj);
else{
climbList.add(climbList.size(), obj);
}
}</p>
<p>c)</p>
<p>NO
YES</p>
<p>Here’s my RetroBug - is it necessary to use a constructor?</p>
<p>public class RetroBug extends Bug{
private Location loc = getLocation();
private int dir = getDirection();
public void act(){
loc = getLocation();
dir = getDirection();
super.act();
//restore();
}
public void restore(){
Grid<actor> gr = getGrid();
if(gr.get(loc)==null || gr.get(loc) instanceof Flower){
moveTo(loc);
}
setDirection(dir);
}
}</actor></p>
<p>Horses, can someone tell me if part B works? I’m unsure.</p>
<p>a)</p>
<p>public int findHorseSpace(String name){
for(int x=0; x<spaces.length; x++){
String newName = spaces[x].getName();
if(newName!=null){
if(newName.equals(name))
return x;
}
}
return -1;
}</p>
<p>b)</p>
<p>public void consolidate(){
for(int x=0; x<spaces.length; x++){
Horse hors = spaces[x];
if(hors==null){
for(int y=x+1; y<spaces.length; y++){
Horse hors2 = spaces[y];
if(hors2!=null){
spaces[x] = hors2;
spaces[y] = hors;
break;
}
}
}
}
}</p>
<p>Finally, #4</p>
<p>a)</p>
<p>public int countWhitePixels(){
int count = 0;
for(int x=0; x<pixelValues.length; x++){
for(int y=0; y<pixelValues[x].length; y++){
int num = pixelValues[x][y];
if(num==GrayImage.WHITE)
count++;
}
}
return count;
}</p>
<p>b)</p>
<p>public void processImage(){
for(int x=0; x<pixelValues.length; x++){
for(int y=0; y<pixelValues[x].length; y++){
int newRow = x+2;
int newCol = y+2;
if(x<pixelValues.length && y<pixelValues[x].length){
int newValue = pixelValues[newRow][newCol];
pixelValues[x][y] = pixelValues[x][y] - newValue;
if(pixelValues[x][y]<=GrayImage.BLACK){
pixelValue[x][y] = GrayImage.BLACK;
}
}
}
}
}</p>
<p>@KingAurora I agree with everything, B for horses as well. One question, do you need to use GreyImage.BLACK or can you just use BLACK for #4?</p>
<p>Awesome! and you can just use BLACK - just tested it in eclipse, haha. I wasn’t sure on the test so I just threw that in.</p>
<p>@kingaura</p>
<p>for your horse class, you didn’t check of there were 2 horses with the same name?</p>
<ol>
<li></li>
</ol>
<p>a)
int track = climbList.size();
climbList.add(track, new ClimbInfo(peakName, climbTime));</p>
<p>b.
for(int i = 0; i < climbList.size(); i++){</p>
<p>if(peakName.compareTo(climbList.get(i).getName() == -1)){</p>
<p>climbList.add(i, new ClimbInfo(peakName, climbTime))</p>
<p>}else{</p>
<p>climbList.add(i + 1, new ClimbInfo(peakName, climbTime));</p>
<p>}</p>
<p>is anything wrong here? i don’t think I had to put the first thing in part A, but I still think it works.</p>
<p>@BattleBrick</p>
<p>part a seems fine, although you didn’t have to add the int thing to the size</p>
<p>For the multiple choice, did any1 remember having a recursion question that asked what did you need to add to get 21, or some number?</p>
<p>yeah I put that the constant was 6</p>
<p>@MoldyBrick: I think your RetroBug will have a problem with initialization … since you have to create a bug before you put it in a grid, getLocation() will always be null when your constructor executes.</p>
<p>@KingAurora: I think your RetroBug has the same problem …</p>
<p>@KingAurora: 3b looks fine. You have a problem with 3a: you do the test for null too late. If a horse stall is empty, spaces[x].getName() throws a NullPointerException.</p>
<p>@dblocks: the description for problem 3 explicitly says that no two horses have the same name.</p>
<p>@KingAurora: 4b has a bug. Your first “if” statement needs to check newRow and newCol, not x and y.</p>
<p>@jkhuggins Thanks for looking them over! For 4B I had set the spot to the new value the line before. Wouldn’t that be fine?</p>
<p>@BattleBrick: your 1b has a couple of major problems. You can’t assume compareTo() returns -1; it could return any negative number. Also, the way you’ve written it, your method will add the new item multiple times … in fact, it’ll double the size of the list.</p>
<p>@KingAurora: you set “newRow” and “newCol” appropriately, but then you ask “if (x<… && y < …)”. You need to ask “if (newRow < … & newCol < …)”.</p>
<p>my teacher taught me that compareTo either returns -1, 0, 1. god dammit.</p>
<p>For the pixel one how much do you think will be taken off since I forgot to used getNumRows and getNumCols and instead just used .length for the for statements? Also on RetroBug I did call restore() in the act() method, but I did it in a weird way. I initialized an int value = 3 and then said if(value%2==1) then oldDir=getDir, etc. and else restore(). And on the horse one for the consolidation I wrote this really convoluted thing to make it work haha, do you think the readers will have the patience to work through it?</p>