码迷,mamicode.com
首页 > 其他好文 > 详细

学习进度12

时间:2020-02-12 22:04:19      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:不同的   blog   end   table   要求   字符串表   构造函数   sharp   array   

实验2-Scala编程初级实践-题目-厦门大学-林子雨-Spark编程基础(Scala版)

模拟图形绘制
对于一个图形绘制程序,用下面的层次对各种实体进行抽象。定义一个 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 类的定义。
技术图片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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)//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})")
    }
    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 test3
{
    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
    }
}

  编译运行结果:

技术图片

 

 

统计学生成绩

学生的成绩清单格式如下所示,第一行为表头,各字段意思分别为学号、性别、课程名 1、课程名 2 等,后面每一行代表一个学生的信息,各字段之间用空白符隔开

技术图片

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

技术图片

 技术图片

技术图片

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import scala.collection.mutable.ArrayBuffer
   
object test4 {
  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))
    println(students.length)
    println(students(1).toString)
   
    getData(students)
    getDataMale(students)
    getDataFeMale(students)
  }
  def getData(value: ArrayBuffer[Student]): Unit ={
    var Mathaverage:Double=0;
    var MathSum:Double = 0;
    var MathMin:Double = value(0).Math;
    var MathMax:Double = 0;
   
    var Englishaverage:Double=0;
    var EnglishSum:Double = 0;
    var EnglishMin:Double = value(0).English;
    var EnglishMax:Double = 0;
   
    var Physicsaverage:Double=0;
    var PhysicsSum:Double = 0;
    var PhysicsMin:Double = value(0).Physics;
    var PhysicsMax:Double = 0;
   
    var number = value.length;
    for (i <- 0 until value.length){
      //三科总数
      MathSum = value(i).Math+MathSum
      EnglishSum = value(i).English+EnglishSum
      PhysicsSum = value(i).Physics+PhysicsSum
      //最大和最小
      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\t average\t min\t max \t")
    println("Math:\t   "+Mathaverage+"\t\t"+MathMin+"\t "+MathMax)
    println("English:\t"+Englishaverage+"\t"+EnglishMin+"\t "+EnglishMax)
    println("Physics:\t"+Physicsaverage+"\t"+PhysicsMin+"\t "+PhysicsMax)
  }
   
  def getDataMale(value: ArrayBuffer[Student]): Unit = {
    var Mathaverage: Double = 0;
    var MathSum: Double = 0;
    var MathMin: Double = value(0).Math;
    var MathMax: Double = 0;
   
    var Englishaverage: Double = 0;
    var EnglishSum: Double = 0;
    var EnglishMin: Double = value(0).English;
    var EnglishMax: Double = 0;
   
    var Physicsaverage: Double = 0;
    var PhysicsSum: Double = 0;
    var PhysicsMin: Double = value(0).Physics;
    var PhysicsMax: Double = 0;
   
    var number = 0;
   
    for (i <- 0 until value.length) {
      if (value(i).gender.equals("male")) {
        number = number + 1
        //三科总数
        MathSum = value(i).Math + MathSum
        EnglishSum = value(i).English + EnglishSum
        PhysicsSum = value(i).Physics + PhysicsSum
        //最大和最小
        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\t average\t min\t max \t(Male)")
    Mathaverage=Mathaverage.toInt
    Englishaverage=Englishaverage.toInt
    Physicsaverage=Physicsaverage.toInt
    println("Math:\t   "+Mathaverage+"\t\t"+MathMin+"\t "+MathMax)
    println("English:\t   "+Englishaverage+"\t\t"+EnglishMin+"\t "+EnglishMax)
    println("Physics:\t   "+Physicsaverage+"\t\t"+PhysicsMin+"\t "+PhysicsMax)
  }
   
  def getDataFeMale(value: ArrayBuffer[Student]): Unit ={
    var Mathaverage:Double=0;
    var MathSum:Double = 0;
    var MathMin:Double = value(1).Math;
    var MathMax:Double = 0;
   
    var Englishaverage:Double=0;
    var EnglishSum:Double = 0;
    var EnglishMin:Double = value(1).English;
    var EnglishMax:Double = 0;
   
    var Physicsaverage:Double=0;
    var PhysicsSum:Double = 0;
    var PhysicsMin:Double = value(1).Physics;
    var PhysicsMax:Double = 0;
   
    var number = 0;
   
    for (i <- 0 until value.length){
      if(value(i).gender.equals("female")){
        number=number+1
        //三科总数
        MathSum = value(i).Math+MathSum
        EnglishSum = value(i).English+EnglishSum
        PhysicsSum = value(i).Physics+PhysicsSum
        //最大和最小
        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\t average\t min\t max \t(FeMale)")
    Mathaverage=Mathaverage.toInt
    Englishaverage=Englishaverage.toInt
    Physicsaverage=Physicsaverage.toInt
    println("Math:\t   "+Mathaverage+"\t\t"+MathMin+"\t "+MathMax)
    println("English:\t   "+Englishaverage+"\t\t"+EnglishMin+"\t "+EnglishMax)
    println("Physics:\t   "+Physicsaverage+"\t\t"+PhysicsMin+"\t "+PhysicsMax)
  }
}
class Student(id1:Int,gender1:String,Math1:Double,English1:Double,Physics1:Double){
  var id:Int = id1
  var gender:String = gender1
  var Math:Double = Math1
  var English:Double = English1
  var Physics:Double = Physics1
   
  override def toString = s"Student($id, $gender, $Math, $English, $Physics)"
}

  

编译运行之后的结果:

技术图片

 

学习进度12

标签:不同的   blog   end   table   要求   字符串表   构造函数   sharp   array   

原文地址:https://www.cnblogs.com/kt-xb/p/12300958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!