《快学Scala》第五章习题解答

RT。

package com.scalalearn.scala.main
/**
  * 快学scala 05习题
  */

//1.改进Counter类,让它不要在大于Int.maxValue时变为负数
class Counter{
  private var value = 0
  def increment():Unit = {
    if(value < Int.MaxValue){
      value = value + 1
    }
  }

  def current() = value
}

//2.写一个BankAccount类,加入deposit和withdraw方法,和一个只读的balance属性
class BankAccount{
  private var balance:Double = 0

  def getBalance():Double = {
    balance
  }

  def deposit(num:Double):Unit = {
    balance = balance + num
  }

  def withdraw(num:Double):Unit = {
    if(num < balance){
      balance = balance - num
    }else{
      throw new Exception("withdraw money over total money")
    }
  }
}

//3.定义一个Time类,有两个属性hours和minutes,hour介于0和23之间
//另外还有一个检测时间是否早于另一个时间的方法,before(other:Time):Boolean
class Time3(var phours:Int,var pminutes:Int){
  private var hours:Int = 0
  private var minutes:Int = 0

  if(phours >=0 && phours <=23){
    hours = phours
  }else{
    throw new Exception("construct Time3 error")
  }
  if(minutes >= 0 && minutes <= 59){
    minutes = pminutes
  }else{
    throw new Exception("construct Time3 error")
  }

  def getHours() = hours
  def getMinutes() = minutes

  def before(other:Time3):Boolean={
    if(hours < other.getHours() || (hours == other.getHours() && minutes < other.getMinutes())){
      true
    }else{
      false
    }
  }
}

//4.重新实现Timer类,内部呈现改成字午夜起的分钟数
class Time4(phours:Int,pminutes:Int) {
  var minutes:Int = 0

  if(!(phours >= 0 && phours <=23)){
    throw new Exception("construct error")
  }
  if(!(pminutes >= 0 && pminutes <=59)){
    throw new Exception("construct error")
  }

  minutes = phours*60 + pminutes

  def getHours():Int={
    minutes/60
  }

  def getMinutes():Int={
    minutes%60
  }

  def before(other:Time4):Boolean = {
    if(minutes < other.getHours*60+other.getMinutes){
      true
    }else{
      false
    }
  }
}

//6.在5.2节的Persion类中加一个主构造器,将负年龄换为0
class Persion(page:Int){
  var age:Int = 0

  if(page < 0){
    age = 0
  }else{
    age = page
  }

  def getAge():Int = age
}

object LearnScala05 {
  def main(args:Array[String]):Unit={
    println("==================execise1====================")
    var counter = new Counter
    for(i <- 1 to Int.MaxValue) counter.increment()
    println(counter.current())

    println("==================execise2====================")
    var bank = new BankAccount
    bank.deposit(200)
    bank.withdraw(100)
    println(bank.getBalance())

    println("==================execise3====================")
    var time31 = new Time3(11,23)
    var time32 = new Time3(12,11)
    var time33 = new Time3(6,56)
    println(time31.getHours()+":"+time31.getMinutes())
    println(time31.before(time32))
    println(time31.before(time33))

    println("==================execise4====================")
    var time41 = new Time4(11,23)
    var time42 = new Time4(12,11)
    var time43 = new Time4(6,56)
    println(time41.getHours()+":"+time41.getMinutes())
    println(time41.before(time42))
    println(time41.before(time43))

    println("==================execise6====================")
    var person = new Persion(-1)
    println(person.getAge())

    //其余均是问答题,不再在代码中阐述
  }
}


你可能感兴趣的:(《快学Scala》第五章习题解答)