Reputation:
I'm working on a homework problem that can be done in java or pseudo code, I'm trying the java code way.
Background: There exists a machine that makes iphone casing. The machine has three possible cases that it can make, each of which have different paint costs. Given original amounts of paint, return the integer value of the minimum amount of cases that can be made.
Here is my code,
public class ProblemFour
{
//Array entries represent cost for red, green and blue respectively.
int[] classicCost = {4, 1, 1};
int[] coolCost = {3, 2, 1};
int[] modernCost = {1, 3, 2};
String[] names = {"classic", "cool" , "modern"};
int red;
int green;
int blue;
Random generator = new Random();
int pick = generator.nextInt(3);
public int minPhone(int r, int g, int b, String mold)
{
int counter = 0;
if (r > 1 && g > 1 && b > 1 && mold.equals("classic"))
{
counter += 1;
red = r - classicCost[0];
green = g - classicCost[1];
blue = b - classicCost[2];
Random generator = new Random();
int pick = generator.nextInt(3);
counter += minPhone(red, green, blue, names[pick]);
}
else if (r > 1 && g > 1 && b > 1 && mold.equals("cool"))
{
counter += 1;
red = r - coolCost[0];
green = g - coolCost[1];
blue = b - coolCost[2];
Random generator = new Random();
int pick = generator.nextInt(3);
counter += minPhone(red, green, blue, names[pick]);
}
else if (r > 1 && g > 1 && b > 1 && mold.equals("modern"))
{
counter += 1;
red = r - modernCost[0];
green = g - modernCost[1];
blue = b - modernCost[2];
Random generator = new Random();
int pick = generator.nextInt(3);
counter += minPhone(red, green, blue, names[pick]);
}
return counter;
}
}
public static void main(String[] args)
{
ProblemSetThree.ProblemFour example = new ProblemFour();
String[] names = {"classic", "cool" , "modern"};
Random generator = new Random();
int pick = generator.nextInt(3);
//System.out.println(pick);
int testCount = example.minPhone(6, 7, 7, names[pick]);
System.out.println(testCount);
}
}
So in the example, the correct answer is 2, not 3. My code will sometimes give either. How do I make sure that it only returns the minimum number?
After this, I need to turn this into a dynamic programing program. I'm not interested in optimizating or software engineering aspects of this code, only that it returns the correct answer.
Thank you.
Upvotes: 1
Views: 532
Reputation: 3109
As this looks like homework, just a couple of tips:
The dynamic programming solution would be:
Recursively, that would be:
//costs
int[] A = {4, 1, 1};
int[] B = {3, 2, 1};
dint[] C = {1, 3, 2};
public int minPhone(int r, int g, int b){
int minA = 0;
int minB = 0;
int minC = 0;
if(r-A[0] > 0,g-A[1] > 0,r-A[2] > 0){ //can I make a case A?
minA = minPhone(r-A[0],g-A[1],r-A[2]);
}
if(r-B[0] > 0,g-B[1] > 0,r-B[2] > 0){ //can I make a case B?
minB = minPhone(r-B[0],g-B[1],r-B[2]);
}
if(r-C[0] > 0,g-C[1] > 0,r-C[2] > 0){ //can I make a case C?
minC = minPhone(r-C[0],g-C[1],r-C[2]);
}
minABC = min(minA,min(minB,minC));
return minABC;
}
Now, you need to come up with a version that does the same thing, but not recursively. That would be the dynamic programming. Hint: Think of creating a large spreadsheet like the recursive solution, but just use an array to do it without recursion.
Upvotes: 2