Java(实验三)类与对象-定义并实现一个长方体类(Cube),包含长(length)、宽(width)与高(height)等三个属性
一、实验目的:
1、学会定义并实现类。
2、学会定义并创建类的对象,通过类的对象访问类的成员属性与方法。
3、学会定义并实现派生类,学会使用派生类的对象。
4、理解并学会使用类的多态性。
二、实验环境:
Eclipse+Windoe10+Java
三、实验内容:
1.定义并实现一个长方体类(Cube),包含长(length)、宽(width)与高(height)等三个属性,包含计算体积(calVolume)与计算表面积(calArea)等两个方法,类的属由构造函数进行初始化或通过成员函数赋值。编写一段程序,测试该类。
package code3;
public class Cube {float length,width,heigth;
double calVolume(){ return length*width*heigth; }
double calArea(){return 2*(length*width + width*heigth + length*heigth);}
public static void main(String[] args) { // TODO Auto-generated method stubCube c=new Cube();c.heigth=5;c.length=4;c.width=3;System.out.println("长方体面积为:"+c.calArea());System.out.println("长方体体积为:"+c.calVolume());}}
2.定义并实现一个三角形类(Triangle),其三个边长(edge1, edge2, edge3)为其属性,包含判断其是否为三角形(isTriangle)、计算周长(calPerimeter)及计算面积(calArea)等三个方法,类的属由构造函数进行初始化或通过成员函数赋值。编写一段程序,测试该类。
package code3;
public class Triangle {static double edge1,edge2,edge3;
boolean isTriangle(){
if(edge1+edge2>edge3 && edge2+edge3>edge1 && edge1+edge3>edge2)
return true; return false; }
double calPerimeter(){ return edge1+edge2+edge3; }
double calArea(){ double a,b; a=(edge1+edge2+edge3)/2; b=(float)Math.sqrt(a*(a-edge1)*(a-edge2)*(a-edge3)); return b; }
public static void main(String[] args) { // TODO Auto-generated method stub Triangle c=new Triangle(); c.edge1=3; c.edge2=4; c.edge3=5; System.out.println("三角形的三边为:"+edge1+" "+edge2+" "+edge3);
if(c.isTriangle()) { System.out.println(); System.out.println("构成一个三角形"); System.out.println("三角形面积:"+c.calArea()); System.out.println("三角形体积:"+c.calPerimeter()); } else System.out.println("不是三角形"); }}
3.定义并实现一个Person类,包含姓名(name)与编号(code)等两个属性,通过构造函数为属性赋值,拥有显示属性值的方法(showInfo)。从Person类派生出一个Student类,拥有数学成绩、英语成绩、Java成绩等三个属性,拥有输入成绩、计算平均成绩、显示信息(姓名、编号及平均值)等方法。编写一段程序,测试这两个类。
package code3;
import java.util.Scanner;
class person { double code; String name; public person(int code, String name) { super(); this.code = code; this.name = name; } public double getCode() { return code; } public void setCode(int code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; }
public void showInfo(){ System.out.println("姓名为:" + getName()); System.out.println("编号为:" + getCode()); }} class student extends person{public student(int code, String name) { super(code,name); this.code = code; this.name = name; }double math,eng,java;
public double getmath() { return math;}
public void setmath(double math) {this.math = math ;}
public double getEng() {
return eng ;
}
public void setEng (double eng ) {
this.eng = eng ;}
public double getJava () {
return java ;}
public void setJava (double java ) {
this.java= java;}
public void intpu(){
Scanner input = new Scanner(System.in);
System.out.println("请分别输入数学、英语、java成绩:");
double math = input.nextDouble();
double eng = input.nextDouble();
double java = input.nextDouble();
setmath (math);
setEng (eng);
setJava (java );
input.close(); }
public double s(){
return (math+eng+java)/3;
}
public void show(){
person p = new person(1,"小红");p.showInfo(); System.out.println(p.getName()+"数学成绩为:" + getmath()); System.out.println(p.getName()+"英语成绩为:" + getEng());System.out.println(p.getName()+"java成绩为:" + getJava()); double avg = s(); System.out.print(p.getName()+"平均成绩为:" + avg); }}
public class text {
public static void main(String[] args) {
// TODO Auto-generated method stub
student stu = new student(0,null);
stu.intpu();
stu.show();
}}
4.定义并实现一个Circle类,属性为圆的半径radius,其值由构造函数初始化。包含计算周长(calPerimeter)与计算面积(calArea),显示信息(半径、周长与面积)(showInfo)等方法。从Circle类派生出Cylinder类,拥有高(height)这个属性,其值由构造函数初始化。包含计算表面积(calArea)、计算体积(calVolume)及显示信息(半径、表面积、体积)(showInfo)等方法。编写一段程序,测试这两个类。
package code3;
class Circle{ private double radius; public Circle() { super(); } public Circle(double radius) { super(); this.radius = radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public double calPerimeter(){ return 2*Math.PI*getRadius(); // 2*π*R } public double calArea(){ return Math.PI*Math.pow(getRadius(), 2); //π*R*R } public void showInfo(){ System.out.println("圆的半径为:" + getRadius()); String str = String.format("圆的周长为:%.2f", calPerimeter()); System.out.println(str); String str2 = String.format("圆的面积为:%.2f", calArea()); System.out.println(str2); } }class Cylinder extends Circle {private double height; Circle cir = new Circle(1.0); public Cylinder() { super(); } public Cylinder(double height) {super(); this.height = height; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double calArea2(){ return (cir.calArea()*2 + cir.calPerimeter()*getHeight()); } public double calVolume(){ return cir.calArea()*getHeight(); }public void showInfo2(){ cir.showInfo(); System.out.println("圆柱体的半径为:" + cir.getRadius()); String str3 = String.format("圆柱体表面积为:%.2f", calArea2()); System.out.println(str3); String str4 = String.format("圆柱体的体积为:%.2f", calVolume()); System.out.println(str4); }}public class five {public static void main(String[] args) { // TODO Auto-generated method stub Cylinder cyl = new Cylinder(4.0); cyl.showInfo2(); }}
5.定义并实现如下三个类:(1)Shape类,无属性,有一个抽象方法calArea;(2)Rectangle类,从Shape类派生,有长度(length)与宽度(width)两个属性,需重写calArea方法;(3)Circle类,从Shape类派生,有半径(Radius)一个属性,需重写calArea方法。编写一段程序来测试这几个类。
package code3;
abstract class Shape { public abstract double calArea();}class Rectangle extends Shape { private double length; private double width; public Rectangle() { super(); } public Rectangle(double length, double width) { super(); this.length = length; this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double calArea(){ return getLength()*getWidth(); }}class Circle extends Shape { private double Radius; public Circle() { super(); } public Circle(double radius) { super(); Radius = radius; } public double getRadius() { return Radius; } public void setRadius(double radius) { Radius = radius; } public double calArea() { return Math.PI*(Math.pow(getRadius(), 2)); }}public class six { public static void main(String[] args) { // TODO Auto-generated method stub Rectangle R = new Rectangle(5.0,7.0); System.out.println("矩形的面积为:" + R.calArea()); Circle C = new Circle(1.0); System.out.println("圆形的面积为:" + C.calArea()); }}
6.在5的基础上,从Rectangle类派生Cube类,有属性高度(width),有计算表面积(calArea)及计算体积(calVolume)等方法。编写一段程序来测试这几个类。
package code3;
class Cube extends Rectangle { private double width; Rectangle r = new Rectangle(2,3);
public Cube(double width) { super(); this.width = width; }
public Cube() { super(); // TODO Auto-generated constructor stub}
public Cube(double length, double width) { super(length, width); // TODO Auto-generated constructor stub }
public double getWidth() { return width; }
public void setWidth(double width) { this.width = width;
public double calArea(){ return 2*(r.getLength()*r.getWidth()+r.getLength()*getWidth()+r.getWidth()*getWidth());
}
public double calVolume(){ return r.calArea()*getWidth(); }}
public class seven { public static void main(String[] args) { // TODO Auto-generated method stub Cube c = new Cube(2); System.out.println("长方体的面积为:" + c.calArea()); System.out.println("长方体的体积为:" + c.calVolume()); }}
Java(实验三)类与对象-定义并实现一个长方体类(Cube),包含长(length)、宽(width)与高(height)等三个属性相关推荐
- JAVA创建一个Box类(长方体),在Box类中定义三个变量,分别表示长方体的长(length)、宽(width)和高(heigth)
编程创建一个Box类(长方体),在Box类中定义三个变量,分别表示长方体的长(length).宽(width)和高(heigth),再定义一个方法void setBox(int l, int w, i ...
- PHP:类和对象定义及使用
类和对象定义及使用 <?php // 类 class Person{// 属性public $name;public $age;// 构造方法public function __construc ...
- c++课后题,声明一个长方体类Box,该类有长度(length),宽度(width),高度(height)三个数据成员,类中有获取及修改长度…………
c++课后题,声明一个长方体类Box,该类有长度(length),宽度(width),高度(height)三个数据成员, 类中有获取及修改长度,宽度,高度的函数,还有计算长方体表面积和体积的函数.请按 ...
- 设计一个长方体类Cuboid(Java)
设计一个长方体类Cuboid (10 分) 要求:设计一个名为Cuboid的类表示长方体.这个类包括三个名为length.width和height 的double型数据域,它们分别表示长方体的长.宽和 ...
- 4.定义并实现一个Person类,包含姓名(name)与编号(code)等两个属性,通过构造函数为属性赋值,拥有显示属性值的方法(showInfo)。从Person类派生出一个Student类,拥有数
package monster.zf.Test; /*** 4.* 定义并实现一个Person类,包含姓名(name)与编号(code)* 等两个属性,通过构造函数为属性赋值,拥有显示属性值的方法(s ...
- 深入篇【C++】类与对象:友元函数与友元类
深入篇[C++]类与对象:友元函数与友元类 ①.提出问题:重载operator<< ②.解决问题:友元 Ⅰ.友元函数 [特点] Ⅱ.友元类 [特点] ③.总结问题 ①.提出问题:重载ope ...
- java有且仅有一个main_组成java Application的若干类中,有且仅有一个主类,只有主类中含有主方法main();...
[单选题]Java 应用程序入口的 main 方法,其声明格式可以是(A) [填空题]点的水平投影与正面投影的连线( ) 于 OX 轴. [单选题]-Is there anything wrong w ...
- 定义并实现一个三角形类(Triangle),其三个边长(edge1, edge2, edge3)为其属性,包含判断其是否为三角形(isTriangle)、计算周长(calPerimeter)及计算面积
package monster.zf.Test; /*** 定义并实现一个三角形类(Triangle),其三个边长(edge1, edge2, edge3)* 为其属性,包含判断其是否为三角形(isT ...
- 类是对象的抽象,对象是类的实例
类是对象的抽象,对象是类的实例 是 正确的 看:https://www.cnblogs.com/marlanchen/p/11563929.html[转载]
最新文章
- AppleWatch开发教程之调试程序使用帮助文档
- 分布式系统架构设计系列文章
- oracle如何导出和导入数据库/表
- caffe使用ctrl-c不能保存模型
- C语言(CED)编写程序,求sum=1*1*1+2*2*2+3*3*3+4*4*4+5*5*5+····+n*n*n
- Vue 学习笔记(2)Vue 生命周期、组件
- 研究生量子计算机专业,量子计算机研究.PDF
- 02. 实现Singleton模式(C++版本)
- 中国企业云计算应用现状及需求调研报告
- Hexo next主题修改背景报 failed to locate @import file F:\blog\source\_data\styles.styl
- 数据库系列之MySQL表ibd文件删除恢复
- 交换机组播风暴_交换机广播风暴控制知识
- 玩转NFT夏季:这份工具宝典值得收藏
- 第十一届蓝桥杯b组(10月真题)
- Hexo + yilia 主题实现文章目录
- 数据库与php衔接,【杂谈】PHP怎样衔接Mysql数据库
- 【IOS学习之工具学习】sublime text mac(代码编辑器)
- 新版股票api接口大全
- 树莓派3一根网线直连电脑(针对树莓派不能上网有方法解决)
- 计算机中的用户拒绝访问权限,win7系统打开c盘提示“拒绝访问”的处理方法