Advertisement
Guest User

Untitled

a guest
Jul 9th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 5.17 KB | None | 0 0
  1. object Day01 extends App{
  2.  
  3.   println("Age Till 65 Method")
  4.   println("===========================================")
  5.   ageTillSixtyFive("Cooper", 19)
  6.   ageTillSixtyFive("Cooper", 65)
  7.   ageTillSixtyFive("Cooper", 95)
  8.  
  9.   //////////////////////////////////////////////////
  10.   println()
  11.   println()
  12.   println()
  13.   ////////////////////////////////////////////////////
  14.  
  15.   println("Find Max Method")
  16.   println("===========================================")
  17.   println(findMax(7, 10))
  18.   println(findMax(9399, 4383))
  19.   println(findMax(1, 100))
  20.  
  21.   //////////////////////////////////////////////////
  22.   println()
  23.   println()
  24.   println()
  25.   ////////////////////////////////////////////////////
  26.  
  27.   println("Summer Winter Method")
  28.   println("===========================================")
  29.   println(summer_winter(15))
  30.   println(summer_winter(33))
  31.   println(summer_winter(97))
  32.   println(summer_winter(100))
  33.  
  34.   //////////////////////////////////////////////////
  35.   println()
  36.   println()
  37.   println()
  38.   ////////////////////////////////////////////////////
  39.  
  40.   println("Check Speed Method")
  41.   println("===========================================")
  42.   checkSpeed(45)
  43.   checkSpeed(60)
  44.   checkSpeed(90)
  45.   checkSpeed(119)
  46.   checkSpeed(120)
  47.  
  48.   //////////////////////////////////////////////////
  49.   println()
  50.   println()
  51.   println()
  52.   ////////////////////////////////////////////////////
  53.  
  54.   println("Odd Even Method")
  55.   println("===========================================")
  56.   oddEven(10)
  57.  
  58.   //////////////////////////////////////////////////
  59.   println()
  60.   println()
  61.   println()
  62.   ////////////////////////////////////////////////////
  63.  
  64.   println("Sum of 3's and 5's Method")
  65.   println("===========================================")
  66.   println(sumOfThreesAndFives(20))
  67.   println(sumOfThreesAndFives(28))
  68.   println(sumOfThreesAndFives(96))
  69.  
  70.   //////////////////////////////////////////////////
  71.   println()
  72.   println()
  73.   println()
  74.   ////////////////////////////////////////////////////
  75.  
  76.   println("isPrime Method")
  77.   println("===========================================")
  78.   println(isPrime(7))
  79.   println(isPrime(49))
  80.   println(isPrime(97))
  81.  
  82.   //////////////////////////////////////////////////
  83.   println()
  84.   println()
  85.   println()
  86.   ////////////////////////////////////////////////////
  87.  
  88.   println("printPrimes Method")
  89.   println("===========================================")
  90.   printPrimes(100)
  91.  
  92.   //////////////////////////////////////////////////
  93.   println()
  94.   println()
  95.   println()
  96.   ////////////////////////////////////////////////////
  97.  
  98.   println("printPrimes Method")
  99.   println("===========================================")
  100.   sort(1,2,3)
  101.   sort(1,3,2)
  102.  
  103.   sort(2,1,3)
  104.   sort(2,3,1)
  105.  
  106.   sort(3,2,1)
  107.   sort(3,1,2)
  108.  
  109.   //////////////////////////////////////////////////
  110.   println()
  111.   println()
  112.   println()
  113.   ////////////////////////////////////////////////////
  114.  
  115.   println("isPerfectNumber Method")
  116.   println("===========================================")
  117.   println(isPerfectNumber(28))
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.   // #1
  125.   def ageTillSixtyFive(name: String, age: Int): Unit = {
  126.     val currentYear = 2019
  127.     println(s"Hello $name you will turn 65 in ${65 - age + currentYear}")
  128.   }
  129.  
  130.  
  131.   // #2
  132.   def findMax(numberOne: Int, numberTwo: Int): Int = {
  133.     if (numberOne > numberTwo) numberOne
  134.     else numberTwo
  135.   }
  136.  
  137.  
  138.   // #3
  139.   def summer_winter(number: Int): String = {
  140.     if (number % 3 == 0 && number % 5 == 0) "SummerWinter"
  141.     else if (number % 3 == 0) "Summer"
  142.     else if (number % 5 == 0) "Winter"
  143.     else number.toString
  144.   }
  145.  
  146.   // #4
  147.   def checkSpeed(speed: Int): Unit = {
  148.     var points: Int = 0
  149.     if (speed < 55) println("Ok")
  150.     else {
  151.       points = (speed - 55) / 5
  152.       println(s"Points: $points")
  153.     }
  154.     if (points > 12) println("License Suspended")
  155.   }
  156.  
  157.  
  158.   // #5
  159.   def oddEven(limit: Int): Unit = {
  160.     var postFix = ""
  161.     for(i <- 0 to limit) {
  162.       if (i % 2 == 0) postFix = "Even"
  163.       else postFix = "Odd"
  164.       println(s"$i is $postFix")
  165.     }
  166.   }
  167.  
  168.  
  169.   // #6
  170.   def sumOfThreesAndFives(limit: Int): Int = {
  171.     var sum = 0
  172.     for (i <- 0 to limit) {
  173.       if (i % 3 == 0 || i % 5 == 0) sum += i
  174.     }
  175.     sum
  176.   }
  177.  
  178.  
  179.   // #7
  180.   def isPrime(number: Int): Boolean = {
  181.     /* Logic = Start at 2 and proceed to half the number
  182.      Ex.) isPrime(40)
  183.      for loop ranges from 2 to 20
  184.      if 40 % 2 == 0 then 40 is not prime
  185.      if this failed the first time, say the number is now 30
  186.      we proceed to the next number to search for a divisor that is not 1 nor the number itself.
  187.      */
  188.     var initialBool = true
  189.     for (i <- 2 to (number / 2)) {
  190.       if (number % i == 0) initialBool = false
  191.     }
  192.     initialBool
  193.   }
  194.  
  195.  
  196.   // #8
  197.   def printPrimes(limit: Int): Unit = {
  198.     for (i <- 0 to limit) {
  199.       if (i > 1) {
  200.         if (isPrime(i)) println(i)
  201.       }
  202.     }
  203.   }
  204.  
  205.   // #9
  206.   def sort(x: Int, y: Int, z: Int): Unit = {
  207.     if (x > y && x > z && y > z) print(x,y,z)
  208.     if (x > y && x > z && z > y) print(x,z,y)
  209.  
  210.     if (y > x && y > z && x > z) print(y,x,z)
  211.     if (y > x && y > z && z > x) print(y,z,x)
  212.  
  213.     if (z > y && z > x && x > y) print(z,x,y)
  214.     if (z > y && z > x && y > x) print(z,y,x)
  215.     println()
  216.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement