• 2020年寒假假期总结0112


      实验二Scala 语言基础

      实验一是对于linux系统的安装和常用命令,在之前的博客园随笔中已经有了记录,所以便不再赘述,直接开始第二章关于scala语言的学习

      对于scala语言,今天并没有完全学习完,只是完成了实验内容,在下面会总结一下,自己在完成实验过程中的总结的几个刚接触的易错点

      1. 计算级数

      请用脚本的方式编程计算并输出下列级数的前n项之和Sn,直到Sn刚好大于或等于q 为止,其中q为大于0的整数,其值通过键盘输入。sn=2/1+3/2+4/3+…n+1/n,例如,若 q 的值为 50.0,则输出应为:Sn=50.416695。请将源文件保存为exercise2-1.scala,在REPL模式下测试运行,测试样例:q=1时,Sn=2;q=30时,Sn=30.891459; q=50时,Sn=50.416695。  

    object demo {
      def main(args: Array[String]): Unit = {
        import  scala.io.StdIn
        println("请输入Q值:")
        val number = StdIn.readDouble()
        var sn=0.toDouble
        var i = 0.toDouble
        while (number > sn  ){
          i=i+1
          val temp = ((i + 1) / i).toDouble
          println(temp)
          sn=sn+((i+1)/i)
        }
        println(number)
        println(sn)
      }
    }

      注意点:对于sn和i值都需要进行toDouble方法转换,否则默认的是int类型,所以在刚运行的时候,每次都是会+1,输出的一直都是50,后来找到原因在这里。

      2. 模拟图形绘制  

      对于一个图形绘制程序,用下面的层次对各种实体进行抽象。定义一个Drawable的特质,其包括一个draw方法,默认实现为输出对象的字符串表示。定义一个Point类表示点,其混入了Drawable特质,并包含一个shift方法,用于移动点。所有图形实体的抽象类为 Shape,其构造函数包括一个Point类型,表示图形的具体位置(具体意义对不同的具体图形不一样)。Shape类有一个具体方法moveTo和一个抽象方法zoom,其中moveTo将图形从当前位置移动到新的位置, 各种具体图形的moveTo可能会有不一样的地方。zoom方法实现对图形的放缩,接受一个浮点型的放缩倍数参数,不同具体图形放缩实现不一样。继承Shape类的具体图形类型包括直线类Line和圆类Circle。Line类的第一个参数表示其位置,第二个参数表示另一个端点,Line放缩的时候,其中点位置不变,长度按倍数放缩(注意,缩放时,其两个端点信息也改变了),另外,Line的move行为影响了另一个端点,需要对 move方法进行重载。Circle类第一个参数表示其圆心,也是其位置,另一个参数表示其半径,Circle缩放的时候,位置参数不变,半径按倍数缩放。另外直线类Line和圆类Circle 都混入了Drawable特质,要求对draw进行重载实现,其中类Line的draw输出的信息样式为“Line:第一个端点的坐标--第二个端点的坐标)”,类Circle的draw输出的信息样式为“Circle center:圆心坐标,R=半径”。如下的代码已经给出了Drawable和Point的定义,同时也给出了程序入口main函数的实现,请完成Shape类、Line类和Circle类的定义。   

    case class Point(var x: Double, var y: Double) extends Drawable {
      var Px: Double = x
      var Py: Double = y
    
      def shift(deltaX: Double, deltaY: Double) {
        Px += deltaX
        Py += deltaY
      }
    
      override def draw(): Unit = {
        println("Point:(" + Px + "," + Py + ")")
      }
    }
    
    trait Drawable {
      def draw() {
        println(this.toString)
      }
    }
    
    abstract class Shape extends Drawable {
      def moveTo(point: Point)
    
      def zoom(step: Double)
    
      def draw()
    }
    
    class Line(point1: Point, point2: Point) extends Shape {
      override def moveTo(point: Point): Unit = {
        point1.shift(point.Px, point.Py)
        point2.shift(point.Px, point.Py)
      }
    
      override def zoom(step: Double): Unit = {
        var tempx = (point2.Px + point1.Px) / 2 - point1.Px
        if (tempx < 0) {
          tempx = 0 - tempx
        }
        var centerx = ((point2.Px + point1.Px) / 2)
        var zoomlengthx = (tempx * step)
        point1.Px = centerx - zoomlengthx
        point2.Px = centerx + zoomlengthx
    
        var tempy = (point2.Py + point1.Py) / 2 - point1.Py
        if (tempy < 0) {
          tempy = 0 - tempy
        }
        var centery = ((point2.Py + point1.Py) / 2)
        var zoomlengthy = (tempy * step)
        point1.Py = centery - zoomlengthy
        point2.Py = centery + zoomlengthy
      }
    
      override def draw(): Unit = {
        println("Line:(" + point1.Px + "," + point1.Py + ")--(" + point2.Px + "," + point2.Py + ")")
      }
    }
    
    class Circle(point: Point, r: Double) extends Shape {
      var R=r
      override def moveTo(point: Point): Unit = {
        this.point.Px=point.Px
        this.point.Py=point.Py
      }
    
      override def zoom(step: Double): Unit = {
        R=R*step
      }
    
      override def draw(): Unit = {
        println("Circle center:(" + point.Px + "," + point.Py + "),R=" + R)
      }
    }
    
    
    object demo2 {
      def main(args: Array[String]) {
        val p = new Point(10, 30)
        p.draw;
        val line1 = new Line(Point(0, 0), Point(20, 20))
        line1.draw
        line1.moveTo(Point(5, 5)) //移动到一个新的点
        line1.draw
        line1.zoom(2) //放大两倍
        line1.draw
        val cir = new Circle(Point(10, 10), 5)
        cir.draw
        cir.moveTo(Point(30,20))
        cir.draw
        cir.zoom(0.5)
        cir.draw
      }
    
    }

      

       注意点:第二个实验内容考察的是对于scala中继承的一些注意点,就是在构造方法上有一些区别,同时需要注意的是var是代表该变量是可以被重新赋值的,而val则是不可以被重新赋值的,这个需要注意,但是val的好处就是易于被回收。在scala中,类默认的变量类型是可以被其他类访问的类型(可以认为是public类型),如果加上private就是无法访问,但是scala又特殊在有一个伴生类的东西,即单例对象与类同名时,这个单例对象被称为这个类的伴生对象,而这个类被称为这个单例对象的伴生类。伴生类和伴生对象要在同一个源文件中定义,伴生对象和伴生类可以互相访问其私有成员。还有就是在构造函数的时候,出了构造函数意外的方法是不会被调用的,其他的语句都是会被执行的,这个与其他语言不同。

      3.统计学生信息

      学生的成绩清单格式如下所示,第一行为表头,各字段意思分别为学号、性别、课程名

    课程名2等,后面每一行代表一个学生的信息,各字段之间用空白符隔开

    Id  gender  Math    English   Physics    

    301610    male   80      64        78 

    301611  female   65      87        58 

    ...

      给定任何一个如上格式的清单(不同清单里课程数量可能不一样),要求尽可能采用函数式编程,统计出各门课程的平均成绩,最低成绩,和最高成绩;另外还需按男女同学分开,分别统计各门课程的平均成绩,最低成绩,和最高成绩。

    import scala.collection.mutable.ArrayBuffer
    
    class Student(id: Int, gender: String, math: Int, english: Int, physics: Int) {
      var SiD = id
      var Sgender = gender
      var Smath = math
      var Senglish = english
      var Sphysics = physics
    }
    
    object demo3 {
      def main(args: Array[String]): Unit = {
        var studentlist = new ArrayBuffer[Student]()
        studentlist.append(new Student(301610, "male", 80, 64, 78))
        studentlist.append(new Student(301611, "female", 65, 87, 58))
        studentlist.append(new Student(301612, "female", 44, 71, 77))
        studentlist.append(new Student(301613, "female", 66, 71, 91))
        studentlist.append(new Student(301614, "female", 70, 71, 100))
        studentlist.append(new Student(301615, "male", 72, 77, 72))
        studentlist.append(new Student(301616, "female", 73, 81, 75))
        studentlist.append(new Student(301617, "female", 69, 77, 75))
        studentlist.append(new Student(301618, "male", 73, 61, 65))
        studentlist.append(new Student(301619, "male", 74, 69, 68))
        studentlist.append(new Student(301620, "male", 76, 62, 76))
        studentlist.append(new Student(301621, "male", 73, 69, 91))
        studentlist.append(new Student(301622, "male", 55, 69, 61))
        studentlist.append(new Student(301623, "male", 50, 58, 75))
        studentlist.append(new Student(301624, "female", 63, 83, 93))
        studentlist.append(new Student(301625, "male", 72, 54, 100))
        studentlist.append(new Student(301626, "male", 76, 66, 73))
        studentlist.append(new Student(301627, "male", 82, 87, 79))
        studentlist.append(new Student(301628, "female", 62, 80, 54))
        studentlist.append(new Student(301629, "male", 89, 77, 72))
    
        sortsum(studentlist)
      }
      def sortsum(list: ArrayBuffer[Student]): Unit = {
        var count=0
        var mathSum = 0
        var mathAverage = 0
        var mathMin = 100
        var mathMax = 0
        var englishSum = 0
        var englishAverage = 0
        var englishMin = 100
        var englishMax = 0
        var physicsSum = 0
        var physicsAverage = 0
        var physicsMin = 100
        var physicsMax = 0
        var femaleSrudent=new ArrayBuffer[Student]()
        var maleSrudent=new ArrayBuffer[Student]()
        list.foreach(student => {
          count+=1
          if(student.Sgender.equals("female")){
            femaleSrudent.append(student)
          }else{
            maleSrudent.append(student)
          }
          if(student.Smath>mathMax){
            mathMax=student.Smath
          }else if(student.Smath<mathMin){
            mathMin=student.Smath
          }
          if(student.Senglish>englishMax){
            englishMax=student.Senglish
          }else if(student.Senglish<englishMin){
            englishMin=student.Senglish
          }
          if(student.Sphysics>physicsMax){
            physicsMax=student.Sphysics
          }else if(student.Sphysics<physicsMin){
            physicsMin=student.Sphysics
          }
          mathSum+=student.Smath
          englishSum+=student.Senglish
          physicsSum+=student.Sphysics
        })
        mathAverage=mathSum/count
        englishAverage=englishSum/count
        physicsAverage=physicsSum/count
        println("course		average	min	max")
        println("Math:		"+mathAverage+"		"+mathMin+"	"+mathMax)
        println("English:	"+englishAverage+"		"+englishMin+"	"+englishMax)
        println("Physics:	"+physicsAverage+"		"+physicsMin+"	"+physicsMax)
        sortmale(maleSrudent)
        sortfemale(femaleSrudent)
      }
      def sortmale(list: ArrayBuffer[Student]): Unit = {
        var count=0
        var mathSum = 0
        var mathAverage = 0
        var mathMin = 100
        var mathMax = 0
        var englishSum = 0
        var englishAverage = 0
        var englishMin = 100
        var englishMax = 0
        var physicsSum = 0
        var physicsAverage = 0
        var physicsMin = 100
        var physicsMax = 0
        list.foreach(student => {
          count+=1
          if(student.Smath>mathMax){
            mathMax=student.Smath
          }else if(student.Smath<mathMin){
            mathMin=student.Smath
          }
          if(student.Senglish>englishMax){
            englishMax=student.Senglish
          }else if(student.Senglish<englishMin){
            englishMin=student.Senglish
          }
          if(student.Sphysics>physicsMax){
            physicsMax=student.Sphysics
          }else if(student.Sphysics<physicsMin){
            physicsMin=student.Sphysics
          }
          mathSum+=student.Smath
          englishSum+=student.Senglish
          physicsSum+=student.Sphysics
        })
        mathAverage=mathSum/count
        englishAverage=englishSum/count
        physicsAverage=physicsSum/count
        println("course		average	min	max(males)")
        println("Math:		"+mathAverage+"		"+mathMin+"	"+mathMax)
        println("English:	"+englishAverage+"		"+englishMin+"	"+englishMax)
        println("Physics:	"+physicsAverage+"		"+physicsMin+"	"+physicsMax)
      }
      def sortfemale(list:ArrayBuffer[Student]): Unit = {
        var count=0
        var mathSum = 0
        var mathAverage = 0
        var mathMin = 100
        var mathMax = 0
        var englishSum = 0
        var englishAverage = 0
        var englishMin = 100
        var englishMax = 0
        var physicsSum = 0
        var physicsAverage = 0
        var physicsMin = 100
        var physicsMax = 0
        list.foreach(student => {
          count+=1
          if(student.Smath>mathMax){
            mathMax=student.Smath
          }else if(student.Smath<mathMin){
            mathMin=student.Smath
          }
          if(student.Senglish>englishMax){
            englishMax=student.Senglish
          }else if(student.Senglish<englishMin){
            englishMin=student.Senglish
          }
          if(student.Sphysics>physicsMax){
            physicsMax=student.Sphysics
          }else if(student.Sphysics<physicsMin){
            physicsMin=student.Sphysics
          }
          mathSum+=student.Smath
          englishSum+=student.Senglish
          physicsSum+=student.Sphysics
        })
        mathAverage=mathSum/count
        englishAverage=englishSum/count
        physicsAverage=physicsSum/count
        println("course		average	min	max(females)")
        println("Math:		"+mathAverage+"		"+mathMin+"	"+mathMax)
        println("English:	"+englishAverage+"		"+englishMin+"	"+englishMax)
        println("Physics:	"+physicsAverage+"		"+physicsMin+"	"+physicsMax)
      }
    }

      

       注意点:这里面需要注意的是Array类型的使用,其他方面的话只有一点,那就是构造函数的重载,

    class Student(id: Int, gender: String, math: Int, english: Int, physics: Int) {
      var SiD = id
      var Sgender = gender
      var Smath = math
      var Senglish = english
      var Sphysics = physics
      def this(id: Int, gender: String, math: Int, english: Int, physics: Int,science:Int ){
        this(id: Int, gender: String, math: Int, english: Int, physics: Int)
        var Sscience=science
      }
    }

      可以看出如果对这个类构造函数重载的时候,需要写def this方法,并在第一句中加上第一个构造方法。

  • 相关阅读:
    第三周作业
    第二周作业
    第一次作业(2)
    第一次作业
    百度翻译新API C#版在 winform,Asp.Net的小程序
    ajax 里的数据请求
    结合css与javascript来实现手机移动端的屏幕滑动效果
    js公农历互转(1900~2100年)
    webpack命令
    vscode快速输出console.log
  • 原文地址:https://www.cnblogs.com/heiyang/p/12185141.html
Copyright © 2020-2023  润新知