- right triangle pythagorean theorem solver
solve for a, b, or c
// solve c
printRT(solveRT(3, 4)) // 3, 4, 5
val r = new rt(3, 4)
r.print // 3, 4, 0
r.solve.print // 3, 4, 5
// solve a
new rt(0, 12, 13).solve.print // 5, 12, 13
// solve b, read line, space delimited no commas
val r = inputRT // 7 0 25
r.solve.print // 7, 24, 25
// solve c, a = b
var r = new rt(2)
r.print // 2, 2, 0
r.solve.print // 2, 2, 2.8284271247461903
(inputRT).solve.print // 2 => 2, 2, 2.8284271247461903
// scale
new rt(2).solve.scale(2).print // 4, 4, 5.656854249492381
new rt(2).solve.halve.print // 1, 1, 1.4142135623730951
// :paste into repl
import math._
type rt = RightTriangle
class RightTriangle(val a:Double = 0, private var _b:Double = 0, val c:Double = 0)
{
def b = _b
_b = if (_b + c == 0) a else _b
def solve(): RightTriangle =
{
solveRT(this.a, this.b, this.c)
}
def print() =
{
printRT(this)
}
def scale(aScale:Double): RightTriangle =
{
scaleRT(this, aScale)
}
def halve(): RightTriangle =
{
halveRT(this)
}
}
// lib
def solveRT(rt:RightTriangle): RightTriangle =
{
solveRT(rt.a, rt.b, rt.c)
}
def solveRT(a:Double, _b:Double = 0, c:Double = 0): RightTriangle =
{
def fx(hyp:Double, leg:Double): Double =
{
sqrt(pow(hyp, 2) - pow(leg, 2))
}
val b = if (_b + c == 0) a else _b
(a, b, c) match
{
case (0, _, _) => new RightTriangle(fx(c, b), b, c) // solve a
case (_, 0, _) => new RightTriangle(a, fx(c, a), c) // b
case _ => new RightTriangle(a, b, hypot(a, b)) // c
}
}
def halveRT(rt:RightTriangle): RightTriangle =
{
scaleRT(rt, 0.5)
}
def inputRT: RightTriangle =
{
val input:String = io.StdIn.readLine()
val rt:Array[Double] = input.trim().replaceAll(" +", " ").split(" ").map(_.toDouble)
rt.length match
{
case 1 => new RightTriangle(rt(0))
case 2 => new RightTriangle(rt(0), rt(1))
case 3 => new RightTriangle(rt(0), rt(1), rt(2))
}
}
def irt = inputRT
def printRT(rt:RightTriangle)
{
println(f"a: ${rt.a}\nb: ${rt.b}\nc: ${rt.c}")
}
def scaleRT(rt:RightTriangle, scale:Double) =
{
new RightTriangle(rt.a * scale, rt.b * scale, rt.c * scale)
}
- file io
number lines
import java.io._ // PrintWriter
val inFile = io.Source.fromFile("/users/lc/file.txt")
val outFile = new PrintWriter(new File("/users/lc/file.txt.out"))
var i = 0
for (line <- inFile.getLines()) {
i += 1
println(s"$i. $line")
outFile.write("%d. %s\n".format(i, line))
}
outFile.close
inFile.close
- misc
scala.util.Properties.versionString // 2.12.1
val guid = java.util.UUID.randomUUID
guid.toString().filter(!"-".contains(_))
guid.toString().toUpperCase()
"foo bar".getBytes // Array[Byte] = Array(102, 111, 111, 32, 98, 97, 114)
"scala".intersect("language") // String = ala
"FOO".toLowerCase()
1.to(10).foreach (print)
1.to(10).foreach (println)
1.to(10).foreach {x => println(x + ".")}
1.to(10).map(_ * 2) // scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
List(1, 2, 3, 4, 5).map(_ * 10) // List[Int] = List(10, 20, 30, 40, 50)
val d = new java.util.Date // Wed Nov 12 08:50:00 MST 2014
math.pow(3, 2)
import math._
pow(3, 2)
hypot(3, 4) // Double = 5.0
(90:Double).toRadians // Double = 1.5707963267948966
1.5707963267948966.toDegrees // Double = 90.0
(122:Float)/255 // 0.47843137
(122:Double)/255 // 0.47843137254901963
122/255.0 // 0.47843137254901963
"decode%20url".replaceAll("%20", " ")
val a:Double = 2.0
println(s"a: $a") // 2.0
println(f"a: $a") // 2.0
println(f"a: $a%g") // 2.00000
println(f"a: $a%.1g") // 2
println(f"a: $a%.2g") // 2.0
println(f"a: $a%.3g") // 2.00
- java
java -version # 12.0.1, 04-16-2019
# /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java
# temp files: /Users/*/Library/Application Support/Oracle/Java/Deployment/cache
// javac Hello.java
// java Hello
//
public class Hello {
public static void main(String[] args) {
System.out.println("hello Java");
}
}