mike rivers

Greenhorn
+ Follow
since Dec 27, 2002
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by mike rivers

just a final note to say thankyou to everyone (especially david, you ra star)who posted here. we finally made it, with seconds to spare.
21 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

}
}
21 years ago
ohhh no! i left all that permutations, factorials, and combinations stuff behind me a few years ago. sounds like a shocker!
good luck with it.
21 years ago
thanks david, i think the concepts are becomming clearer. i'm going back to uni early on the 8th, so i can get to some books. i might well post some code around then.
$1 million! are you working on that one then? i take it its really hard.
thanks again
21 years ago
it would appear that the brute force method is right for this exercise (and my level of programming). however, one of the questions asked by the lecturer is; what effect would increasing the number of variables have. i think that brute force could not realistically be used on a larger scale situation. - thank heavens we don't have to solve for a greater number of variables.
21 years ago
just wanted to say a big thankyou to all who have posted here. any more suggestions are always appreciated.
21 years ago
another step along the way, thanks guys! you're all so helpful. thanks!!
i'm using fractions so your suggestions will most certainly help - i'll see what the public library has.
21 years ago
thanks very much guys for all of your support. i still don't really know any real java, i am an absolute beginner, with a intermediate problem. i'm gonna go away now and see what i can do, and read around in these areas. i think i need to use arrays, and recursive queries, but i don't know how to do that, not that i'm asking you lot, although it would help ;-) but at least i have some idea where in the books i need to look.
thanks again, i thought the world of java was filled with rude people (sun's forums, they didn't help) until i was told about this place.
21 years ago
hey all, you probably get this sort of thing all the time. i've been working pretty hard on this. i've joined a course late, and i have to complete this exercise. i am still very lost in the world of java. if anyone can help it will be greatly appreciated. code, suggestions, reading topice etc....
the knapsack:
knapsack can hold 35kg
have 20 objects with random weights (1-5kg) and random value ($1-10)
object:given the knapsack can hold upto 20 objects. maximise the total value of the contents of the knapsack.
the towers of hanoi:
three pins, a,b,c
three disks 1,2,3 initially placed on pin a, with 1 ontop of 2 and 2 on 3
disk 1 is the smallest, then 2, and the largest is disk 3
object nly 1 disk can move at a time, they must all end up on pin 3 in the same order as the starting order. only a smaller disk can be placed on a larger one. solve
email: five_belliez@hotmail.com
21 years ago