Academia.eduAcademia.edu

Fundamentals of Java Programming

2017

https://doi.org/10.1007/978-3-319-89491-1

Abstract

Fundamentals of Java Programming By Mitsunori Ogihara

References (135)

  1. There are four syntax errors in the code:
  2. the String literal in line 7 does not have a closing double quotation mark, 3. the colon at the end of line 7 should be a semicolon, and 4. There should be one more "}" at the end. At the compilation step, we encounter the following error messages: BuggyHelloWorld.java:2: error: '{' expected public class BuggyHelloWorld BuggyHelloWorld.java:8: error: unclosed string literal System.out.pritnln( "Hello, World! );
  3. BuggyHelloWorld.java:8: error: ';' expected System.out.pritnln( "Hello, World! );
  4. BuggyHelloWorld.java:9: error: illegal start of expression System.out.printin( Hello, Class!" );
  5. BuggyHelloWorld.java:9: error: ';' expected System.out.printin( Hello, Class!" );
  6. BuggyHelloWorld.java:9: error: ')' expected System.out.printin( Hello, Class!" );
  7. BuggyHelloWorld.java:9: error: unclosed string literal System.out.printin( Hello, Class!" );
  8. BuggyHelloWorld.java:10: error: ';' expected System.out.printin( "Hello, its' me!" ): BuggyHelloWorld.java:10: error: ';' expected System.out.printin( "Hello, its' me!" ): BuggyHelloWorld.java:13: error: class, interface, or enum expected } 10 errors Each error message consists of the source file name, theHere is an execution example: 1 Enter one number: 4
  9. 45 System . out . printf ( " Your input :% n%s% nThe counts :% n" , input );
  10. 46 printInfo ( " Period " , nPeriod );
  11. 47 printInfo ( " Comma " , nComma );
  12. 48 printInfo ( " Question Mark " , nQuestion );
  13. 49 printInfo ( " Exclamation Mark " , nExclamation );
  14. 50 printInfo ( " Colon " , nColon );
  15. 51 printInfo ( " Semicolon " , nSemicolon );
  16. 52 printInfo ( " Others " , nOthers );
  17. 53 } 54 } Listing 10.9 A program that counts punctuations (part 4). The part for generating a report 10.2 Using a char Data in a Switch-Statement Round=015,Value=1.414260864257813
  18. Round=016,Value=1.414222717285156
  19. Round=017,Value=1.414203643798828
  20. Round=018,Value=1.414213180541992
  21. Round=019,Value=1.414217948913574
  22. Round=020,Value=1.414215564727783
  23. Round=021,Value=1.414214372634888
  24. Round=022,Value=1.414213776588440
  25. Round=023,Value=1.414213478565216
  26. Round=024,Value=1.414213627576828
  27. Round=025,Value=1.414213553071022
  28. Round=026,Value=1.414213590323925
  29. Round=027,Value=1.414213571697474
  30. Round=028,Value=1.414213562384248
  31. Round=029,Value=1.414213557727635
  32. Round=030,Value=1.414213560055941
  33. Round=031,Value=1.414213561220095
  34. Round=032,Value=1.414213561802171
  35. Round=033,Value=1.414213562093210
  36. Round=034,Value=1.414213562238729
  37. Round=035,Value=1.414213562311488
  38. Round=036,Value=1.414213562347868
  39. Round=037,Value=1.414213562366058
  40. Round=038,Value=1.414213562375153
  41. Round=039,Value=1.414213562370605
  42. Round=040,Value=1.414213562372879
  43. Round=041,Value=1.414213562374016
  44. Round=042,Value=1.414213562373448
  45. Round=043,Value=1.414213562373163
  46. Round=044,Value=1.414213562373021
  47. Round=045,Value=1.414213562373092
  48. Round=002,Value=0.875000000000000
  49. Round=003,Value=1.312500000000000
  50. Round=004,Value=1.531250000000000
  51. Round=005,Value=1.640625000000000
  52. Round=006,Value=1.695312500000000
  53. Round=007,Value=1.722656250000000
  54. Round=008,Value=1.736328125000000
  55. Round=009,Value=1.729492187500000
  56. Round=010,Value=1.732910156250000
  57. Round=011,Value=1.731201171875000
  58. Round=012,Value=1.732055664062500
  59. Round=013,Value=1.731628417968750
  60. Round=014,Value=1.731842041015625
  61. Round=015,Value=1.731948852539063
  62. Round=016,Value=1.732002258300781
  63. Round=017,Value=1.732028961181641
  64. Round=018,Value=1.732042312622070
  65. Round=019,Value=1.732048988342285
  66. Round=020,Value=1.732052326202393
  67. Round=021,Value=1.732050657272339
  68. Round=022,Value=1.732051491737366
  69. Round=023,Value=1.732051074504852
  70. Round=024,Value=1.732050865888596
  71. Round=025,Value=1.732050761580467
  72. Round=026,Value=1.732050813734531
  73. Round=027,Value=1.732050787657499
  74. Round=028,Value=1.732050800696015
  75. Round=029,Value=1.732050807215273
  76. Round=030,Value=1.732050810474902
  77. Round=031,Value=1.732050808845088
  78. Round=032,Value=1.732050808030181
  79. Round=033,Value=1.732050807622727
  80. Round=034,Value=1.732050807419000
  81. Round=035,Value=1.732050807520864
  82. Round=036,Value=1.732050807571795
  83. Round=037,Value=1.732050807546330
  84. Round=038,Value=1.732050807559062
  85. Round=039,Value=1.732050807565429
  86. Round=040,Value=1.732050807568612
  87. Round=041,Value=1.732050807570204
  88. Round=042,Value=1.732050807569408
  89. Round=043,Value=1.732050807569010
  90. Round=044,Value=1.732050807568811
  91. Round=045,Value=1.732050807568911
  92. Round=046,Value=1.732050807568861
  93. Root=1.732050807568861, Square=2.999999999999943 Enter your guess: 123 No.Hits=0, No.Misses=0 Enter your guess: 456 No.Hits=2, No.Misses=0 Enter your guess: 457 No.Hits=1, No.Misses=1 Enter your guess: 467 No.Hits=0, No.Misses=2 Enter your guess: 478 No.Hits=0, No.Misses=1 Enter your guess: 567 No.Hits=0, No.Misses=3 Enter your guess: 756
  94. Congratulations! You've guessed it right! 12 Arrays 53:241 54:251 55:257 56:263 57:269 58:271 59:277 60:281 61:283 62:293 63:307 64:311 65:313 66:317 67:331 68:337 69:347 70:349 71:353 72:359 73:367 74:373 75:379 76:383 77:389 78:397 79:401 80:409 81:419 82:421 83:431 84:433 85:439 86:443 87:449 88:457 89:461 90:463 91:467 92:479 93:487 94:491 95:499 96:503 97:509 98:521 99:523 100:541 101:547 102:557 103:563 104:569 105:571 106:577 107:587 108:593 109:599 110:601 111:607 112:613 113:617 114:619 115:631 116:641 117:643 118:647 119:653 120:659 121:661 122:673 123:677 124:683 125:691 126:701 127:709 128:719 129:727 130:733 131:739 132:743 133:751 134:757 135:761 136:769 137:773 138:787 139:797 140:809 141:811 142:821 143:823 144:827 145:829 146:839 147:853 148:857 149:859 150:863 151:877 152:881 153:883 154:887 155:907 156:911 157:919 158:929 159:937 160:941 161:947 162:953 163:967 164:971 165:977 166:983 167:991 168:997 12.5 String Methods That Return an Array 317 23 4 Emily 81.00
  95. Instantiate a new temporary array, say newArray.
  96. For all indexes i that are strictly smaller than p, copy oldArray[ i ] to newArray[ i ].
  97. Place x in newArray[ p ].
  98. For all indexes i that are strictly greater than p, copy oldArray[ i ] to newArray[ i + 1 ].
  99. Assign newArray to oldArray. Figure 13.5 visualizes this action. In the middle three steps, there is no overlap among the destinations of the elements, so the orders of the three steps can be permuted. To remove the element at some index p, we execute the following algorithm:
  100. Instantiate a new temporary array with a different name, say newArray.
  101. For all indexes i that are strictly smaller than p, copy oldArray[ i ] to newArray[ i ].
  102. 3 Joanne Brackeen (born July 26, 1938) is an American jazz pianist and composer. Terri Lyne Carrington (born August 4, 1965) is an American jazz drummer, composer, and producer. Carmen Mercedes McRae (April 8, 1922 to November 10, 1994) was an American jazz singer. Linda May Oh (born 1984 in Malaysia) is a Jazz bassist and composer. Esperanza Emily Spalding (born October 18, 1984) is an American jazz bassist and singer. Blossom Dearie (April 28, 1924 to February 7, 2009) was an American jazz singer, composer, and pianist.
  103. 1 Rectangular Arrays 14.1.1 Defining Multi-Dimensional Arrays Arrays may have more than one dimension. We call arrays having more than one dimension multi- dimensional arrays. For an integer N ≥ 1, an N -dimensional array as a type is declared with N pairs of brackets []. In the following code, mDouble is declared as a two-dimensional array of double and myFlags is declared as a three-dimensional array of boolean. 1 double [][] myDouble ;
  104. myFlags ; In an instantiation of a multi-dimensional array, the length must be specified for at least one dimension, but not necessarily for all of them. In the following code, the first line instantiates a two- dimensional array whose first dimension has length 11 and whose second dimension has length 35, and the second line instantiates a three-dimensional array whose first dimension has length 3. In the second array, the three elements myFlags[ 0 ], myFlags[ 1 ], and myFlags[ 2 ] are expected to be two-dimensional arrays, but they are presently null and so their shapes are unknown yet. 3 myDouble = new double [ 11 ][ 35 ];
  105. In an instantiation of a multi-dimensional array, if one dimension is without length specification, so must be its subsequent dimensions. Therefore, myDouble = new double [][ 7 ];
  106. myFlags = new boolean [ 5 ][][ 4 ];
  107. are both syntactically incorrect.
  108. M. Ogihara, Fundamentals of Java Programming, https://doi.org/10.1007/978-3-319-89491-1_14
  109. Enter two paths : tmp1 tmp2
  110. ----Their initial state 6 Existence : tmp1 = false , tmp2 = false 7
  111. ----Create File1 -> true
  112. ----Create File2 -> true 10 Existence : tmp1 = true , tmp2 = true 11 12 ----Rename File1 to File1 -> true 13 ----Rename File1 to File2 -> true 14 Existence : tmp1 = false , tmp2 = true 15 16 ----Create File1 as Directory -> true 17 ----Create File2 as Directory -> false 18 Existence : tmp1 = true , tmp2 = true 19 20 ----Delete File1 -> true
  113. Parent: Documents
  114. Absolute Path: /Users/nancy/Documents
  115. Parent's File List
  116. ------------------ 8 Index: Exec? Read?Write?IsDir?IsFil? Size Name 9 00000: false true true false true 18436 .DS_Store 10 00001: false true true false true 0 .localized 11 00002: true true true true false 476 archives 12 00003: true true true true false 204 classical.txt 13 00004: true true true true false 136 cranium 14 00005: true true true true false 714 CSC120 15 00006: true true true true false 578 CSC527 16 00007: true true true true false 204 easy 17 00008: false true true false true 132102 eceSeminarOct2017.docx 18 00009: true true true true false 272 fiances 19 00010: true true true true false 306 frank 20 00011: true true true true false 612 globaltheme 21 00012: true true true true false 204 india.txt 22 00013: true true true true false 102 letters 23 00014: true true true true false 204 Microsoft User Data 24 00015: true true true true false 204 MitsuCollection.txt 25 00016: false true true false true17438298 Mult_Pattern_2_EdXX.docx 26 00017: true true true true false 204 MyCollection.txt 27 00018: true true true true false 170 papers 28 00019: true true true true false 782 pdfs 29 00020: true true true true false 340 Projects 30 00021: true true true true false 1020 Resume 31 00022: true true true true false 442 reviews 32 00023: true true true true false 578 temp 33 00024: true true true true false 374 temporary 'f':35 'g':-27 'h':-39 'i':8 'j':2 'k':-21 'l':-37 'm':-22 'n':-9 'o':13 'p':7 'q':-1 'r':-16 's':-31 't':-30 'u':-29 'v':-3 'w':-1 'x':3 'y':-27 '{':5 '|':-2 '}':5 17 Enter your choice: 1 18 Enter account index: 0
  117. ------ 21 0: My Saving has the balance of $150,000.00 22 1: My Checking has the balance of $1,000.00 23 2: Her Saving has the balance of $200,000.00
  118. Enter account origination index: 2 31 Enter account destination index: 1 32 Enter amount: 5000000
  119. ------ 34 0: My Saving has the balance of $150,000.00 35 1: My Checking has the balance of $51,000.00 36 2: Her Saving has the balance of $150,000.00
  120. Quit 42 Enter your choice: 2 43 Enter account index: 2 44 Enter amount: 999900
  121. ------ 46 0: My Saving has the balance of $150,000.00 47 1: My Checking has the balance of $51,000.00 48 2: Her Saving has the balance of $140,001.00
  122. % java PizzaComplexMain 2 Enter data file name: pizzaComplexData.txt 3 Enter your choice by first letter 4 View, Add, Delete, Search, Quit: V 5 0:Four Cheese:$14.00:Mozzarella, Parmesan, Ricotta, Gorgonzola 6 1:Pepperoni:$10.00:Pepperoni 7 2:Nettuno:$14.00:Tuna, Pasta Sauce, Onion, Green Pepper 8 3:Capricciosa:$14.50:Olive, Ham, Mozzarella, Artichoke 9 4:Meat Lovers:$15.00:Pepperoni, Ham, Sausage, Bacon, Mozzarella 10 5:Veggie:$12.50:Onion, Green Pepper, Tomato, Mozzarella, Olive 11 Enter your choice by first letter 12 View, Add, Delete, Search, Quit: A 13 Enter name: Hawaiian 14 Enter price: 13.00 15 Enter ingredients separated by comma: Ham,Pineapple 16 Enter your choice by first letter 17 View, Add, Delete, Search, Quit: V 18 0:Four Cheese:$14.00:Mozzarella, Parmesan, Ricotta, Gorgonzola 19 1:Pepperoni:$10.00:Pepperoni 20 2:Nettuno:$14.00:Tuna, Pasta Sauce, Onion, Green Pepper 21 3:Capricciosa:$14.50:Olive, Ham, Mozzarella, Artichoke 22 4:Meat Lovers:$15.00:Pepperoni, Ham, Sausage, Bacon, Mozzarella 23 5:Veggie:$12.50:Onion, Green Pepper, Tomato, Mozzarella, Olive 24 6:Hawaiian:$13.00:Ham, Pineapple 25 Enter your choice by first letter 26 View, Add, Delete, Search, Quit: S 27 Enter key: ham 28 3:Capricciosa:$14.50:Olive, Ham, Mozzarella, Artichoke 29 4:Meat Lovers:$15.00:Pepperoni, Ham, Sausage, Bacon, Mozzarella 30 6:Hawaiian:$13.00:Ham, Pineapple 31 Enter your choice by first letter 32 View, Add, Delete, Search, Quit: Q ----Choose action to be performed----
  123. choice: 1 Enter the name: 2 Atkins Road Enter the price: 280000 ----Choose action to be performed----
  124. choice: 4 0: 1 Presidential Place : $1,535,000 1: 10 Wilkinson Road : $343,450 2: 11 Wilkinson Drive : $766,000 3: 2 Atkins Road : $280,000 4: 2 Marigold Terrace : $615,000 5: 34 Coral Way : $807,500 6: 7000 Flamingo Drive : $790,000 7: 71 Canary Drive : $199,700 8: 7900 Plainview Drive : $195,500
  125. ----Choose action to be performed----
  126. choice: 2 Enter the position: 2 ----Choose action to be performed----
  127. ----Choose action to be performed----
  128. choice : 8 Enter the position : 7 The item is : 7900 Plainview Drive : $195 ,500 Enter the new price : 205000 ----Choose action to be performed ----
  129. ----Choose action to be performed ----
  130. choice : 10 Enter an output file path : propertyData2 . txt ----Choose action to be performed ----
  131. choice : 0 ... Terminating 19 Online and Recursive Algorithms 9:3.628800e+05 10:3.628800e+06 ... 140:1.346201e+241 141:1.898144e+243 142:2.695364e+245 143:3.854371e+247 144:5.550294e+249 145:8.047926e+251 146:1.174997e+254 147:1.727246e+256 148:2.556324e+258 149:3.808923e+260 150:5.713384e+262 The program can compute the approximate factorial if n as large as 170, which is the limit. % java FactorialDouble Enter a positive integer: 173 ... 151:8.627210e+264 152:1.311336e+267 153:2.006344e+269 154:3.089770e+271 155:4.789143e+273 156:7.471063e+275 157:1.172957e+278 158:1.853272e+280 159:2.946702e+282 160:4.714724e+284 161:7.590705e+286 162:1.229694e+289 163:2.004402e+291 164:3.287219e+293 165:5.423911e+295 166:9.003692e+297 167:1.503617e+300 168:2.526076e+302 169:4.269068e+304 170:7.257416e+306 171:Infinity 172:Infinity 173:Infinity % Infinity is a special value of the boxed class Double that represents the positive infinity.
  132. The "size" of each input to the problem can be measured as a nonnegative integer.
  133. Declare a long variable, returnValue, and store 1 in it (Line 7).
  134. If n > 1, replace the value of returnValue with the product of compute( n -1 ) and n (Lines 8-11). A recursive call appears in Line 10.
  135. Print the values of n and returnValue using the format n=%-4dn!=%30d.