(1)定义Point(点)、Circle(圆形)、Square(正方形)类。点信息包括x,y坐标。圆信息包括圆心坐标和半径。正方形信息包括中心坐标和边长。用关联(Point对象作为Circle和Square的成员)和继承(Circle和Square分别继承Point)两种方法实现并测试。
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入p的x坐标:");
double x = sc.nextDouble();
System.out.println("请输入p的y坐标:");
double y = sc.nextDouble();
System.out.println("请输入r或a的值:");
double a = sc.nextDouble();
Circle1 c1 = new Circle1(x, y, a);
System.out.println(c1);
Square1 s1 = new Square1(x, y, a);
System.out.println(s1);
Circle2 c2 = new Circle2(x, y, a);
System.out.println(c2);
Square2 s2 = new Square2(x, y, a);
System.out.println(s2);
}
}
class Point { // 关联:Point对象作为Circle1和Square1的成员
// 继承:Circle2和Square2分别继承Point
public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}
double x, y;
}
class Circle1 { // Point对象作为Circle1和Square1的成员
Point p;
double r;
Circle1(double x, double y, double r) {
this.p = new Point(x, y);
this.r = r;
}
@Override
public String toString() {
return "关联Circle1: p=" + p + ", r=" + r;
}
}
class Square1 { // Point对象作为Circle1和Square1的成员
Point p;
double a;
Square1(double x, double y, double a) {
this.p = new Point(x, y);
this.a = a;
}
@Override
public String toString() {
return "关联Square1: p=" + p + ", a=" + a;
}
}
class Circle2 extends Point { // Circle2和Square2分别继承Point
double r;
Circle2(double x, double y, double r) {
super(x, y);
this.r = r;
}
@Override
public String toString() {
return "继承Square2: " + "p=(" + x + "," + y + "),r=" + r;
}
}
class Square2 extends Point { // Circle2和Square2分别继承Point
double a;
Square2(double x, double y, double a) {
super(x, y);
this.a = a;
}
@Override
public String toString() {
return "继承Square2: " + "p=(" + x + "," + y + "),a=" + a;
}
}
(2)定义抽象类Shape(形状),包含形状名字属性及相应构造方法。
定义两个接口,一个接口IArea定义求面积的方法,一个接口IVolume定义求体积的方法。
定义Shape类的子类矩形(Rectangle)类,实现IArea接口。
定义长方体类同时实现IArea(求表面积)和IVolume接口。
在main方法中测试。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入形状:");
String shape = sc.next();
System.out.println("请分别输入矩形长、宽:");
double x = sc.nextDouble();
double y = sc.nextDouble();
Rectangle rec1 = new Rectangle(shape, x, y);
System.out.println("矩形的面积为:" + rec1.area());
System.out.println("请分别输入长方体长、宽、高:");
double l = sc.nextDouble();
double w = sc.nextDouble();
double h = sc.nextDouble();
T t1 = new T("长方体", l, w, h);
System.out.println("长方体的表面积为:" + t1.area());
System.out.println("长方体的体积为:" + t1.v());
}
}
abstract class Shape { // 定义抽象类Shape(形状),包含形状名字属性及相应构造方法。
String name;
public Shape(String name) { // 形状名字
super();
this.name = name;
}
}
interface IArea { // 面积
double area();
}
interface IVolume { // 体积
double v();
}
class Rectangle extends Shape implements IArea { // Shape子类Rectangle矩形类
double x, y;
public Rectangle(String name, double x, double y) {
super(name);
this.x = x;
this.y = y;
}
@Override
public double area() {
// TODO Auto-generated method stub
return this.x * this.y;
}
}
class T extends Shape implements IArea, IVolume { // T长方体
double len, wid, h; // len长、wid宽、h高
public T(String name, double len, double wid, double h) {
super(name);
this.len = len;
this.wid = wid;
this.h = h;
}
@Override
public double area() { // 长方体求表面积
// TODO Auto-generated method stub
return (len * wid + len * h + h * wid) * 2;
}
@Override
public double v() { // T长方体求体积
// TODO Auto-generated method stub
return len * wid * h;
}
}