(c) 2006 Kari Laitinen http://www.naturalprogramming.com 2005-04-08 File created. 2006-02-23 Last modification. The javasolutions folder contains answers to the numbered exercises of the Java book. Below you can find a complete list which describes which .java file is the solution to which exercise. When a file name is suggested in an exercise, you can find the solution in a file with the suggested name. Usually I have put "KL" to the end of the names of my solution files. This way you can use the suggested file name for your own solution without the possibility to mix your program with my program. When the task in an exercise is to modify a certain file, the name of the solution file usually begins with the name of the original file. For example, when the task is to modify a file named Whilesum., you should look for the solution in a file whose name looks like Whilesum??????. The following is the complete list of solutions: EXERCISE SOLUTION FILE 2-1 SumSolution_2_1.java 2-2 SumSolution_2_2.java (See the end of this file to find solutions to the exercises in chapters 3 and 4.) 5-1 GameLoser.java 5-2 GameSolution_5_2.java 5-3 DistanceSolutions.java 5-4 Distancesolutions.java 5-5 HeightToCentimeters.java 5-6 HeightToLightYears.java 5-7 ConvertTemperature.java 6-1 Largest4.java and LargestSmallest4.java 6-2 CurrencyConverterKL.java 6-3 SolutionToExercise_6_3.java 6-4 SolutionToExercise_6_4.java 6-5 SolutionToExercise_6_5.java 6-6 WhileoddKL.java 6-8 MeanvalueSolution_6_8.java 6-9 MilesToKilometersTable.java 6-10 CelsiusToFahrenheitTable.java 6-10 Codefinder.java 7-1 ArrayDemoSolution_7_1.java 7-2 ArrayDemoSolution_7_2.java 7-3 ArrayDemoSolution_7_3.java 7-4 ArrayDemoSolution_7_4.java 7-5 ArrayDemoSolution_7_5.java 7-6 IntegersLargerThan99.java 7-7 EvenNumbersInArray.java 7-8 CelsiusBetter.java 8-1 StringWideReverse.java 8-2 StringCharactersCounted.java 8-3 StringToHexadecimalCodes.java 8-4 StringToUpperCase.java 8-5 StringPalindromeCheck.java 8-6 MarilynSolution_8_6.java 8-7 MarilynSolution_8_7.java 8-8 StringInShrinkingPieces.java 8-9 SplittingSentence.java 8-10 MonthsHistoryKL.java 9-1 LettersAndNumbers.java 9-2 Sums3.java 9-3 FactorialKL.java 9-4 BoxKL.java and BoxMoreElegant.java 9-5 WidecallKL.java 9-6 ReverseAndPrintAnArray.java 9-7 SortingToDescendingOrder.java 9-8 GetSumOfArrayElements.java 9-9 Factorial_9_9.java 9-10 ShowcodeKL.java 10-1 BankBetterSolutions_10_1_and_2.java 10-2 BankBetterSolutions_10_1_and_2.java 10-3 AnimalsSolutions.java 10-4 AnimalsSolutions.java 10-5 AnimalsSolutions.java 10-6 AnimalsSolutions.java 10-7 OlympicsSolution_10_7.java 10-8 No solution available. 10-9 StackSolution_10_9.java 10-10 OlympicsSolution_10_10.java 10-11 Car_10_11.java 11-1 BirthdayDistance.java 11-2 Age10000days.java 11-3 NewYearDaySundays.java 11-4 BirthdaysOnSunday.java 11-5 CalendarSimple.java 11-6 DateISO.java 11-7 DateISO.java 11-8 DateISO.java 11-9 DateISO.java 11-10 DateISO.java 11-11 PresidentsImproved.java 11-12 PresidentsImproved.java 11-13 PresidentsImproved.java 11-14 PresidentsImproved.java 11-15 No solution available. 11-16 No solution available. 11-17 This exercise does not exist in the Java book. 11-18 This exercise does not exist in the Java book. 12-1 BetterDateBetter.java 12-2 NowKL.java 12-3 No solution available. 12-4 CalendarsMore.java 12-5 YearcalendarKL.java 12-6 WindowsSolutions.java 12-7 WindowsSolutions.java 12-8 BankRichPersonAccount.java 12-9 TimesWithTextualTime.java 13-1 ExceptionalNumbersModified.java 13-2 ExceptionalNumbersModified.java 14-1 FileprintCountingCharacters.java 14-2 FilecopyCommand.java 14-3 MirrorKL.java 14-4 EditorSimple.java 14-5 SearchMany.java, SearchManyBetter.java 14-6 FileCompare.java 14-7 FilecopyBinaryKL.java 14-8 Examine.java 14-9 No solution needed. 14-10 CollectSolutions.java 14-11 CollectSolutions.java 14-12 collectSolutions.java 14-13 CollectSolutions.java 14-14 CollectSolutions.java 14-15 No solution available. 15-1 TranslateSolutions.java 15-2 TranslateSolutions.java 15-3 No solution available. 15-4 ReverseWithArrayList.java 15-5 CollectWithArrayList.java 15-6 EventsSolution_15_6.java 15-7 CollectSortable.java 15-8 WeddingdatesGregorianCalendar.java 15-9 EventsGregorianCalendar.java 16-1 EvenoddWithBitOperator.java 16-2 UplowLowercase. 16-3 ShowbitsKL.java 16-4 ShowbitsKL.java 16-5 DotsAndDollarsModified.java 16-6 PlaytimeModified.java 16-7 StopwatchKL.java 16-8 AlarmclockKL.java 16-9 DotsAndDollars_16_9.java SOLUTIONS TO THE EXERCISES PRESENTED IN CHAPTERS 3 AND 4: EXERCISE SOLUTION 3-1 1011 1010 B == 186 == BAH 1010 1011 0110 B == 2742 == AB6H 3-2 AF21H == 1010 1111 0010 0001 B == 44833 B29DH == 1011 0010 1001 1101 B == 45725 3-3 1234 == 0000 0100 1101 0010 B == 4D2H 5678 == 0001 0110 0010 1110 B == 162EH 4-1 9 == 1001 B => A3 == 5V, A2 == 0v, A1 == 0V, A0 == 5V 97 == 0110 0001 B => D7 == 0V, D6 == 5V, D5 == 5V, D4 == 0V, D3 == 0V, D2 == 0V, D1 == 0V, D0 == 5V 4-2 The instruction which occupies memory locations 09H and 0AH could be taken away, and the rest of the instructions could be moved two positions towards the beginning of the memory. The instruction in question (15H) loads the register A with value 6CH, but it is not necessary to load register A with this value because the value in question is already present in register A. 4-3 If 00H (zero) were written to memory location 0FH the program would change so that it would print "Hello". The program stops printing characters from memory when it finds a zero. 4-4 If the second byte of the program in address 01H were changed to 0BH, the program would start printing bytes from address 0BH, and thus the letter e would be the first character to be printed. 4-5 No solution needed. Use the simulator. 4-6 The first instruction of the program should be load_register_a_with_value 9 4-7 The second instruction of the program should be load_register_b_with_value 'H' 4-8 Instruction increment_register_b should be replaced with decrement_register_b. Thus both registers should be decremented inside the loop. 4-9 There should be two output_byte_from_register_b instructions inside the loop. The program should look like beginning_of_program: load_register_a_with_value 5 load_register_b_with_value 'A' print_next_letter: output_byte_from_register_b output_byte_from_register_b increment_register_b decrement_register_a jump_if_register_a_zero end_of_program jump_to_address print_next_letter end_of_program: stop_processing 4-10 If the first instruction would be load_register_a_with_value 'x' the program would print xxxxyyyyzzzz If the latter increment_register_a instruction would be removed, the program would print aaaabbbbbbbb 4-11 The instruction load_register_b_with_value ' ' must be replaced with load_register_b_with_value '.' 4-12 See file solution_exercise_4_12.iml in the icom folder. 4-13 See file solution_exercise_4_13.iml in the icom folder.