标签:oom cas obj until pre osi his rri ase
实验二的2
模拟图形绘制
object test { 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 } } abstract class shape extends Drawable { def moveTo(point: Point) def zoom(position: Double) def draw() } case class Point(var x:Double,var y:Double) extends Drawable{ var Px :Double=x var Py :Double=y def shift(deltaX:Double,deltaY:Double){ Px+=deltaX;Py+=deltaY } override def draw():Unit = { println("Point:("+Px+","+Py+")") } } trait Drawable{ def draw(){println(this.toString)} } class Line(point1: Point, point2: Point)extends shape { override def moveTo(point: Point):Unit = { point1.shift(point.Px,point.Py) point2.shift(point.Px,point.Py) } override def zoom(position:Double): Unit ={ var lengthx = point2.Px-point1.Px var lengthy = point2.Py-point1.Py if(lengthx<0){ lengthx = -lengthx; } if(lengthy<0){ lengthy = -lengthy; } var newx = (lengthx/2)*(position-1); point1.Px = point1.Px-newx; point2.Px = point2.Px+newx; var newy =(lengthy/2)*(position-1); point1.Py = point1.Py-newy; point2.Py = point2.Py+newy; } override def draw(): Unit = { println("Line:(" + point1.Px + "," + point1.Py + ")--(" + point2.Px + "," + point2.Py + ")") } } class Circle(point: Point, r: Double) extends shape { var R = r override def moveTo(point: Point): Unit = { this.point.Px = point.Px this.point.Py = point.Py } override def zoom(step: Double): Unit = { R = R * step } override def draw(): Unit = { println("Circle center:(" + point.Px + "," + point.Py + "),R=" + R); } }
3 统计学生成绩
import scala.collection.mutable.ArrayBuffer object test2 { 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)" }
今天学到了如何使用scala来编译运行程序
标签:oom cas obj until pre osi his rri ase
原文地址:https://www.cnblogs.com/zlj843767688/p/12245080.html