Write a Java Program for the UML below. Need a full operational program perferly
ID: 3860900 • Letter: W
Question
Write a Java Program for the UML below. Need a full operational program perferly that can run in Eclipse.
Address > Language Book Author ISBN:String name:String + subject:String AccountStatus +Active + Frozen + Closed name: String +biography:String +biography:String birthDate Date + English + French + German + Spanish + Italian overview String + publisher:String + publicationDate:Date lang:String > Catagory + Paperback + Hardcover + Audiobook + AudioCD +MP3CD +PDF Book Item + barcode: String + ISBN: String + subject: String + title: String +lang: Language + numberOfPages: Integer + format: Format +borrowed: Date + loanPeriod: Integer + dueDate: Date +isOverdue: Boolean Account + number: id + opened: Date +status: AccountStatus accounti Patron +name: FulName address: Address Library name: String address: Address access Librarian name Search --access- +address: Address Catalog position: String ..-access ManageExplanation / Answer
public class StringUtils {
8
9 public static final char DASH = '-';
10
11 public static final char EXPONENT = 'E';
12
13 public static final char PERIOD = '.';
14
15 public static final String ZERO = "0";
16
17
24 public static String filter(final String text, final String filter) {
25 final String[] words = text.split("[" + filter + "]");
26
27 switch (words.length) {
28 case 0: return text;
29 case 1: return words[0];
30 default:
31 final StringBuilder filteredText = new StringBuilder();
32
33 for (final String word : words) {
34 filteredText.append(word);
35 }
36
37 return filteredText.toString();
38 }
39 }
40 }
The MathsUtils class is like an addition to the java.lang.Math class and contains the rounding calculations.
Computer code Code listing 3.21: MathsUtils.java
1 package string;
2
3
10 public class MathsUtils {
11
12
14
15
16 public static final char EXPONENT = 'E';
17
18
19 private static final double E_TO_DOUBLE = 1E-3;
20
21
22 public static final String ZERO = "0";
23
24
25 private static final String ZEROS = "000000000000000000000000000000000";
26
29
30
36 private static boolean isScientific(final double number) {
37 return ((new Double(number)).toString().indexOf(EXPONENT) > 0);
38 }
39
40
48 private static byte calculateMissingSignificantZeros(
49 final byte significantsAfter,
50 final char separator,
51 final double number) {
52
53 final byte after = findSignificantsAfterDecimal(separator, number);
54
55 final byte zeros =
56 (byte) (significantsAfter - ((after == 0) ? 1 : after));
57
58 return ((zeros >= 0) ? zeros : 0);
59 }
60
61
68 private static byte findInsignificantZerosAfterDecimal(
69 final char separator,
70 final double number) {
71
72 if ((Math.abs(number) >= 1) || isScientific(number)) {
73 return 0;
74 } else {
75 final StringBuilder string = new StringBuilder();
76
77 string.append(number);
78 string.delete(0,
79 string.indexOf(new Character(separator).toString()) + 1);
80
81 // Determine what to match:
82 final String regularExpression = "[1-9]";
83
84 final String[] split = string.toString().split(regularExpression);
85
86 return (split.length > 0) ? (byte) split[0].length() : 0;
87 }
88 }
89
90
99 private static byte findSignificantDigits(final byte significantsAfter,
100 final char separator,
101 final double number) {
102
103 if (number == 0) { return 0; }
104 else {
105 String mantissa =
106 findMantissa(separator, new Double(number).toString());
107
108 if (number == (long)number) {
109 mantissa = mantissa.substring(0, mantissa.length() - 1);
110 }
111
112 mantissa = retrieveDigits(separator, mantissa);
113 // Find the position of the first non-zero digit:
114 short nonZeroAt = 0;
115
116 for (; (nonZeroAt < mantissa.length())
117 && (mantissa.charAt(nonZeroAt) == '0'); nonZeroAt++) ;
118
119 return (byte)mantissa.substring(nonZeroAt).length();
120 }
121 }
122
123
132 private static byte findSignificantsAfterDecimal(
133 final byte significantsBefore,
134 final byte significantDigits) {
135
136 final byte afterDecimal =
137 (byte) (significantDigits - significantsBefore);
138
139 return (byte) ((afterDecimal > 0) ? afterDecimal : 0);
140 }
141
142
149 private static byte findSignificantsBeforeDecimal(final char separator,
150 final double number) {
151
152 final String value = new Double(number).toString();
153
154
156 if ((number == 0) || (Math.abs(number) >= E_TO_DOUBLE)
157 && (Math.abs(number) < 1)) {
158
159 return 0;
160 } else if ((Math.abs(number) > 0) && (Math.abs(number) < E_TO_DOUBLE)) {
161 return 1;
162 } else {
163 byte significants = 0;
164
165 for (byte b = 0; b < value.length(); b++) {
166 if (value.charAt(b) == separator) {
167 break;
168 } else if (value.charAt(b) != StringUtils.DASH) {
169 significants++;
170 }
171 }
172
173 return significants;
174 }
175 }
176
177
183 private static short findExponent(final double number) {
184 return new Short(findExponent((new Double(number)).toString()));
185 }
186
193 private static String findExponent(final String value) {
194 final short exponentAt = (short) value.indexOf(EXPONENT);
195
196 if (exponentAt < 0) { return ZERO; }
197 else {
198 return value.substring(exponentAt + 1);
199 }
200 }
201
202
209 private static String findMantissa(final char separator,
210 final String value) {
211
212 String strValue = value;
213
214 final short exponentAt = (short) strValue.indexOf(EXPONENT);
215
216 if (exponentAt > -1) {
217 strValue = strValue.substring(0, exponentAt);
218 }
219 return strValue;
220 }
221
222
229 private static String retrieveDigits(final char separator, String number) {
230 // Strip off exponent part, if it exists:
231 short eAt = (short)number.indexOf(EXPONENT);
232
233 if (eAt > -1) {
234 number = number.substring(0, eAt);
235 }
236
237 return number.replace((new Character(StringUtils.DASH)).toString(), "").
238 replace((new Character(separator)).toString(), "");
239 }
240
241
242
250 public static byte digits(final long value) {
251 return (byte) StringUtils.filter(Long.toString(value), ".,").length();
252 }
253
254
261 public static byte findSignificantsAfterDecimal(final char separator,
262 final double number) {
263
264 if (number == 0) { return 1; }
265 else {
266 String value = (new Double(number)).toString();
267
268 final short separatorAt = (short) value.indexOf(separator);
269
270 if (separatorAt > -1) {
271 value = value.substring(separatorAt + 1);
272 }
273
274 final short exponentAt = (short) value.indexOf(EXPONENT);
275
276 if (exponentAt > 0) {
277 value = value.substring(0, exponentAt);
278 }
279
280 final Long longValue = new Long(value).longValue();
281
282 if (Math.abs(number) < 1) {
283 return (byte) longValue.toString().length();
284 } else if (longValue == 0) {
285 return 0;
286 } else {
287 return (byte) (("0." + value).length() - 2);
288 }
289 }
290 }
291
292
300 public static double power(final int basis, final short exponent) {
301 return power((short) basis, exponent);
302 }
303
304
312 public static double power(final short basis, final short exponent) {
313 if (basis == 0) {
314 return (exponent != 0) ? 1 : 0;
315 } else {
316 if (exponent == 0) {
317 return 1;
318 } else {
319 // The Math method power does change the least significant
320 // digits after the decimal separator and is therefore useless.
321 double result = 1;
322 short s = 0;
323
324 if (exponent > 0) {
325 for (; s < exponent; s++) {
326 result *= basis;
327 }
328 } else if (exponent < 0) {
329 for (s = exponent; s < 0; s++) {
330 result /= basis;
331 }
332 }
333
334 return result;
335 }
336 }
337 }
338
339
347 public static double round(final byte significantsAfter,
348 final char separator,
349 final double number) {
350
351 if (number == 0) { return 0; }
352 else {
353 final double constant = power(10, (short)
354 (findInsignificantZerosAfterDecimal(separator, number)
355 + significantsAfter));
356 final short dExponent = findExponent(number);
357
358 short exponent = dExponent;
359
360 double value = number*constant*Math.pow(10, -exponent);
361 final String exponentSign =
362 (exponent < 0) ? String.valueOf(StringUtils.DASH) : "";
363
364 if (exponent != 0) {
365 exponent = (short) Math.abs(exponent);
366
367 value = round(value);
368 } else {
369 value = round(value)/constant;
370 }
371
372
374 exponent -= Math.signum(dExponent)*(findSignificantDigits
375 (significantsAfter, separator, value) - 1);
376
377 if (dExponent != 0) {
378 String strValue = Double.toString(value);
379
380 strValue = strValue.substring(0, strValue.indexOf(separator))
381 + EXPONENT + exponentSign + Short.toString(exponent);
382
383 value = new Double(strValue);
384 }
385
386 return value;
387 }
388 }
389
390
396 public static double round(final double value) {
397 return (long) (value + .5);
398 }
399
400
408 public static String roundToString(final byte significantDigits,
409 final char separator,
410 double dNumber) {
411
412
413 final byte significantsBefore =
414 findSignificantsBeforeDecimal(separator, dNumber);
415
416 final byte significantsAfter = findSignificantsAfterDecimal(
417 significantsBefore, significantDigits);
418
419 final double rounded = MathsUtils.round(significantsAfter, separator, dNumber);
420
421 final String exponent = findExponent((new Double(rounded)).toString());
422 final String mantissa = findMantissa(separator,
423 (new Double(rounded)).toString());
424
425 final double dMantissa = new Double(mantissa).doubleValue();
426 final StringBuilder result = new StringBuilder(mantissa);
427
428 final byte significants = findSignificantDigits(significantsAfter,
429 separator, dMantissa);
430
431 if (significants <= significantDigits) {
432 if (significantsAfter != 0) {
433 result.append(ZEROS.substring(0,
434 calculateMissingSignificantZeros(significantsAfter,
435 separator, dMantissa)));
436 } else {
437
438 final short decimal = (short) result.indexOf(
439 new Character(separator).toString());
440
441 if (decimal > -1) {
442 result.setLength(decimal);
443 }
444 }
445 } else if (significantsBefore > significantDigits) {
446 dNumber /= power(10, (short) (significantsBefore - significantDigits));
447
448 dNumber = round(dNumber);
449
450 final short digits =
451 (short) (significantDigits + ((dNumber < 0) ? 1 : 0));
452
453 final String strDouble = (new Double(dNumber)).toString().substring(0, digits);
454
455 result.setLength(0);
456 result.append(strDouble + ZEROS.substring(0,
457 significantsBefore - significantDigits));
458 }
459
460 if (new Short(exponent) != 0) {
461 result.append(EXPONENT + exponent);
462 }
463
464 return result.toString();
465 }
466
467
475 public static String roundToString(final char separator,
476 final int significantDigits,
477 float value) {
478
479 return roundToString((byte)significantDigits, separator,
480 (double)value);
481 }
482 }
The code is tested with the following JUnit test:
Computer code Code listing 3.22: MathsUtilsTest.java
1 package string;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6
7 import java.util.Vector;
8
9 import org.junit.Test;
10
11
17 public class MathsUtilsTest {
18
19
25 private static void addValue(final double d, Vector<Double> values) {
26 values.add(-d);
27 values.add(d);
28 }
29
30
31
32
35 @Test
36 public void testRoundToStringDoubleByteCharDouble() {
37
38 final Vector<Double> values = new Vector<Double>();
39 final Vector<String> strValues = new Vector<String>();
40
41 values.add(0.0);
42 strValues.add("0.00000");
43 addValue(1.4012984643248202e-45, values);
44 strValues.add("-1.4012E-45");
45 strValues.add("1.4013E-45");
46 addValue(1.999999757e-5, values);
47 strValues.add("-1.9999E-5");
48 strValues.add("2.0000E-5");
49 addValue(1.999999757e-4, values);
50 strValues.add("-1.9999E-4");
51 strValues.add("2.0000E-4");
52 addValue(1.999999757e-3, values);
53 strValues.add("-0.0019999");
54 strValues.add("0.0020000");
55 addValue(0.000640589, values);
56 strValues.add("-6.4058E-4");
57 strValues.add("6.4059E-4");
58 addValue(0.3396899998188019, values);
59 strValues.add("-0.33968");
60 strValues.add("0.33969");
61 addValue(0.34, values);
62 strValues.add("-0.33999");
63 strValues.add("0.34000");
64 addValue(7.07, values);
65 strValues.add("-7.0699");
66 strValues.add("7.0700");
67 addValue(118.188, values);
68 strValues.add("-118.18");
69 strValues.add("118.19");
70 addValue(118.2, values);
71 strValues.add("-118.19");
72 strValues.add("118.20");
73 addValue(123.405009, values);
74 strValues.add("-123.40");
75 strValues.add("123.41");
76 addValue(30.76994323730469, values);
77 strValues.add("-30.769");
78 strValues.add("30.770");
79 addValue(130.76994323730469, values);
80 strValues.add("-130.76");
81 strValues.add("130.77");
82 addValue(540, values);
83 strValues.add("-539.99");
84 strValues.add("540.00");
85 addValue(12345, values);
86 strValues.add("-12344");
87 strValues.add("12345");
88 addValue(123456, values);
89 strValues.add("-123450");
90 strValues.add("123460");
91 addValue(540911, values);
92 strValues.add("-540900");
93 strValues.add("540910");
94 addValue(9.223372036854776e56, values);
95 strValues.add("-9.2233E56");
96 strValues.add("9.2234E56");
97
98 byte i = 0;
99 final byte significants = 5;
100
101 for (final double element : values) {
102 final String strValue;
103
104 try {
105 strValue = MathsUtils.roundToString(significants, StringUtils.PERIOD, element);
106
107 System.out.println(" MathsUtils.round(" + significants + ", '"
108 + StringUtils.PERIOD + "', " + element + ") ==> "
109 + strValue + " = " + strValues.get(i));
110 assertEquals("Testing roundToString", strValue, strValues.get(i++));
111 } catch (final Exception e) {
112 // TODO Auto-generated catch block
113 e.printStackTrace();
114 }
115 }
116 }
117
118 }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.