Experiment description
Given a set of data, the average score of each student is realized through a spark programming experiment, and the implementation format is: student number:, name:, Chinese, mathematics:, English:, average score: in the order.
Experimental conditions:
Install the Idea software, preferably not the latest version, and insert the scala plug-in into idea. Experimental steps:
Install the scala plug-in in idea:
the first step,
The second step,
Finally, I have decided to install the scala plug-in in the idea, and then I can experiment.
The next step is to create the file: File —> New —> Project —> Maven —>…. You only need to follow the prompts.
Data block score1.txt file:
Student name Chinese Mathematics English
2001501 Li Xiaoming 88 91 86
2001502 Zhang Li 75 81 65
2001503 Liu Chengdong 62 71 58 2001504 Hao Gang 79 85 83 2001505 Liu
Xiaoxiao 75 62 53 2001506 Wang Yan 88 91 93
2001507 Li Yuan81 78 83 2001508 Zhang Dongdong70 83 68 2001509 Li Jian77 80 85 2001510 Wang Xiaoyan72 78 71
api.scala part:
object api{
def main(args: Array[String]): Unit = {
import scala.io.Source
val inputfile = Source.fromFile("C:\\Users\\leglon\\IdeaProjects\\che1\\src\\main\\Scala\\score1.txt") //测试样例文件的路径
val lines = inputfile.getLines
val Data = lines.map{
_.split(" ")}
val originalData = Data.toList
val header = originalData.head
val courseNames=header.drop(2)
val courseNum = courseNames.length
def statistic(lines:List[Array[String]])={
for(i<-2 to courseNum+1) yield{
val temp = lines map {
elem => elem (i).toDouble}
(temp.max, temp.min, temp.sum)
def printResult(thersult: Seq[(Double,Double,Double)]): Unit={
(courseNames zip thersult) foreach {
case (course,result)=>println(f"${
course + ":}%-10s${
result._1}%5.2f${
result._2}%8.2f${
result._3}%8.2f"
def getFailureInEnglish(): Unit = {
}
val allResult = statistic ( val allSudents)
println("课程 平均成绩 最低分 最高分")
printResult(allResult)
student.scala section:
class student {
var id:Int= _
var name:String= _
var chinese:Float= _
var math:Float= _
var english:Float= _
def getAverageGrade={
((chinese+math+english)/3)
.formatted("%.2f")
}
def this(id:Int,name:String,chinese:Float,math:Float,english:Float){
this()
this.id=id
this.name=name
this.chinese=chinese
this.math=math
this.english=english
}
override def toString="学号:"+this.id+",姓名:"+this.name+",语文"+this.chinese+
",数学:"+this.math+",英语:"+this.english+",平均成绩:"+getAverageGrade
}
object student{
def main (arge:Array[String]): Unit = {
val s1 = new student(id = 2001501, name = "李小明", chinese = 88, math = 91, english = 86)
val s2 = new student(id = 2001502, name = "张丽", chinese = 75, math = 81, english = 65)
val s3 = new student(id = 2001503, name = "刘成东", chinese = 62, math = 71, english = 58)
val s4 = new student(id = 2001504, name = "饶刚", chinese = 79, math = 85, english = 83)
val s5 = new student(id = 2001505, name = "刘小小", chinese = 75, math = 62, english = 53)
val s6 = new student(id = 2001506, name = "王艳", chinese = 88, math = 91, english = 93)
val s7 = new student(id = 2001507, name = "李远", chinese = 81, math = 78, english = 83)
val s8 = new student(id = 2001508, name = "张东东", chinese = 70, math = 83, english = 68)
val s9 = new student(id = 2001509, name = "李健", chinese = 70, math = 80, english = 85)
val s10 = new student(id = 2001510, name = "王小燕", chinese = 72, math = 78, english = 71)
val a = Array(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10)
a.map(println)
}
}