KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > supergene > Force


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package examples.supergene;
11
12 /**
13  * Solve the change problem using force method. This class was used to
14  * verify if the solution exists in general.
15  * @author Audrius Meskauskas
16  */

17 public final class Force {
18   /** String containing the CVS revision. Read out via reflection!*/
19   private final static String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
20
21   public static boolean REPORT_ENABLED = true;
22
23   private Force() {
24
25   }
26
27   /**
28    * Check the existence of soulution.
29    * @param a_sum the sum needed
30    * @return true if the change can be expressed in coins, satisfying
31    * pennies mod 2 = nickels mod 2
32    *
33    * @author Audrius Meskauskas
34    */

35   public static boolean solve(int a_sum) {
36     for (int q = 0; q < 4; q++) {
37       for (int d = 0; d < 10; d++) {
38         for (int n = 0; n < 20; n++) {
39           for (int p = 0; p < 99; p++) {
40             if (AbstractSupergeneTest.amountOfChange(q, d, n, p) == a_sum) {
41               if (p % 2 == n % 2) {
42                 if (REPORT_ENABLED) {
43                   System.out.println("Force " + a_sum + ": " + q
44                                      + " quarters " + d + " dimes "
45                                      + n + " nickels " + p + " pennies");
46                 }
47                 return true;
48               }
49             }
50           }
51         }
52       }
53     }
54     if (REPORT_ENABLED) {
55       System.out.println("Force " + a_sum + ": no solution");
56     }
57     return false;
58   }
59
60   /**
61    * Test the Force method itself.
62    * @param args ignored
63    */

64   public static void main(String JavaDoc[] args) {
65     for (int i = 1; i <= 100; i++) {
66       solve(i);
67     }
68   }
69
70 }
71
Popular Tags