Setting up Scala for IntelliJ is very easy

Image for post
Image for post
Photo by Manuel Nägeli on Unsplash

Getting the Scala for IntelliJ plugin

Image for post
Image for post
A selection of featured plugins on the IntelliJ Marketplace. The Scala plugin is the very first, it’s free, and it’s already installed on the computer on which this screenshot was taken.

Getting IntelliJ and Scala in one click

Verifying that the plugin actually works

Image for post
Image for post
Select Scala on the left, IDEA on the right. I don’t recommend the sbt option. No comment on Lightbend at this time.
Image for post
Image for post
Check the project location, JDK, Scala SDK, click Finish.
Image for post
Image for post
Image for post
Image for post
package helloworldobject HelloWorld {  def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
Image for post
Image for post
IntelliJ just after you change the Scala class to a Scala object, but before you type in the main procedure.
Image for post
Image for post
package currencyimport java.util.Currencyobject CurrencyAmount {  val UNITED_STATES_DOLLARS: Currency = Currency.getInstance("USD")}class CurrencyAmount(cents: Long,
currencyID: Currency =
CurrencyAmount.UNITED_STATES_DOLLARS)
extends Ordered[CurrencyAmount] {
val amountInCents: Long = cents
val currencyIdentifier: Currency = currencyID
// THIS WILL FAIL SOME OF THE TESTS
override def toString: String = {
val pointPlace = this.currencyIdentifier.getSymbol.length +
Math.ceil(Math.log10(Math.abs(this.amountInCents))).toInt -
this.currencyIdentifier.getDefaultFractionDigits +
(if (this.amountInCents < 0) 1 else 0)
(this.currencyID.getSymbol +
this.amountInCents.toString).patch(pointPlace, ".", 0)
}
override def equals(obj: Any): Boolean = obj match {
case obj: CurrencyAmount =>
this.currencyIdentifier == obj.currencyIdentifier &&
this.amountInCents == obj.amountInCents
case _ => false
}
override def hashCode: Int = this.currencyID.hashCode * 65536 +
this.amountInCents.toInt
// STUB TO FAIL THE TEST
def +(summand: CurrencyAmount): CurrencyAmount = new
CurrencyAmount(0, this.currencyID)
// STUB TO FAIL THE TEST
def unary_- = new CurrencyAmount(0, this.currencyID)
def -(subtrahend: CurrencyAmount): CurrencyAmount = this +
(-subtrahend)
// STUB TO FAIL THE TEST
def *(multiplier: Int): CurrencyAmount = new
CurrencyAmount(0, this.currencyID)
// STUB TO FAIL THE TEST
def /(divisor: Int): CurrencyAmount = new
CurrencyAmount(0, this.currencyID)
override def compare(that: CurrencyAmount): Int = {
val diff = this - that
diff.amountInCents match {
case 0 => 0
case n if n < 0 => -1
case _ => 1
}
}
}
package currencyclass CurrencyConversionNeededException(msg: String,
amtA: CurrencyAmount,
amtB: CurrencyAmount)
extends RuntimeException(msg) {
val amountA: CurrencyAmount = amtA
val amountB: CurrencyAmount = amtB
val exchangeRate: Double = 0.5 // TODO: Get actual exchange rate
def exchangeAToB: CurrencyAmount = {
val exchangeAmount = Math.floor(exchangeRate *
amountA.amountInCents).toLong
new CurrencyAmount(exchangeAmount, amountB.currencyIdentifier)
}
def exchangeBToA: CurrencyAmount = {
val exchangeAmount = Math.floor(amountB.amountInCents /
exchangeRate).toLong
new CurrencyAmount(exchangeAmount, amountA.currencyIdentifier)
}
}
    @Test
public void testGetMessage() {
System.out.println("getMessage");
String message = "Conversion needed for euros, dollars";
try {
throw new CurrencyConversionNeededException(message,
euroAmount, dollarAmount);
} catch (CurrencyConversionNeededException currConvExc) {
assertEquals(message, currConvExc.getMessage());
}
}
package currencyimport java.util.Currencyimport org.junit.Test
import org.junit.Assert._
class CurrencyAmountTest { private val bucks = Currency.getInstance("USD") @Test def testToString(): Unit = {
println("toString")
val someAmount = new CurrencyAmount(5322L, bucks)
val expected = "$53.22"
val actual = someAmount.toString
assertEquals(expected, actual)
}
@Test def testToString8Cents(): Unit = {
val someAmount = new CurrencyAmount(8L, bucks)
val expected = "$0.08" // Or, if you prefer, "$.08"
val actual = someAmount.toString
assertEquals(expected, actual)
}
@Test def testToString47Cents(): Unit = {
val someAmount = new CurrencyAmount(47L, bucks)
val expected = "$0.47" // Or, if you prefer, "$.47"
val actual = someAmount.toString
assertEquals(expected, actual)
}
@Test def testToStringPowerOfTen(): Unit = {
val someAmount = new CurrencyAmount(100000000L, bucks)
val expected = "$1000000.00"
val actual = someAmount.toString
assertEquals(expected, actual)
}
@Test def testToStringNegativeAmounts(): Unit = {
val someAmount = new CurrencyAmount(-380L, bucks)
val expected = "$-3.80" // Or, if you prefer, "-$3.80"
val actual = someAmount.toString
assertEquals(expected, actual)
}
@Test def testEquals(): Unit = {
println("equals")
val someAmount = new CurrencyAmount(5322L, bucks)
val sameAmount = new CurrencyAmount(5322L, bucks)
assertEquals(someAmount, sameAmount)
val diffAmount = new CurrencyAmount(239L, bucks)
assertNotEquals(someAmount, diffAmount)
}
@Test def testHashCode(): Unit = {
println("hashCode")
val startNum = Math.floor(Math.random() * 1000).toInt
val len = Math.floor(Math.random() * 1000).toInt
val endNum = startNum + len - 1
val amounts = for (cents <- startNum to endNum)
yield new CurrencyAmount(cents, bucks)
val hashes = amounts.map(_.hashCode).toSet
assertEquals(len, hashes.size)
}
@Test def testPlus(): Unit = {
println("+")
val summandA = new CurrencyAmount(4799L, bucks)
val summandB = new CurrencyAmount(5201L, bucks)
val expected = new CurrencyAmount(10000L, bucks)
val actual = summandA + summandB
assertEquals(expected, actual)
}
@Test(expected = classOf[CurrencyConversionNeededException])
def testCannotAddDifferentCurrencies(): Unit = {
val euros = Currency.getInstance("EUR")
val summandA = new CurrencyAmount(4799L, euros)
val summandB = new CurrencyAmount(5201L, bucks)
print("Attempting to add " + summandA.toString + " to " +
summandB.toString)
val result = summandA + summandB
println(" somehow gave result " + result.toString)
}
@Test def testMinus(): Unit = {
println("-")
val minuend = new CurrencyAmount(8998L, bucks)
val subtrahend = new CurrencyAmount(8489L, bucks)
val expected = new CurrencyAmount(509L, bucks)
val actual = minuend - subtrahend
assertEquals(expected, actual)
}
@Test def testCompare(): Unit = {
println("compare")
val negBal = new CurrencyAmount(-13400L, bucks)
val amountA = new CurrencyAmount(4359L, bucks)
val amountB = new CurrencyAmount(70329L, bucks)
val amountC = new CurrencyAmount(12500000000L, bucks)
var assertionMessage = negBal.toString +
" should be found to be less than " + amountA.toString
assertTrue(assertionMessage, negBal < amountA)
assertionMessage = amountA.toString +
" should be found to be less than " + amountB.toString
assertTrue(assertionMessage, amountA < amountB)
assertionMessage = amountB.toString +
" should be found to be less than " + amountC.toString
assertTrue(assertionMessage, amountB < amountC)
assertionMessage = amountC.toString +
" should be found to be more than " + amountB.toString
assertTrue(assertionMessage, amountC > amountB)
assertionMessage = amountB.toString +
" should be found to be more than " + amountA.toString
assertTrue(assertionMessage, amountB > amountA)
assertionMessage = amountA.toString +
" should be found to be more than " + negBal.toString
assertTrue(assertionMessage, amountA > negBal)
}
@Test def testCompareThroughCollectionSort(): Unit = {
val negBal = new CurrencyAmount(-13400L, bucks)
val zero = new CurrencyAmount(0L, bucks)
val amountA = new CurrencyAmount(4359L, bucks)
val amountB = new CurrencyAmount(70329L, bucks)
val amountC = new CurrencyAmount(12500000000L, bucks)
val unsortedList = List(amountA, negBal, amountC, zero, amountB)
val expected = List(negBal, zero, amountA, amountB, amountC)
val actual = unsortedList.sorted
assertEquals(expected, actual)
}
}

What about NetBeans, Eclipse?

is a composer and photographer from Detroit, Michigan. He has been working on a Java program to display certain mathematical diagrams.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store