This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package src.exercises | |
import scala.math._ | |
import BigInt.probablePrime | |
import util.Random | |
object chap01 { | |
// 1. In the Scala REPL, type 3. followed by the Tab key. What methods can be | |
// applied? | |
// => Do it in REPL. There are many methods including %, &, *, +, toByte, toChar etc. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. The signum of a number is 1 if the number is positive, -1 if it is negative, and | |
// 0 if it is zero. Write a function that computes this value. | |
def signum(n: Int) = { | |
if (n > 0) 1 | |
else if (n < 0) -1 | |
else 0 | |
} //> signum: (n: Int)Int | |
signum(4) //> res0: Int = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Write a code snippet that sets a to an array of n random integers between 0 | |
// (inclusive) and n (exclusive). | |
val a = new Array[Int](10) //> a : Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | |
for(i <- 0 until a.length) a(i) = scala.util.Random.nextInt(10) | |
a //> res0: Array[Int] = Array(9, 0, 5, 8, 6, 6, 3, 9, 0, 3) | |
// 2. Write a loop that swaps adjacent elements of an array of integers. For example, | |
// Array(1, 2, 3, 4, 5) becomes Array(2, 1, 4, 3, 5). | |
val a = Array[Int](1,2,3,4,5) | |
a //> res1: Array[Int] = Array(1, 2, 3, 4, 5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Set up a map of prices for a number of gizmos that you covet. Then produce | |
// a second map with the same keys and the prices at a 10 percent discount. | |
val gizmos = Map("iPod" -> 20000, "iPhone" -> 45000, "iPad" -> 30000, "iMac" -> 85000) | |
//> gizmos : scala.collection.immutable.Map[java.lang.String,Int] = Map(iPod -> | |
//| 20000, iPhone -> 45000, iPad -> 30000, iMac -> 85000) | |
val discountedGizmos = for((k, v) <- gizmos) yield (k, v * 0.9) | |
//> discountedGizmos : scala.collection.immutable.Map[java.lang.String,Double] | |
//| = Map(iPod -> 18000.0, iPhone -> 40500.0, iPad -> 27000.0, iMac -> 76500.0) | |
// 2. Write a program that reads words from a file. Use a mutable map to count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Improve the Counter class in Section 5.1, "Simple Classes and Parameterless | |
// Methods," on page 51 so that it doesn't turn negative at Int.MaxValue. | |
class Counter { | |
private var value = 0 | |
def increment() { if(value < Int.MaxValue) value += 1 } | |
def current = value | |
def isLess(other: Counter) = value < other.value // can access private field of other object | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1. Write an object Conversions with methods inchesToCentimeters, gallonsToLiters, and | |
//milesToKilometers. | |
object Conversions { | |
def inchesToCentimeters(inches: Double) = inches * 2.54 | |
def gallonsToLiters(gallons: Double) = gallons * 3.78541 | |
def milesToKilometers(miles: Double) = miles * 1.60934 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1. Write an example program to demonstrate that | |
//package com.horstmann.impatient | |
//is not the same as | |
//package com | |
//package horstmann | |
//package impatient | |
// File: Demo1.scala | |
package com { | |
package horstmann { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env scala | |
import scala.collection.immutable.IndexedSeq | |
val alphabets = 'A' to 'Z' | |
val insult = if(args.length > 0) args(0).toUpperCase else "STUPID" | |
val heading = alphabets filterNot (insult contains(_)) | |
println(getInsult) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env scala | |
import scala.collection.immutable.IndexedSeq | |
import scala.io.StdIn | |
val version = "Scala Insult Generator v0.1" | |
val cmdArg = if(args.length > 0) Some(args(0).toUpperCase) else None | |
val result = cmdArg match { |
OlderNewer