Last active
August 15, 2017 04:26
-
-
Save yangwansu/fb9c51c4d2b8cf700615aa466bbbb0e9 to your computer and use it in GitHub Desktop.
쉽게 배워 빨리 써먹는 Scala 연습문제를 풀어보았다.
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
/** Charter 03 : 배열사용하기 **/ | |
/** 2. 정수배열의 인접한 원소를 교환하는 루프를 작성하라. 예를 들어 Array(1,2,3,4,5) 는 Array (2,1,4,3,5) 가 된다. **/ | |
val array1 = Array(1,2,3,4,5) | |
for(i <- 0 until (array1.length-1 , 2); temp = array1(i)) {array1(i) = array1(i+1); array1(i+1)= temp } | |
/** 3. 2번 문제를 반복하되 교환한 값으로 새 배열을 생성하라. for/yield 를 사용한다. **/ | |
(for(i <- 0 until (array1.length); | |
v = if(i == array1.length -1) array1(i) | |
else if (i % 2 == 0) array1(i+1) | |
else array1(i-1) ) | |
yield v).toArray | |
/** 4. 정수 배열이 주어졌을 때, 기존 배열의 모든 양수 값이 기존 순서대로 오고 그 뒤에 모든 0 혹은 음수 값이 기존 순서대로 오는 새 배열을 생성하라. **/ | |
val array = Array(-1, 3,5,7,-3,-5,0,7) | |
array.filter(_>0) ++ array.filter(_<=0) | |
/** Charter 05 : 클래스 **/ | |
/* 1. 5.1 간단한 클래스와 인자 없는 메소드에 나온 Counter 클래스를 Int.MaxValue에서 음수가 되지 않게 개선하라.*/ | |
class Counter { | |
private var value = 0 | |
def increment { value += (if(value==Int.MaxValue) 0 else 1) } | |
def current = value | |
} | |
/* 2. 메소드 deposit 과 withdraw, 일기 전용 프로퍼티 balance 로 BankAccount 클래스를 작성하라.*/ | |
class BankAccount { | |
private var balance = 0 | |
def deposit(amount:Int) { balance += (if(amount > 0) amount else 0 ) } | |
def withdraw(amount:Int) { balance -= (if(amount <= balance) amount else 0 ) } | |
def current = balance; | |
} | |
/* 3. 읽기 전용프로퍼티 hours와 minutes, 이 시간이 다른 시간보다 앞선 시간인지 확인하는 before(other: Time): Boolean 메서드를 가진 Time 클래스를 작성하라 | |
Time Object 는 new Time(hrs, min) 으로 생성할 수 있어야 하는데, 여기서 hrrs는 군용 시간 형식 (0과 23사이 ) 이다.*/ | |
class Time(hrs:Int ,min:Int){ | |
val hours = if(0 <= hrs && hrs < 24 ) hrs else throw new IllegalArgumentException | |
val minutes = if(0 <= min && min < 59 ) min else throw new IllegalArgumentException | |
def before(other: Time): Boolean = { | |
(hours * 60 + minutes) < (other.hours * 60 + other.minutes) | |
} | |
} | |
new Time(12,0).before( new Time(11,58)) | |
/* 4. 내부표현이 자정이후ㅜ.... */ | |
class Time(hrs:Int ,min:Int){ | |
if(0 > hrs || hrs >= 24 )throw new IllegalArgumentException | |
if(0 > min || min >= 60 )throw new IllegalArgumentException | |
private val time = hrs * 60 + min | |
def before(other: Time): Boolean = { | |
(time) < (other.time) | |
} | |
} | |
new Time(12,0).before( new Time(11,58)) | |
/* 5. */ | |
import scala.beans.BeanProperty | |
class Student { | |
@BeanProperty var name:String = _ | |
@BeanProperty var id:Long = _ | |
} | |
/* Chapter 6. 오브젝트 */ | |
object Conversions { | |
def inchesToCentimeters(inchi:Double):Double = { | |
//blah blah | |
inchi | |
} | |
def gallonsToLiters(gallons:Double):Double = { | |
//blah blah | |
gallons; | |
} | |
def milesToKilometers(miles:Double):Double = { | |
miles; | |
} | |
} | |
/* 2. 이전 문제는 그다지 객체지향적이지 않다. | |
일반적인 슈퍼클래스인 UnitConversion 을 제공하고 이를 확장하는 IncheToCentimeters, GallonsToLiters, MilesToKilometers 오브젝트을 정의하라 */ | |
abstract class UnitConversion { | |
def convert(value:Double) :Double | |
} | |
object IncheToCentimeters extends UnitConversion { | |
override def convert(value:Double) : Double = { value} | |
} | |
//...... | |
/* 4. */ | |
class Point (val x:Double,val y:Double){ | |
} | |
object Point { | |
def apply(x:Double, y:Double) = new Point(x,y) | |
} | |
/*5. */ | |
object Reverse extends App { | |
for(item <- args.reverse) {println(item)} | |
} | |
/*6. */ | |
object Card extends Enumeration { | |
val Clover = Value(0, "♣︎") | |
val Diamond = Value("♦︎") | |
val Heart = Value("♥︎") | |
val Space = Value("♠︎") | |
} | |
for(card <- Cards.values) {println(card)} | |
/*7. */ | |
object Card extends Enumeration { | |
val Clover = Value(0, "♣︎") | |
val Diamond = Value("♦︎") | |
val Heart = Value("♥︎") | |
val Space = Value("♠︎") | |
type Card = Card.Value | |
} | |
import Card._ | |
def isRed(card:Card) = card == Diamond || card == Heart | |
/* | |
8. RGB 큐브의 8개의 모서리를 나타내는 Enumeration을 작성하라. ID로 색상 값을 사용하라. (예: 0xff0000은 빨강) | |
*/ | |
object RGBCube extends Enumeration { | |
type RGBCube = RGBCube.Value | |
} | |
/* Chapter 7. */ | |
/* 1. */ | |
/** EX7.1.1.scala | |
package com | |
package hosrtmann | |
object Message { | |
def apply(str: String): Message = new Message(str) | |
} | |
class Message(str: String) { | |
override def toString: String = str; | |
} | |
package impatient { | |
object Greet { | |
def greeting(msg:Message) { println(msg)} | |
} | |
} | |
**/ | |
/** EX7.1.2.scala | |
package com.hosrtmann.impatient | |
import com.hosrtmann.Message | |
object Hello { | |
def greeting(name: String) { | |
Greet.greeting(Message("Hello " + name)) | |
} | |
} | |
**/ | |
/* 2. */ | |
/* EX7.2.1.scala | |
package com.horstmann | |
object Values { | |
} | |
object EX7_2_1 { | |
println(com.horstmann.Values) | |
} | |
*/ | |
/* EX7.2.2.scala | |
package com.horstmann | |
package com.hosrstmann { | |
object Value { | |
} | |
} | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment