今天完成了实验一和实验二,主要内容为实验二,对于scala的语法的实践编程应用
1. 计算级数
请用脚本的方式编程计算并输出下列级数的前 n 项之和 Sn,直到 Sn 刚好大于或等于 q
为止,其中 q 为大于 0 的整数,其值通过键盘输入。
例 如 , 若 q 的 值 为 50.0 , 则 输 出 应 为 : Sn=50.416695 。 请 将 源 文 件 保 存 为
exercise2-1.scala,在REPL模式下测试运行,测试样例:q=1时,Sn=2;q=30时,Sn=30.891459;
q=50 时,Sn=50.416695。
源代码:
package Test2 /* * 计算级数 */ import io.StdIn._ object Demo1{ def main(args:Array[String]){ var n:Float = 1 var Sn:Float = 0 println("请输入一个整数:") var q = readInt() while(Sn < q) { Sn += (n+1)/n n += 1 } println(s"Sn = $Sn") } }
实验截图:
2. 模拟图形绘制
对于一个图形绘制程序,用下面的层次对各种实体进行抽象。定义一个 Drawable 的特
质,其包括一个 draw 方法,默认实现为输出对象的字符串表示。定义一个 Point 类表示点,
其混入了 Drawable 特质,并包含一个 shift 方法,用于移动点。所有图形实体的抽象类为
主讲教师:林子雨 http://www.cs.xmu.edu.cn/linziyu 第 1 页厦门大学林子雨,赖永炫,陶继平 编著《Spark 编程基础(Scala 版)》 教材配套机房上机实验指南
实验 2 Scala 编程初级实践
主讲教师:林子雨 http://www.cs.xmu.edu.cn/linziyu 第 2 页
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{
def shift(deltaX:Double,deltaY:Double){x+=deltaX;y+=deltaY}
}
trait Drawable{
def draw(){println(this.toString)}
}
// 请完成 Shape 类、Line 类和 Circle 类的定义。
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
}
}
源代码:
package Test2 case class Point(var x:Double ,var y:Double) extends Drawable{ def shift(X:Double ,Y:Double){ x += X y += Y } } trait Drawable{ def draw(){println(this.toString)} } abstract class Shape(var location:Point) { 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})") } 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() { println(s"Circle center:(${location.x},${location.y}),R=$radius") } override def zoom(scale :Double) { radius = radius*scale } } 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 circle = new Circle(Point(10,10),5) circle.draw circle.moveTo(Point(30,20)) circle.draw circle.zoom(0.5) circle.draw } }
实验截图:
3. 统计学生成绩
学生的成绩清单格式如下所示,第一行为表头,各字段意思分别为学号、性别、课程名
1、课程名 2 等,后面每一行代表一个学生的信息,各字段之间用空白符隔开
Id
gender Math English Physics
301610 male 80 64 78
301611 female 65 87 58
...
给定任何一个如上格式的清单(不同清单里课程数量可能不一样),要求尽可能采用函
数式编程,统计出各门课程的平均成绩,最低成绩,和最高成绩;另外还需按男女同学分开,
分别统计各门课程的平均成绩,最低成绩,和最高成绩。
测试样例 1 如下:
Id
gender Math English Physics
301610 male 80 64 78
301611 female 65 87 58
301612 female 44 71 77
301613 female 66 71 91
301614 female 70 71 100
301615 male 72 77 72
301616 female 73 81 75
301617 female 69 77 75
301618 male 73 61 65
301619 male 74 69 68
301620 male 76 62 76
301621 male 73 69 91
301622 male 55 69 61
301623 male 50 58 75
301624 female 63 83 93
301625 male 72 54 100
301626 male 76 66 73
301627 male 82 87 79
301628 female 62 80 54
301629 male 89 77 72
源代码:
import scala.collection.mutable.ArrayBuffer
object Demo3 {
def main(args: Array[String]): Unit = {
var students:ArrayBuffer[Student] = new ArrayBuffer[Student]()
students.append(new Student(301610, "male", 80, 64, 78))
students.append(new Student(301611, "female", 65, 87, 58))
students.append(new Student(301612, "female", 44, 71, 77))
students.append(new Student(301613, "female", 66, 71, 91))
students.append(new Student(301614, "female", 70, 71, 100))
students.append(new Student(301615, "male", 72, 77, 72))
students.append(new Student(301616, "female", 73, 81, 75))
students.append(new Student(301617, "female", 69, 77, 75))
students.append(new Student(301618, "male", 73, 61, 65))
students.append(new Student(301619, "male", 74, 69, 68))
students.append(new Student(301620, "male", 76, 62, 76))
students.append(new Student(301621, "male", 73, 69, 91))
students.append(new Student(301622, "male", 55, 69, 61))
students.append(new Student(301623, "male", 50, 58, 75))
students.append(new Student(301624, "female", 63, 83, 93))
students.append(new Student(301625, "male", 72, 54, 100))
students.append(new Student(301626, "male", 76, 66, 73))
students.append(new Student(301627, "male", 82, 87, 79))
students.append(new Student(301628, "female", 62, 80, 54))
students.append(new Student(301629, "male", 89, 77, 72))
getCourse(students)
getCourseByMale(students)
getCourseByFeMale(students)
}
def getCourse(value: ArrayBuffer[Student]): Unit ={
var Mathaverage:Double=0;
var MathSum:Double = 0;
var MathMin:Double = value(0).Math;
var MathMax:Double = value(0).Math;;
var Englishaverage:Double=0;
var EnglishSum:Double = 0;
var EnglishMin:Double = value(0).English;
var EnglishMax:Double = value(0).Math;;
var Physicsaverage:Double=0;
var PhysicsSum:Double = 0;
var PhysicsMin:Double = value(0).Physics;
var PhysicsMax:Double = value(0).Math;;
var number = value.length;
for (i <- 0 until value.length){
MathSum += value(i).Math
EnglishSum += value(i).English
PhysicsSum += value(i).Physics
//求各科最高成绩
if(value(i).Math > MathMax){
MathMax=value(i).Math;
}
if(value(i).English > EnglishMax){
EnglishMax=value(i).English;
}
if(value(i).Physics > PhysicsMax){
PhysicsMax=value(i).Physics;
}
//求各科最低成绩
if(value(i).Math < MathMin){
MathMin=value(i).Math;
}
if(value(i).English < EnglishMin){
EnglishMin=value(i).English;
}
if(value(i).Physics < PhysicsMin){
PhysicsMin=value(i).Physics;
}
}
//求平均值
Mathaverage = MathSum/number
Englishaverage = EnglishSum/number
Physicsaverage = PhysicsSum/number
println("course average min max ")
println("Math: "+Mathaverage.formatted("%.2f")+" "+MathMin.formatted("%.2f")+" "+MathMax.formatted("%.2f"))
println("English: "+Englishaverage.formatted("%.2f")+" "+EnglishMin.formatted("%.2f")+" "+EnglishMax.formatted("%.2f"))
println("Physics: "+Physicsaverage.formatted("%.2f")+" "+PhysicsMin.formatted("%.2f")+" "+PhysicsMax.formatted("%.2f"))
}
def getCourseByMale(value: ArrayBuffer[Student]): Unit = {//求女生
var Mathaverage: Double = 0;
var MathSum: Double = 0;
var MathMin: Double = value(0).Math;
var MathMax: Double = value(0).Math;
var Englishaverage: Double = 0;
var EnglishSum: Double = 0;
var EnglishMin: Double = value(0).English;
var EnglishMax: Double = value(0).English;
var Physicsaverage: Double = 0;
var PhysicsSum: Double = 0;
var PhysicsMin: Double = value(0).Physics;
var PhysicsMax: Double = value(0).Physics;
var number = 0;
for (i <- 0 until value.length) {
if (value(i).gender.equals("male")) {
number += 1
//求总分数
MathSum += value(i).Math
EnglishSum += value(i).English
PhysicsSum += value(i).Physics
//求各科最高分数
if (value(i).Math > MathMax) {
MathMax = value(i).Math;
}
if (value(i).English > EnglishMax) {
EnglishMax = value(i).English;
}
if (value(i).Physics > PhysicsMax) {
PhysicsMax = value(i).Physics;
}
//求各科最低分数
if (value(i).Math < MathMin) {
MathMin = value(i).Math;
}
if (value(i).English < EnglishMin) {
EnglishMin = value(i).English;
}
if (value(i).Physics < PhysicsMin) {
PhysicsMin = value(i).Physics;
}
}
}
//平均值
Mathaverage = MathSum/number
Englishaverage = EnglishSum/number
Physicsaverage = PhysicsSum/number
println("course average min max (male)")
println("Math: "+Mathaverage.formatted("%.2f")+" "+MathMin.formatted("%.2f")+" "+MathMax.formatted("%.2f"))
println("English: "+Englishaverage.formatted("%.2f")+" "+EnglishMin.formatted("%.2f")+" "+EnglishMax.formatted("%.2f"))
println("Physics: "+Physicsaverage.formatted("%.2f")+" "+PhysicsMin.formatted("%.2f")+" "+PhysicsMax.formatted("%.2f"))
}
def getCourseByFeMale(value: ArrayBuffer[Student]): Unit ={
var Mathaverage:Double=0;
var MathSum:Double = 0;
var MathMin:Double = value(1).Math;
var MathMax:Double = value(1).Math;
var Englishaverage:Double=0;
var EnglishSum:Double = 0;
var EnglishMin:Double = value(1).English;
var EnglishMax:Double = value(1).English;
var Physicsaverage:Double=0;
var PhysicsSum:Double = 0;
var PhysicsMin:Double = value(1).Physics;
var PhysicsMax:Double = value(1).Physics;
var number = 0;
for (i <- 0 until value.length){
if(value(i).gender.equals("female")){
number=number+1
MathSum += value(i).Math
EnglishSum += value(i).English
PhysicsSum += value(i).Physics
if(value(i).Math>MathMax){
MathMax=value(i).Math;
}
if(value(i).English>EnglishMax){
EnglishMax=value(i).English;
}
if(value(i).Physics>PhysicsMax){
PhysicsMax=value(i).Physics;
}
if(value(i).Math<MathMin){
MathMin=value(i).Math;
}
if(value(i).English<EnglishMin){
EnglishMin=value(i).English;
}
if(value(i).Physics<PhysicsMin){
PhysicsMin=value(i).Physics;
}
}
}
//求平均成绩
Mathaverage = MathSum/number
Englishaverage = EnglishSum/number
Physicsaverage = PhysicsSum/number
println("course average min max (female)")
println("Math: "+Mathaverage.formatted("%.2f")+" "+MathMin.formatted("%.2f")+" "+MathMax.formatted("%.2f"))
println("English: "+Englishaverage.formatted("%.2f")+" "+EnglishMin.formatted("%.2f")+" "+EnglishMax.formatted("%.2f"))
println("Physics: "+Physicsaverage.formatted("%.2f")+" "+PhysicsMin.formatted("%.2f")+" "+PhysicsMax.formatted("%.2f"))
}
}
class Student(i:Int,g:String,m:Double,e:Double,p:Double){
var id:Int = i
var gender:String = g
var Math:Double = m
var English:Double = e
var Physics:Double = p
实验截图: