public class Candy
{
private String v;
public Candy( String f )
{
v = f;
}
public String getFlavor()
{
return v;
}
public String toString()
{
return v;
}
}
|
public class BoxOfCandy
{
private Candy[][] box;
public BoxOfCandy(int n, int m)
{
box = new Candy[n][m]; // Box is empty
}
public void insertFlavor(int i, int j, String f)
{
box[i][j] = new Candy(f);
}
/**
* Moves one piece of candy in column col, if necessary and possible,
* so that the box element in row 0 of column col contains a piece of candy,
* as described in part (a).
* Returns false if there is no piece of candy in column col and returns
* true otherwise.
* Precondition: col is a valid column index in box.
*/
public boolean moveCandyToFirstRow( int col )
{
// Write this method for part A
return false; // Statement added to prevent compile error
// Remove it when you write this method
}
/**
* Removes from box and returns a piece of candy with flavor specified
* by the parameter, or returns null if no such piece is found,
* as described in part (b)
*/
public Candy removeNextByFlavor( String flavor )
{
// Write this method for part B
return null; // Statement added to prevent compile error
// Remove it when you write this method
}
public String toString()
{
String s = "";
for( Candy[] r : box )
{
for( Candy c : r )
{
s += c + "\t";
}
s+= "\n";
}
return s;
}
}
|
Use this Java program to test your answer to part A:
public class TestPartA
{
public static void main(String[] args)
{
BoxOfCandy x = new BoxOfCandy(4,3);
x.insertFlavor(0, 1, "lime");
x.insertFlavor(1, 1, "orange");
x.insertFlavor(2, 2, "cherry");
x.insertFlavor(3, 1, "lemon");
x.insertFlavor(3, 2, "grape");
System.out.println(x);
System.out.println(x.moveCandyToFirstRow(0));
System.out.println(x);
System.out.println(x.moveCandyToFirstRow(1));
System.out.println(x);
System.out.println(x.moveCandyToFirstRow(2));
System.out.println(x);
}
}
|
The correct answer is:
null lime null null orange null null null cherry null lemon grape false null lime null null orange null null null cherry null lemon grape true null lime null null orange null null null cherry null lemon grape true null lime cherry null orange null null null null null lemon grape |
Use this Java program to test your answer to part B:
public class TestPartB
{
public static void main(String[] args)
{
BoxOfCandy x = new BoxOfCandy(3,5);
x.insertFlavor(0, 0, "lime");
x.insertFlavor(0, 1, "lime");
x.insertFlavor(0, 3, "lemon");
x.insertFlavor(1, 0, "orange");
x.insertFlavor(1, 3, "lime");
x.insertFlavor(1, 4, "lime");
x.insertFlavor(2, 0, "cherry");
x.insertFlavor(2, 2, "lemon");
x.insertFlavor(2, 4, "orange");
System.out.println(x);
System.out.println(x.removeNextByFlavor("cherry"));
System.out.println(x);
System.out.println(x.removeNextByFlavor("lime"));
System.out.println(x);
System.out.println(x.removeNextByFlavor("grape"));
System.out.println(x);
}
}
|
The correct answer is:
lime lime null lemon null orange null null lime lime cherry null lemon null orange cherry lime lime null lemon null orange null null lime lime null null lemon null orange lime lime lime null lemon null orange null null null lime null null lemon null orange null lime lime null lemon null orange null null null lime null null lemon null orange |