重庆邮电大学《Java程序设计及Eclipse案例开发》随堂考试实验报告学院名称专业名称班级学生学号学生姓名指导教师唐晓军完成时间2015年12月2日最终评定成绩一、实验题目:1、(30分)从键盘读入10个字符串存入数组a中,然后输出这10个字符串中最大字符串和最小字符串。
class Test{public static void main(String args[]) {String max = "a";String min = "z";for(int i=0;i<10;i++){try{System.out.println("请输入字符串:");BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String readline = br.readLine();if(pareTo(readline)<0){max=readline;}if(pareTo(readline)>0){min=readline;}}catch(NumberFormatException e){System.out.println("字符串格式输入有误,请重新输入。
\n");i--;}catch(IOException e){}}System.out.println("最大字符串为:"+max);System.out.println("最小字符串为:"+min);}}2、(40分)自定义一个矩形类(Rectangle),包含的属性有:长(length),宽(width),包含的方法有:关于属性的setter和getter方法,即setLength,getLength,setWidth,getWidth,计算矩形面积的方法(getArea)。
定义矩形类的子类正方形类(Square),包含的属性和方法自行确定,要求完成的功能是,能计算正方形的面积。
定义一个测试类(Test),测试矩形类和正方形类能否正确的计算面积。
以上类中属性和方法的访问权限自行确定,方法和构造方法若有参数,也自行确定。
public class Rectangle {int Length;int Width;public int getLength() {return Length;}public void setLength(int length) {Length = length;}public int getWidth() {return Width;}public void setWidth(int width) {Width = width;}int getArea(){return Length * Width;}}public class Square extends Rectangle{Square(int border) {super.setLength(border);super.setWidth(border);}}public class Test {public void test(){System.out.println("请选择计算的形状的序号:1.矩形 2.正方形");Scanner sc = new Scanner(System.in);int i = sc.nextInt();int len,wid;if(i==1){System.out.print("请输入矩形的长:");Scanner s = new Scanner(System.in);len = s.nextInt();System.out.print("请输入矩形的宽:");wid = s.nextInt();Rectangle re = new Rectangle();re.setLength(len);re.setWidth(wid);System.out.println("矩形面积为:"+re.getArea());}else if(i==2){System.out.print("请输入正方形的边长:");Scanner s = new Scanner(System.in);len = s.nextInt();Square sq = new Square(len);System.out.println("正方形面积为:"+sq.getArea());}else{System.out.println("输入错误!");}}public static void main(String[] args) {new Test().test();}}3、(30分)定义一个Person类,包含姓名(name)、身高(height)、体重(weight),以及talk()方法,该方法的功能是,输出自己的身高和体重信息。
Person类实现Comparable接口,实现按照体重的大小比较两个Person对象的大小。
最后,定义一个测试类,生成一个数组,该数组有6个元素,每个元素类型是Person,调用Arrays.sort方法对该数组排序。
完成以下代码:package com.test;public class Person implements __ Comparable _______<Person>{private String name;private float height, weight;public Person(String name, float height, float weight) { super(); = name;this.height = height;this.weight = weight;}public String getName() {return name;}public void setName(String name) { = name;}public float getHeight() {return height;}public void setHeight(float height) {this.height = height;}public float getWeight() {return weight;}public void setWeight(float weight) {this.weight = weight;}public void speak() {System.out.println("我是"+ name+ ",我的身高是"+ height + ",我的体重是" + weight);}public int compareTo(Person o) {int flag;if(this.weight<o.weight){flag = -1;}else{flag =1;} // 实现此处的代码return flag;}@Overridepublic String toString() {return"Person [name=" + name + ", height=" + height + ", weight="+ weight + "]";}}////////////////////////package com.test;import java.util.Arrays;public class Test {public static void main(String[] args) {int i;Person ps[] = new Person[6];ps[0] = new Person("张三", 170, 110);ps[1] = new Person("李四", 168, 120);ps[2] = new Person("王五", 165, 115);ps[3] = new Person("赵六", 172, 121);ps[4] = new Person("周七", 160, 100);ps[5] = new Person("郑八", 166, 119);System.out.println("排序前数组元素的序列是:");for (i = 0; i < ps.length; i++) {ps[i].speak();}_ Arrays.sort(ps); _____;//调用Java系统类中的排序方法对ps数组排序System.out.println("\n排序后数组元素的序列是:");for (i = 0; i < ps.length; i++) {System.out.println(ps[i]);}}}二、实验目的:随堂考试,检查平时上机实验学习情况。
三、实验要求:完成题目的源代码编写,并按照要求提交到指定FTP位置,并完成本实验报告,现场提交。
上传位置:ftp:\\172.16.35.22学生上传帐号:upload, 上传密码:upload学生下载帐号:stu, 下载密码:stu 四、实验主要步骤:简单描述编程思路。
五、实验结果:此处粘贴源代码,以及程序运行结果。
六、心得体会:简单写两句心得体会,经验总结、建议等。