Wednesday, 1 October 2014

Program involving Chronological logic (GAME OF A THIEF)

     A PROGRAM TO HELP A THIEF.
STORY: a Thief is to robb a house. There are different items of different values and Amounts. 
This is a program to help the thief to take the most valuable items within his capacity.
 This program may seem easy but actually it has a brainstorming logic behind it.

CONCEPT:
                        Eg:
Item1 ($1000)(200kg) , Item2($2000)(100kg) , Item3($300)(200kg)  , Item4($4000)(300kg)

The thief’s capacity is 500kg of items , then he will take
Item4(300kg) , Item2(100kg) , Item1(100kg)

NOTE: Item-weights should be noted not to exceed the capacity.
CODING:
        import java.util.Scanner;
//program to steal most valuable items from a house by AGNIVA
public class Thief
{
    static int W[];
    static int M[];int n;
    static Scanner sc = new Scanner(System.in);
    public Thief(int num){
        n = num;
        W = new int[n];
        M = new int[n];
    }
    public void bblsort(){
        for (int c = 0; c < ( n - 1 ); c++) {
            for (int d = 0; d < n - c - 1; d++) {
                 if (M[d] < M[d+1]) /* For ascending order use < */
                 {
                    int swap  = M[d];
                    int swap2 = W[d];
                     M[d]   =   M[d+1];
                     M[d+1] = swap;
                     W[d]   =   W[d+1];
                     W[d+1] = swap2;
                 }
            }
         }
    }
    public void display(int fin)
    {
        System.out.println("------------------------------------------------------");
        for(int i = 0;i<=fin;i++){
            System.out.println(" STOLEN ITEM having"+(i+1)+"th PREFENCE::"+M[i]);
            System.out.println(" MASS ::"+W[i]+"Kg");
        }
    }
    public void input()
    {
         for(int i = 0;i<n;i++){
            System.out.println("ENTER Amount of item"+(i+1)+" in RUPEES");
            M[i] = sc.nextInt();
            System.out.println("ENTER MASS of item "+(i+1)+" in Kg");
            W[i] = sc.nextInt();
        }
    }
    public int sum(int g){
        int sn = 0;
        for(int i = 0;i<=g;i++){
            sn+= W[i];
        }
        return sn;
    }   
    public static void main(String[] args){
        System.out.println("ENTER NUMBER OF ITEMS");
        int no = sc.nextInt();
        Thief ob = new Thief(no);
        ob.input();
        System.out.println("ENTER THE CAPACITY :");
        int cap = sc.nextInt();
        ob.bblsort();
        int s = 0;int x = no-1;
        do{
            s = ob.sum(x);
            if(s>cap){
                x--;
            }
        }while((s>cap)&&(x>0));
        s = ob.sum(x);
        if(x == 0 && W[0]>cap){
            System.out.println("ITEM"+1+" RUPEES"+M[0]);
            System.out.println("ITEM Wt"+cap+"Kg ");
        }
        else{
            ob.display(x);
            if(s<cap ){
                try{
                int t = cap - s;
                System.out.println("REMAINING ITEM"+(x+2)+"= RUPEES"+M[x+1]);
                System.out.println("ITEM Wt"+t+"Kg ");
               }
               catch(ArrayIndexOutOfBoundsException e){
                   System.out.println("THE CAPACITY EXCEEDS THE OVERALL WEIGHTS");
                }
            }
        }
        System.out.println("ALL THESE ITEMS SHOULD BE STOLEN");
    }
}
SNAPSHOTS:




0 comments :

Post a Comment