实验二 Scala编程初级实践

(一) 安装Scala

把下载的scala-2.11.8.tgz文件解压缩到“/usr/local/”目录下

修改文件夹名称,并为hadoop用户赋予权限

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5N2cR6QX-1618666045470)(https://img2020.cnblogs.com/blog/1971052/202104/1971052-20210417212002685-1829821134.png)]

接着需要把scala命令添加到path环境变量中,这里我们在 ~/.bashrc 中进行设置,可以采用vim编辑器打开.bashrc文件,在.bashrc文件的最开头位置,修改path环境变量设置,把scala命令所在的目录“/usr/local/scala/bin”增加到path中


接着还需要让该环境变量生效,设置好后检验一下是否设置正确

(二) 计算级数

第一种方法:用:paste多行输入


第二种方法:
创建文件夹以及所需的文件夹结构,使用上次实验的sbt对scala应用程序进行编译打包,通过spark-submit运行程序,具体如下

(三) 模拟图形绘制

代码如下:

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)
	}
}

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(s"Line:(${location.x},${location.y})--(${endPoint.x},${endPoint.y})")
	} 
	//按指定格式重载click
	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"Circlecenter:(${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
	}
}

  • 6
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值