Java实验3(3)

2019-04-22 21:16

public String toString() {

return \ } }

11、使用Windows写字板编辑编辑类Cylinder源程序如下: class Cylinder extends Circle {

private double length;

// Default constructor public Cylinder() {

super(); length = 1.0; }

// Construct a cylinder with specified radius, and length public Cylinder(double r, double l) {

super(r); length = l; }

// Getter method for length public double getLength() {

return length; }

// Setter method for length

public void setLength(double length) {

this.length = length; }

// Find cylinder surface area public double findArea() {

return 2*super.findArea()+(2*getRadius()*Math.PI)*length; }

// Find cylinder volume

public double findVolume() {

return super.findArea()*length; }

public boolean equals(Cylinder cylinder) {

return (this.radius == cylinder.getRadius()) && (this.length == cylinder.getLength()); }

// Override the toString() method defined in the Object class public String toString() {

return \ } }

12、使用Windows写字板编辑编辑类Max源程序如下: public class Max {

// Return the maximum between two objects

public static Comparable max(Comparable o1, Comparable o2) {

if (o1.compareTo(o2) > 0) return o1; else

return o2; } }

13、将上面编辑的源程序保存成Java源程序文件(扩展名为java),程序文件

名分别为TestInterface .java、Circle.java、Cylinder.java、Max.java。 a) 进入命令提示符状态,在源程序文件存放目录下分别编译以上源程

序文件。观察编译后源程序文件存放目录中的文件,熟悉Java程序的文件结构。

b) 如果编译正确,则键入如下命令行,使用Java解释器运行源程序: java TestInterface 程序运行结果如下:

14、使用Windows写字板编辑编辑类Circle,源代码如下: public class Circle {

protected double radius;

public Circle() {

this(1.0); }

public Circle(double radius) {

this.radius = radius; }

public double getRadius() {

return radius; }

public void setRadius(double radius) {

this.radius = radius; }

public double findArea() {

return radius*radius*Math.PI;

}

public double findPerimeter() {

return 2*radius*Math.PI; }

public boolean equals(Circle circle) {

return this.radius == circle.getRadius(); }

public String toString() {

return \ } }

15、使用Windows写字板编辑编辑类TestCloneable,源代码如下: public class TestCloneable {

// Main method

public static void main(String[] args) {

// Declare and create an instance of CloneableCircle CloneableCircle c1 = new CloneableCircle(5); CloneableCircle c2 = (CloneableCircle)c1.clone();

System.out.println(\

// Check if two variables point to the same object if (c1 == c2)

System.out.println(\ else

System.out.println(\

// Check if two objects are of identical contents if (c1.equals(c2))

System.out.println(\ else

System.out.println(\

// Modify c1's radius, name c1.setRadius(10);

c1.getCreator().setFirstname(\ c1.getCreator().setMi(\

// Display c1 and c2

System.out.println(\ System.out.println(\ System.out.println(\

System.out.println();

if (c1 instanceof Cloneable) {

System.out.println(\ } else {

System.out.println(\ }

// Check if a Circle object is cloneable Circle c = new Circle(); if (c instanceof Cloneable) {

System.out.println(\ } else {

System.out.println(\ } } }

// CloneableCircle is a subclass of Circle, which implements the // Cloneable interface

class CloneableCircle extends Circle implements Cloneable {

// Store the creator of the object

private Name creator = new Name(\

// Construct a CloneableCircle with specified radius public CloneableCircle(double radius) {

super(radius); }

// Getter method for creator public Name getCreator()


Java实验3(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:取消学校、医院行政级别有利于公共服务均衡化

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: