云南大学软件学院 实验报告
课程: Java程序设计实验 学期: 2012-2013学年 第一学期 任课教师: 陆歌浩
专业: 软件工程 学号: 序号: 姓名: 成绩:
实验5 Applying OO Concepts Using Java
一、实验目的
练习Java中面向对象概念的应用--学会使用现有对象和自定义类。
二、实验内容
Program
Create a class called Circle.java with fields named radius, area, and diameter. Include methods named setRadius(), getRadius, and computeDiameter(), which calculates the circle's diameter, and computeArea(), which computes the circle's area.
Hint: diameter of a circle is twice its radius.
Hint: area of a circle is 3.14 multiplied by the square of the radius.
Create a class called TestCircle. java whose main method declares 2 Circle objects. Using setRadius() method, assign one Circle a small radius value and assign another a larger radius value. Call computeDiameter() and computeArea for each circle and display the results.
Output result of using command input "5 8":
The area of a is 78.5
The diameter of a is 10.0
The area of b is 200.96
The diameter of b is 16.0
实验代码:
import javax.swing.JOptionPane;
public class TestCircle {
public static void main(String[] args)
{
Circle a = new Circle();
Circle b = new Circle();
double rad_a,rad_b;
rad_a = Double.parseDouble(JOptionPane.showInputDialog("请输入圆a的半径:"));
//接收圆a的半径
rad_b = Double.parseDouble(JOptionPane.showInputDialog("请输入圆b的半径:"));
//接收圆b的半径
a.setRadius(rad_a); //设置圆a的半径
b.setRadius(rad_b); //设置圆b的半径
String str1 ="The area of a is ";
String str2 ="The diameter of a is ";
String str3 ="The area of b is ";
String str4 ="The diameter of b is ";
JOptionPane.showMessageDialog(null,str1 + a.computeArea()+"\n" + str2 +a.computeDiameter()+
"\n"+ str3 +b.computeArea() +"\n"
+ str4 +b.computeDiameter(),"执行结果(孙金文——20101120007)", JOptionPane.PLAIN_MESSAGE);
}
}
class Circle
{
private double radius;
private double area;
private double diameter;
static final double PI=3.14;
public void setRadius(double rad) //设置半径的方法
{
radius = rad;
}
public double getRadius() //返回半径的方法
{
return radius;
}
public double computeDiameter() //计算直径的方法
{
diameter = radius*2;
return diameter;
}
public double computeArea() //计算面积的方法
{
area = PI* Math.pow(radius, 2);
return area;
}
}
运行截图:
PAGE
1
本文档为【云大JAVA实验5Lab #5(陆歌浩)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。