### Exercise Solutions

 1. (a) Int    (b) Double    (c) Double    (d) Char    (e) String (the REPL will say java.lang.String)    (f) Int    (g) Double    (h) Double    (i) Int2. (a)        Carry Bits        11111    First Operand     10101101    Second Operand    11010100                      --------    Sum              110000001Note that the top bit, which is struck through, is lost in 8-bit arithmetic.(b)        Carry Bits         11111    First Operand     00111110    Second Operand    00111011                      --------    Sum               01111001(c)    First Operand         01001010    Second Operand        00110010                          --------    Partial results      010010100                      010010100000                     0100101000000                     -------------                      111001110100Again, the bits that are struck through would be lost in 8-bit arithmetic.3. (a) 7 (3,1) = 00000111 = 07 hex = 007 octal    (b) 18 (9,4,2,1) = 00010010 = 12 hex = 022 octal    (c) 57 (28, 14, 7, 3, 1) = 00111001 = 39 hex = 071 octal    (d) 93 (46, 23, 11, 5, 2, 1) = 01011101 = 5D hex = 135 octal    (e) 196 (98, 49, 24, 12, 6, 3, 1) = 11000100 = C4 hex, 304 octalThe values in parentheses are the quotients from repeatedly dividing by 2.4. (a) 0x35 = 00110101 = 3*16+5 = 53    (b) 0x96 = 10010110 = 9*16+6 = 150    (c) 0xA8 = 10101000 = 10*16+8 = 168    (d) 0x7F = 01111111 = 7*16+15 = 1275. (a) 87 = 01010111, -87 = 10101000+1 = 10101001    (b) 32 = 00100000, -32 = 11011111+1 = 11100000    (c) 105 = 01101001, -105 = 10010110+1 = 10010111    (d) 1 = 00000001, -1 = 11111110+1 = 111111116. println("How fast is the projectile launched at in m/s?")val speed = readDoubleprintln("What angle is it lanched at in radians?")val angle = readDoubleval vx = speed * math.cos(angle)val vy = speed * math.sin(angle)val g = 9.8val maxHeightTime = vy / gval distance = 2 * maxHeightTime * vxprintln("The projectile hits the ground in " + distance + " meters.")7.scala> // We want 100,000 = x*(1-0.27)scala> 100000/(1-0.27)res0: Double = 136986.301369863The first line is just a comment explaining what I am doing. Note that having 0.27 as a Double causes this whole expression to use Double arithmetic. That is something you need to be careful of. Often I would put ".0" at the end of the integer values to be certain that Double arithmetic is used.8. You should find that toUpperCase gives out a new String where all the letter characters are in upper case. If you make a String that starts or ends with white space, the trim method will remove it. The substring method gives you a new String that is a part of the originally starting at the index of the first argument and going to one before the index of the second argument. The replace method gives back a new String in which all occurrences of the first argument have been replaced with the second argument.9. scala> val mercurysSemiMajorAxis = 0.387098mercurysSemiMajorAxis: Double = 0.387098scala> val marssSemiMajorAxis = 1.523679marssSemiMajorAxis: Double = 1.523679scala> val neptunesSemiMajorAxis = 30.10366151neptunesSemiMajorAxis: Double = 30.10366151scala> val mercurysOrbitalPeriod = math.pow(mercurysSemiMajorAxis,1.5)mercurysOrbitalPeriod: Double = 0.24084153760758378scala> val marssOrbitalPeriod = math.pow(marssSemiMajorAxis,1.5)marssOrbitalPeriod: Double = 1.8807896358663763scala> val neptunesOrbitalPeriod = math.pow(neptunesSemiMajorAxis,1.5)neptunesOrbitalPeriod: Double = 165.1691687473645210. I am doing this using the document at http://www.irs.gov/pub/irs-pdf/f1040ez.pdf.println("1. Wages, salaries, and tips. This should be shown in box 1 of your Form(s) W-2.")val wages = readDoubleprintln("2. Taxable interest. If the total is over \$1,500, you cannot use Form 1040EZ.")val taxableInterest = readDoubleprintln("3. Unemployment compensation and Alaska Permanent Fund dividends (see instructions).")val unemploymentCompensation = readDoubleval adjustedGrossIncome = wages + taxableInterest + unemploymentCompensationprintln("4. Adjusted Gross Income = " + adjustedGrossIncome)println("5. Enter 9750 if you are single or 19500 is you are married and filing jointly.")val standardDeducation = readDoubleval taxableIncome = adjustedGrossIncome - standardDeducationprintln("6. Your taxable income is " + taxableIncome)println("7. Federal income tax withheld from Form(s) W-2 and 1099.")val withholdings = readDoubleprintln("8a. Earned income credit.")val earnedIncomeCredit = readDoubleprintln("8b. Nontaxable combat pay.")val combatPay = readDoubleval totalPaymentsAndCredits = withholdings + earnedIncomeCredit + combatPayprintln("9. Your total payments and credits are " + totalPaymentsAndCredits)println("10. Look up the value from 6 in the tax table and enter it here.")val tax = readDoubleval paymentOrRefund = tax - totalPaymentsAndCreditsprintln("11. You owe or are owed " + paymentOrRefund + ". A negative value is a refund.")Note that while this script uses the Double type for money, that is only to make things easy at this point. Because Double in inexact, real applications involving money should use the Int type and work in cents, not dollars.11.println("What is your first ingredient?")val name1 = readLineprintln("How many units of " + name1 + " do you need?")val amount1 = readDoubleprintln("How much does " + name1 + " cost per unit?")val cost1 = readDoubleprintln("What is your second ingredient?")val name2 = readLineprintln("How many units of " + name2 + " do you need?")val amount2 = readDoubleprintln("How much does " + name2 + " cost per unit?")val cost2 = readDoubleprintln("What is your third ingredient?")val name3 = readLineprintln("How many units of " + name3 + " do you need?")val amount3 = readDoubleprintln("How much does " + name3 + " cost per unit?")val cost3 = readDoubleval totalCost1 = amount1 * cost1println(name1 + " will cost you " + totalCost1)val totalCost2 = amount2 * cost2println(name2 + " will cost you " + totalCost2)val totalCost3 = amount3 * cost3println(name3 + " will cost you " + totalCost3)val recipeCost = totalCost1 + totalCost2 + totalCost3println("The recipe will cost you " + recipeCost + ".")
Comments