简单java程序设计上机实验报告_Java程序设计实验报告(最新整理)

《Java程序设计实验报告(最新整理)》由会员分享,可在线阅读,更多相关《Java程序设计实验报告(最新整理)(8页珍藏版)》请在人人文库网上搜索。

1、Java 程序设计实验报告实验一实验题目:从键盘上读入 10 个字符串存入数组 a 中,然后输出这 10个字符串中最大字符串和最小字符串。实验代码:public class StrPro public static void main(String args) String str = new String5;System.out.println(Please input 10 strings:);int i;String max,min;for(i=0;i0) min = stri;System.out.println(最大的字符串为:+max); System.out.println(最小。

2、的字符串为:+min);实验结果:实验心得体会:掌握了 java 的基本语法,数组的定义与使用,做这个实验要了解字符串数组的定义及字符串数组的输入方法,还有比较字符串数组的大小的调用方法等。实验题目:实验二自定义一个矩形类(Rectangle),包含的属性有:长(length), 宽(width),包含的方法有:关于属性的 setter 和 getter 方法, 即 setLength,getLength,setWidth,getWidth,计算矩形面积的方法(getArea)。定义矩形类的子类正方形类(Square),包含的属性和方法自行确定,要求完成的功能是,能计算正方形的面积。定义一个测。

3、试类(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()retu。

4、rn 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(请输入。

5、矩形的长:); 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(请输入正方形的边长:);elseScanner s = new Scanner(System.in); len = 。

6、s.nextInt();Square sq = new Square(len);System.out.println(正方形面积为:+sq.getArea();System.out.println(输入错误!);public static void main(String args) new Test().test();实验结果:实验心得体会:做这个实验要掌握如何定义类以及类的成员变量、类的方法,学会对象的创建、对象属性的引用和方法的调用以及如何定义和使用构造方法。掌握 this 的使用以及派生子类的方法,理解关键字 super 的含义。理解继承中属性的隐藏和方法的覆盖机制,理解在继承关系中构。

7、造方法的调用过程。实验三实验题目:定义一个 Student 类,包含姓名(name)、身高(height)、体重(weight),以及 talk()方法,该方法的功能是,输出自己的身高和体重信息。Student 类实现 Comparable 接口,实现按照体重的大小比较两个 Student 对象的大小。最后,定义一个测试类,生成一个数组,该数组有 6 个元素,每个元素类型是 Student,调用 Arrays.sort 方法对该数组排序。实验代码:public class Student implements Comparable private String name;private flo。

8、at height, weight;public Student(String name, float height, float weight) super(); this.name = name;this.height = height;this.weight = weight;public String getName() return name;public void setName(String name) this.name = name;public float getHeight() return height;public void setHeight(float heigh。

9、t) 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);Overridepublic int compareTo(Student o) int flag; if(this.weighto.weight)flag = -1;elseflag =1。

10、;return flag;Overridepublic String toString() return Person name= + name + , height= + height + ,weight=+ weight + ;public class Test public static void main(String args) int i;Student ps = new Student6;ps0 = new Student(张三, 170, 110);ps1 = new Student(李四, 168, 120);ps2 = new Student(王五, 165, 115);p。

11、s3 = new Student(赵六, 172, 121);ps4 = new Student(周七, 160, 100);ps5 = new Student(郑八, 166, 119);System.out.println(排序前数组元素的序列是:);for (i = 0; i ps.length; i+) psi.speak();Arrays.sort(ps);/调用Java系统类中的排序方法对ps数组排序System.out.println(n排序后数组元素的序列是:);for (i = 0; i ps.length; i+) System.out.println(psi);实验结果:。

12、实验心得体会:本次实验主要掌握对 compareTo 方法的重写,当返回值为 0 的时候此方法调用会出现错误,导致不进行排序,需要特别注意。这个实验主要使我们掌握了对类的接口的实现,和数组的比较,并使我们理解其中的细节。“”“”At the end, Xiao Bian gives you a passage. Minand once said, people who learn to learn are very happy people. In every wonderful life, learning is an eternal theme. As a professional cle。

13、rical and teaching position, I understand the importance of continuous learning, life is diligent, nothing can be gained, only continuous learning can achieve better self. Only by constantly learning and mastering the latest relevant knowledge, can employees from all walks of life keep up with the pace of enterprise development and innovate to meet the needs of the market. This document is also edited by my studio professionals, there may be errors in the document, if there are errors, please correct, thank you。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值