001 package com.croftsoft.apps.fraction; 002 003 import java.util.Random; 004 005 import com.croftsoft.core.math.MathLib; 006 007 /********************************************************************* 008 * Question data. 009 * 010 * @version 011 * 2002-07-20 012 * @since 013 * 2002-07-20 014 * @author 015 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 016 *********************************************************************/ 017 018 public final class FractionQuestion 019 ////////////////////////////////////////////////////////////////////// 020 ////////////////////////////////////////////////////////////////////// 021 { 022 023 private final int maxDenominator; 024 025 private final Random random; 026 027 // 028 029 private int firstDenominator; 030 031 private int firstNumerator; 032 033 private int secondDenominator; 034 035 private int secondNumerator; 036 037 private int thirdDenominator; 038 039 private int thirdNumerator; 040 041 // 042 043 private int commonDenominator; 044 045 private int firstAnswerNumerator; 046 047 private int secondAnswerNumerator; 048 049 private int thirdAnswerNumerator; 050 051 // 052 053 private boolean first; 054 055 private boolean second; 056 057 private boolean third; 058 059 private boolean fourth; 060 061 ////////////////////////////////////////////////////////////////////// 062 ////////////////////////////////////////////////////////////////////// 063 064 public FractionQuestion ( int maxDenominator ) 065 ////////////////////////////////////////////////////////////////////// 066 { 067 if ( maxDenominator < 1 ) 068 { 069 throw new IllegalArgumentException ( "maxDenominator < 1" ); 070 } 071 072 this.maxDenominator = maxDenominator; 073 074 this.random = new Random ( ); 075 076 reset ( ); 077 } 078 079 ////////////////////////////////////////////////////////////////////// 080 ////////////////////////////////////////////////////////////////////// 081 082 public void reset ( ) 083 ////////////////////////////////////////////////////////////////////// 084 { 085 first = false; 086 087 second = false; 088 089 third = false; 090 091 fourth = false; 092 093 int gcf; 094 095 while ( true ) 096 { 097 firstDenominator = random.nextInt ( maxDenominator ) + 1; 098 099 firstNumerator = random.nextInt ( firstDenominator + 1 ); 100 101 if ( firstNumerator == 0 ) 102 { 103 continue; 104 } 105 else 106 { 107 gcf = MathLib.greatestCommonFactor ( 108 firstDenominator, firstNumerator ); 109 110 firstDenominator /= gcf; 111 112 firstNumerator /= gcf; 113 } 114 115 secondDenominator = random.nextInt ( maxDenominator ) + 1; 116 117 secondNumerator = random.nextInt ( secondDenominator + 1 ); 118 119 if ( secondNumerator == 0 ) 120 { 121 continue; 122 } 123 else 124 { 125 gcf = MathLib.greatestCommonFactor ( 126 secondDenominator, secondNumerator ); 127 128 secondDenominator /= gcf; 129 130 secondNumerator /= gcf; 131 } 132 133 if ( firstDenominator > secondDenominator ) 134 { 135 if ( firstDenominator % secondDenominator == 0 ) 136 { 137 commonDenominator = firstDenominator; 138 } 139 else 140 { 141 commonDenominator = firstDenominator * secondDenominator; 142 } 143 } 144 else if ( firstDenominator < secondDenominator ) 145 { 146 if ( secondDenominator % firstDenominator == 0 ) 147 { 148 commonDenominator = secondDenominator; 149 } 150 else 151 { 152 commonDenominator = firstDenominator * secondDenominator; 153 } 154 } 155 else 156 { 157 continue; 158 } 159 160 if ( commonDenominator > maxDenominator ) 161 { 162 continue; 163 } 164 165 firstAnswerNumerator 166 = ( commonDenominator / firstDenominator ) * firstNumerator; 167 168 secondAnswerNumerator 169 = ( commonDenominator / secondDenominator ) * secondNumerator; 170 171 thirdAnswerNumerator = firstAnswerNumerator + secondAnswerNumerator; 172 173 thirdNumerator = thirdAnswerNumerator; 174 175 if ( thirdNumerator == 0 ) 176 { 177 thirdDenominator = 1; 178 } 179 else 180 { 181 thirdDenominator = commonDenominator; 182 183 gcf = MathLib.greatestCommonFactor ( 184 thirdDenominator, thirdNumerator ); 185 186 thirdDenominator /= gcf; 187 188 thirdNumerator /= gcf; 189 } 190 191 if ( ( thirdNumerator <= thirdDenominator ) 192 && ( thirdDenominator <= maxDenominator ) ) 193 { 194 break; 195 } 196 } 197 } 198 199 ////////////////////////////////////////////////////////////////////// 200 ////////////////////////////////////////////////////////////////////// 201 202 public int getFirstDenominator ( ) { return firstDenominator; } 203 204 public int getFirstNumerator ( ) { return firstNumerator; } 205 206 public int getSecondDenominator ( ) { return secondDenominator; } 207 208 public int getSecondNumerator ( ) { return secondNumerator; } 209 210 public int getThirdDenominator ( ) { return thirdDenominator; } 211 212 public int getThirdNumerator ( ) { return thirdNumerator; } 213 214 ////////////////////////////////////////////////////////////////////// 215 ////////////////////////////////////////////////////////////////////// 216 217 public int getCommonDenominator ( ) { return commonDenominator; } 218 219 public int getFirstAnswerNumerator ( ) 220 { return firstAnswerNumerator; } 221 222 public int getSecondAnswerNumerator ( ) 223 { return secondAnswerNumerator; } 224 225 public int getThirdAnswerNumerator ( ) 226 { return thirdAnswerNumerator; } 227 228 ////////////////////////////////////////////////////////////////////// 229 ////////////////////////////////////////////////////////////////////// 230 231 public boolean getFirst ( ) { return first; } 232 233 public boolean getSecond ( ) { return second; } 234 235 public boolean getThird ( ) { return third; } 236 237 public boolean getFourth ( ) { return fourth; } 238 239 public void setFirst ( boolean first ) { this.first = first; } 240 241 public void setSecond ( boolean second ) { this.second = second; } 242 243 public void setThird ( boolean third ) { this.third = third; } 244 245 public void setFourth ( boolean fourth ) { this.fourth = fourth; } 246 247 ////////////////////////////////////////////////////////////////////// 248 ////////////////////////////////////////////////////////////////////// 249 }