posted 17 years ago
ok so i made some code now for the knapsack. its not very complicated (but also not streamlined). i've created the 20 random objects, but i need to sort through array [b] now, find the highest value to weight ratio objects, place them in the sack, and subtract the object's corresponding weight value from the 35kg knapsack limit.
import java.awt.*;
import javax.swing.*;
public class knapsack extends JApplet{
public void paint (Graphics p){
float a[]=new float [40]; //initialises array "a"
a[0]=(int)(Math.random()*5+1);//generates the weight of the object
a[1]=(int)(Math.random()*10+1);//generates the value of the object
p.drawString("Weight in Kg of Object 1 = "+a[0] ,50,50);
p.drawString("Value in � of Object 1 = "+a[1] ,50,65);
float b[]= new float [20];
b[0]=(a[1]/a[0]);//creates the value of the object in pounds kilo
p.drawString("� per Kg of object 1 = "+b[0] ,50,80);
a[2]=(int)(Math.random()*5+1);
a[3]=(int)(Math.random()*10+1);
b[1]=(a[3]/a[2]);
p.drawString("Weight in Kg of Object 2 = "+a[2] ,50,100);
p.drawString("Value in � of Object 2 = "+a[3] ,50,115);
p.drawString("� per Kg of object 2 = "+b[1] ,50,130);//object 2
a[4]=(int)(Math.random()*5+1);
a[5]=(int)(Math.random()*10+1);
b[2]=(a[5]/a[4]);
p.drawString("Weight in Kg of Object 3 = "+a[4] ,50,150);
p.drawString("Value in � of Object 3 = "+a[5] ,50,165);
p.drawString("� per Kg of object 3 = "+b[2] ,50,180);//object 3
a[6]=(int)(Math.random()*5+1);
a[7]=(int)(Math.random()*10+1);
b[3]=(a[7]/a[6]);
p.drawString("Weight in Kg of Object 4 = "+a[6] ,50,200);
p.drawString("Value in � of Object 4 = "+a[7] ,50,215);
p.drawString("� per Kg of object 4 = "+b[3] ,50,230);//object 4
a[8]=(int)(Math.random()*5+1);
a[9]=(int)(Math.random()*10+1);
b[4]=(a[9]/a[8]);
p.drawString("Weight in Kg of Object 5 = "+a[8] ,50,250);
p.drawString("Value in � of Object 5 = "+a[9] ,50,265);
p.drawString("� per Kg of object 5 = "+b[4] ,50,280);//object 5
a[10]=(int)(Math.random()*5+1);
a[11]=(int)(Math.random()*10+1);
b[5]=(a[11]/a[10]);
p.drawString("Weight in Kg of Object 6 = "+a[10] ,50,300);
p.drawString("Value in � of Object 6 = "+a[11] ,50,315);
p.drawString("� per Kg of object 6 = "+b[5] ,50,330);//object 6
a[12]=(int)(Math.random()*5+1);
a[13]=(int)(Math.random()*10+1);
b[6]=(a[13]/a[12]);
p.drawString("Weight in Kg of Object 7 = "+a[12] ,50,350);
p.drawString("Value in � of Object 7 = "+a[13] ,50,365);
p.drawString("� per Kg of object 7 = "+b[6] ,50,380);//object 7
a[14]=(int)(Math.random()*5+1);
a[15]=(int)(Math.random()*10+1);
b[7]=(a[15]/a[14]);
p.drawString("Weight in Kg of Object 8 = "+a[14] ,50,400);
p.drawString("Value in � of Object 8 = "+a[15] ,50,415);
p.drawString("� per Kg of object 8 = "+b[7] ,50,430);//object 8
a[16]=(int)(Math.random()*5+1);
a[17]=(int)(Math.random()*10+1);
b[8]=(a[17]/a[16]);
p.drawString("Weight in Kg of Object 9 = "+a[16] ,50,450);
p.drawString("Value in � of Object 9 = "+a[17] ,50,465);
p.drawString("� per Kg of object 9 = "+b[8] ,50,480);//object 9
a[18]=(int)(Math.random()*5+1);
a[19]=(int)(Math.random()*10+1);
b[9]=(a[19]/a[18]);
p.drawString("Weight in Kg of Object 10 = "+a[18] ,50,500);
p.drawString("Value in � of Object 10 = "+a[19] ,50,515);
p.drawString("� per Kg of object 10 = "+b[9] ,50,530);//object 10
a[20]=(int)(Math.random()*5+1);
a[21]=(int)(Math.random()*10+1);
b[10]=(a[21]/a[20]);
p.drawString("Weight in Kg of Object 11 = "+a[20] ,350,50);
p.drawString("Value in � of Object 11 = "+a[21] ,350,65);
p.drawString("� per Kg of object 11 = "+b[10] ,350,80);//object 11
a[22]=(int)(Math.random()*5+1);
a[23]=(int)(Math.random()*10+1);
b[11]=(a[23]/a[22]);
p.drawString("Weight in Kg of Object 12 = "+a[22] ,350,100);
p.drawString("Value in � of Object 12 = "+a[23] ,350,115);
p.drawString("� per Kg of object 12 = "+b[11] ,350,130);//object 12
a[24]=(int)(Math.random()*5+1);
a[25]=(int)(Math.random()*10+1);
b[12]=(a[25]/a[24]);
p.drawString("Weight in Kg of Object 13 = "+a[24] ,350,150);
p.drawString("Value in � of Object 13 = "+a[25] ,350,165);
p.drawString("� per Kg of object 13 = "+b[12] ,350,180);//object 13
a[26]=(int)(Math.random()*5+1);
a[27]=(int)(Math.random()*10+1);
b[13]=(a[27]/a[26]);
p.drawString("Weight in Kg of Object 14 = "+a[26] ,350,200);
p.drawString("Value in � of Object 14 = "+a[27] ,350,215);
p.drawString("� per Kg of object 14 = "+b[13] ,350,230);//object 14
a[28]=(int)(Math.random()*5+1);
a[29]=(int)(Math.random()*10+1);
b[14]=(a[29]/a[28]);
p.drawString("Weight in Kg of Object 15 = "+a[28] ,350,250);
p.drawString("Value in � of Object 15 = "+a[29] ,350,265);
p.drawString("� per Kg of object 15 = "+b[14] ,350,280);//object 15
a[30]=(int)(Math.random()*5+1);
a[31]=(int)(Math.random()*10+1);
b[15]=(a[31]/a[30]);
p.drawString("Weight in Kg of Object 16 = "+a[30] ,350,300);
p.drawString("Value in � of Object 6 = "+a[31] ,350,315);
p.drawString("� per Kg of object 6 = "+b[15] ,350,330);//object 16
a[32]=(int)(Math.random()*5+1);
a[33]=(int)(Math.random()*10+1);
b[16]=(a[33]/a[32]);
p.drawString("Weight in Kg of Object 17 = "+a[32] ,350,350);
p.drawString("Value in � of Object 17 = "+a[33] ,350,365);
p.drawString("� per Kg of object 17 = "+b[16] ,350,380);//object 17
a[34]=(int)(Math.random()*5+1);
a[35]=(int)(Math.random()*10+1);
b[17]=(a[35]/a[34]);
p.drawString("Weight in Kg of Object 18 = "+a[34] ,350,400);
p.drawString("Value in � of Object 18 = "+a[35] ,350,415);
p.drawString("� per Kg of object 18 = "+b[17] ,350,430);//object 18
a[36]=(int)(Math.random()*5+1);
a[37]=(int)(Math.random()*10+1);
b[18]=(a[37]/a[36]);
p.drawString("Weight in Kg of Object 19 = "+a[36] ,350,450);
p.drawString("Value in � of Object 19 = "+a[37] ,350,465);
p.drawString("� per Kg of object 19 = "+b[18] ,350,480);//object19
a[38]=(int)(Math.random()*5+1);
a[39]=(int)(Math.random()*10+1);
b[19]=(a[39]/a[38]);
p.drawString("Weight in Kg of Object 20 = "+a[38] ,350,500);
p.drawString("Value in � of Object 20 = "+a[39] ,350,515);
p.drawString("� per Kg of object 20 = "+b[19] ,350,530); //object 20
}
}