zoukankan      html  css  js  c++  java
  • 学习笔记——spark基础实验二

      今天学习了scala语言的基础知识,并完成了spark基础实验二的部分内容。

      题目1:

    import io.StdIn._
    
    object jishu{
        def main(args:Array[String]){
        var Sn:Float = 0
        var n:Float = 1
        print("请输入q:")
        val q = readInt();
        while(Sn<q){
            Sn += (n+1)/n
            n += 1
        }
        println("Sn="+Sn)
        }
    }   

     

    题目2:

    case class Point(var x:Double,var y:Double) extends Drawable{
            def shift(deltaX:Double,deltaY:Double){x+=deltaX;y+=deltaY}
        }
        trait Drawable{
            def draw(){println(this.toString)}
        }
        // 请完成 Shape 类、Line 类和 Circle 类的定义。
        abstract class Shape(var location:Point){//location是Shape的一个可变字段
        def moveTo(newLocation:Point){ //默认实现,只是修改位置
            location = newLocation
            }
         def zoom(scale:Double)
        }
        class Line(beginPoint:Point,var endPoint:Point) extends Shape(beginPoint) with Drawable{
        override def draw(){
            println("line:("+location.x+","+location.y+")--"+"("+endPoint.x+","+endPoint.y+")")
        }
        override def moveTo(newLocation:Point){
                endPoint.shift(newLocation.x - location.x,newLocation.y - location.y) //直线移动时,先移动另外一个端点
            location = newLocation //移动位置
            }
            override def zoom(scale:Double){
            val midPoint = Point((endPoint.x + location.x)/2,(endPoint.y + location.y)/2) //求出中点,并按中点进行缩放
            location.x = midPoint.x + scale * (location.x - midPoint.x)
            location.y = midPoint.y + scale * (location.y -midPoint.y)
            endPoint.x = midPoint.x + scale * (endPoint.x - midPoint.x)
            endPoint.y = midPoint.y + scale * (endPoint.y -midPoint.y)
            }
        }
        class Circle(center:Point,var radius:Double) extends Shape(center) with Drawable{
        override def draw(){//按指定格式重载click
            println(s"Circle center:(${location.x},${location.y}),R=$radius")
        }
            override def zoom(scale:Double){
        radius = radius*scale //对圆的缩放只用修改半径
            }
        }
        
    
        object MyDraw{
         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
            }
        }
    模拟图形绘制

  • 相关阅读:
    react-flux的使用(2018/12/16)
    react-redux 的使用*(2018/12/17)
    小程序推送消息(Template)
    小程序富文本照片左右滚动
    前端自动化工具
    拾色器前端工具
    微信小程序 摇一摇
    小程序在线阅读文档
    配置JDK环境变量
    小程序 获取前几名加样式
  • 原文地址:https://www.cnblogs.com/zwang/p/12302645.html
Copyright © 2011-2022 走看看