Java类的定义

来源:百度知道 编辑:UC知道 时间:2024/06/17 01:32:33
完成如下实验:
1、编程实现矩形类,其中应包括计算矩形周长和面积的方法。
(1)使用矩形类,编程统计若干块土地的相关信息。由用户输入每块土地的长与宽(自己确定),程序将计算其面积并显示出来。
(2)从矩形类派生一个子类:长方体类。对长方体类的操作是求表面积和体积。编程检查、运行所编写的长方体类。
2、编程实现复数类,实现复数的四则运算。并编写程序,测试所写的复数类。
3、编写一个类Book,代表教材:
(1)具有属性:名称(title)、页数(pageNum),其中页数不能少于200页,否则输出错误信息,并赋予默认值200;
(2)具有方法:detail,用来在控制台输出每本教材的名称和页数;
(3)具有带参数的构造方法:用来完成对象的初始化工作,并在构造方法中完成对页数的最小值限制。

package java.lancs ;

/**
* Graphics objects for practical classes (Java 1.1 version)
* @author Roger Garside/Richard Cardoe
* @version Last Rewritten: 24/Sept/97
*/

import java.awt.* ;
import java.awt.event.* ;

/*
* class to hold details about the shape to draw
*/
class BasicShape
{
// name of the shape - RECTANGLE, OVAL, etc.
int shape ;
// dimensions of the shape
int x, y, w, h ;
// colour of the shape
Color colour ;

// constructor to initialise the variables to default values
public BasicShape()
{
shape = -1 ;
x = -1 ;
y = -1 ;
w = -1 ;
h = -1 ;
colour = Color.green ;
} // end of constructor method

// constructor to initialise the variables to specifier values
public BasicShape(int sh, int x1, int y1, int w1, int h1, C