2021-2022年寒假學習進度10
一、實驗目的
- 掌握 Scala語言的基本語法、資料結構和控制結構;
- 掌握面向物件程式設計的基礎知識,能夠編寫自定義類和特質;
- 掌握函數語言程式設計的基礎知識,能夠熟練定義匿名函式。熟悉 Scala的容器類庫的基本層次結構,熟練使用常用的容器類進行資料;
- 熟練掌握 Scala的 REPL執行模式和編譯執行方法。
二、實驗平臺
已經配置完成的 Scala 開發環境。Scala 版本為 2.11.8.
三、實驗內容和要求
1.計算級數
請用指令碼的方式程式設計計算並輸出下列級數的前 n項之和 Sn,直到 Sn剛好大於或等於 q 為止,其中q為大於 0的整數,其值通過鍵盤輸入。
例如, 若
使用尾遞迴的方法來計算級數
import scala.io.StdIn object Test { def main(args: Array[String]): Unit = { println("請輸入q:") val q=StdIn.readInt() println("計算級數為:"+data(q)) } def data(q:Int):Float={ def loop(n:Int,current:Float):Float={ if(current>q)return current loop(n+1, current+(n+1).toFloat/n) } loop(2,2) } }
2.模擬圖形繪製
對於一個圖形繪製程式,用下面的層次對各種實體進行抽象。定義一個 Drawable的特質,其包括一個 draw方法,預設實現為輸出物件的字串表示。定義一個 Point類表示點, 其混入了 Drawable特質,幷包含一個 shift方法,用於移動點。所有圖形實體的抽象類為
Shape,其建構函式包括一個 Point型別,表示圖形的具體位置(具體意義對不同的具體圖形不一樣)。Shape類有一個具體方法 moveTo和一個抽象方法 zoom,其中 moveTo將圖形從當前位置移動到新的位置, 各種具體圖形的 moveTo可能會有不一樣的地方。zoom方法實現對圖形的放縮,接受一個浮點型的放縮倍數引數,不同具體圖形放縮實現不一樣。繼承
package experiment /** * @ClassName MyDraw.java * @author 趙浩博 * @version 1.0.0 * @Description TODO * @createTime 2022年01月10日 18:04:00 */ 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(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 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 } }
計學生成績
學生的成績清單格式如下所示,第一行為表頭,各欄位意思分別為學號、性別、課程名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 |
|
樣例 1 的統計結果輸出為: |
|||||
course |
average |
min |
max |
||
Math: |
69.20 |
44.00 |
89.00 |
||
English: |
71.70 |
54.00 |
87.00 |
||
Physics: |
76.65 |
54.00 |
100.00 |
||
course |
average |
min |
max (males) |
||
Math: |
72.67 |
50.00 |
89.00 |
||
English: |
67.75 |
54.00 |
87.00 |
Physics: course |
75.83 average |
61.00 min |
100.00 max (females) |
||||
Math: |
64.00 |
44.00 |
73.00 |
||||
English: |
77.63 |
71.00 |
87.00 |
||||
Physics: |
77.88 |
54.00 |
100.00 |
||||
測試樣例 2 |
|||||||
Id |
gender |
Math |
English |
Physics |
Science |
||
301610 |
male |
72 |
39 |
74 |
93 |
||
301611 |
male |
75 |
85 |
93 |
26 |
||
301612 |
female |
85 |
79 |
91 |
57 |
||
301613 |
female |
63 |
89 |
61 |
62 |
||
301614 |
male |
72 |
63 |
58 |
64 |
||
301615 |
male |
99 |
82 |
70 |
31 |
||
301616 |
female |
100 |
81 |
63 |
72 |
||
301617 |
male |
74 |
100 |
81 |
59 |
||
301618 |
female |
68 |
72 |
63 |
100 |
||
301619 |
male |
63 |
39 |
59 |
87 |
||
301620 |
female |
84 |
88 |
48 |
48 |
||
301621 |
male |
71 |
88 |
92 |
46 |
||
301622 |
male |
82 |
49 |
66 |
78 |
||
301623 |
male |
63 |
80 |
83 |
88 |
||
301624 |
female |
86 |
80 |
56 |
69 |
||
301625 |
male |
76 |
69 |
86 |
49 |
||
301626 |
male |
91 |
59 |
93 |
51 |
||
301627 |
female |
92 |
76 |
79 |
100 |
||
301628 |
male |
79 |
89 |
78 |
57 |
||
301629 |
male |
85 |
74 |
78 |
80 |
||
樣例 2 的統計結果為: |
|||||||
course |
average |
min |
max |
||||
Math: |
79.00 |
63.00 |
100.00 |
||||
English: |
74.05 |
39.00 |
100.00 |
||||
Physics: |
73.60 |
48.00 |
93.00 |
||||
Science: |
65.85 |
26.00 |
100.00 |
||||
course |
average |
min |
max |
||||
Math: |
77.08 |
63.00 |
99.00 |
||||
English: |
70.46 |
39.00 |
100.00 |
||||
Physics: |
77.77 |
58.00 |
93.00 |
||||
Science: |
62.23 |
26.00 |
93.00 |
||||
course |
average |
min |
max |
||||
Math: |
82.57 |
63.00 |
100.00 |
||||
English: |
80.71 |
72.00 |
89.00 |
||||
Physics: |
65.86 |
48.00 |
91.00 |
||||
Science: |
72.57 |
48.00 |
100.00 |