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) Int
2. (a)
Carry Bits 11111
First Operand 10101101
Second Operand 11010100
--------
Sum 110000001
Note 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
-------------
111001110100
Again, 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 octal
The 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 = 127
5. (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 = 11111111
6.
println("How fast is the projectile launched at in m/s?")
val speed = readDouble
println("What angle is it lanched at in radians?")
val angle = readDouble
val vx = speed * math.cos(angle)
val vy = speed * math.sin(angle)
val g = 9.8
val maxHeightTime = vy / g
val distance = 2 * maxHeightTime * vx
println("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.301369863
The 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.387098
mercurysSemiMajorAxis: Double = 0.387098
scala> val marssSemiMajorAxis = 1.523679
marssSemiMajorAxis: Double = 1.523679
scala> val neptunesSemiMajorAxis = 30.10366151
neptunesSemiMajorAxis: Double = 30.10366151
scala> val mercurysOrbitalPeriod = math.pow(mercurysSemiMajorAxis,1.5)
mercurysOrbitalPeriod: Double = 0.24084153760758378
scala> val marssOrbitalPeriod = math.pow(marssSemiMajorAxis,1.5)
marssOrbitalPeriod: Double = 1.8807896358663763
scala> val neptunesOrbitalPeriod = math.pow(neptunesSemiMajorAxis,1.5)
neptunesOrbitalPeriod: Double = 165.16916874736452
10. 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 = readDouble
println("2. Taxable interest. If the total is over $1,500, you cannot use Form 1040EZ.")
val taxableInterest = readDouble
println("3. Unemployment compensation and Alaska Permanent Fund dividends (see instructions).")
val unemploymentCompensation = readDouble
val adjustedGrossIncome = wages + taxableInterest + unemploymentCompensation
println("4. Adjusted Gross Income = " + adjustedGrossIncome)
println("5. Enter 9750 if you are single or 19500 is you are married and filing jointly.")
val standardDeducation = readDouble
val taxableIncome = adjustedGrossIncome - standardDeducation
println("6. Your taxable income is " + taxableIncome)
println("7. Federal income tax withheld from Form(s) W-2 and 1099.")
val withholdings = readDouble
println("8a. Earned income credit.")
val earnedIncomeCredit = readDouble
println("8b. Nontaxable combat pay.")
val combatPay = readDouble
val totalPaymentsAndCredits = withholdings + earnedIncomeCredit + combatPay
println("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 = readDouble
val paymentOrRefund = tax - totalPaymentsAndCredits
println("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 = readLine
println("How many units of " + name1 + " do you need?")
val amount1 = readDouble
println("How much does " + name1 + " cost per unit?")
val cost1 = readDouble
println("What is your second ingredient?")
val name2 = readLine
println("How many units of " + name2 + " do you need?")
val amount2 = readDouble
println("How much does " + name2 + " cost per unit?")
val cost2 = readDouble
println("What is your third ingredient?")
val name3 = readLine
println("How many units of " + name3 + " do you need?")
val amount3 = readDouble
println("How much does " + name3 + " cost per unit?")
val cost3 = readDouble
val totalCost1 = amount1 * cost1
println(name1 + " will cost you " + totalCost1)
val totalCost2 = amount2 * cost2
println(name2 + " will cost you " + totalCost2)
val totalCost3 = amount3 * cost3
println(name3 + " will cost you " + totalCost3)
val recipeCost = totalCost1 + totalCost2 + totalCost3
println("The recipe will cost you " + recipeCost + ".")