【Java程序设计、UML面向对象分析与设计】Java程序设计题(UML类图设计)

题目

有一些圆(由半径确定)和矩形(由长和宽)确定,需要根据它们的周长或面积对它们进行排序,并输出排序后的图形信息(图形类型,图形属性特征,图形周长与面积等)。试采用面向对象程序设计思想,用UML类图描述你的设计方案,包括:
(1)类名,类的主要属性及类的主要方法;
(2)类与类之间的关系。
并给出:(1)每个类的主要功能描述,类的每个主要属性的作用,类的每个主要方法的功能描述;(2)从代码重复率,程序的可扩展性等角度简要评价你的设计方案。

答题

(1)类名、主要属性及主要方法见UML设计类图
在这里插入图片描述
(2)Rectangle、Circle类是Shape类的子类,Shapes类对Shape类有依赖关系,Test类对Shape类(抽象类)、Shapes类、Rectangle类、Circle类有依赖关系。
并给出之后的省略,来不及详细写了,看得懂代码的话应该就可以明白了。

UML设计

在这里插入图片描述

代码实现(分为五个块)

1、<< abstract >> Shape

public abstract class Shape implements Comparable<Shape>
{
    
    
    private String ShapeName;
    abstract double calPerimeter();
    abstract double calArea();
    abstract void print();//显示图形面积
    public Shape(String ShapeName)
    {
    
    
        this.ShapeName = ShapeName;
    }
    
    public String toString()
    {
    
    
        String str = "图形信息如下:\n";
        str += "图形类型:" + ShapeName + "\n";
        return str;
    }
    
    public int compareTo(Shape shape)
    {
    
    
        double area1 = this.calArea();
        double area2 = shape.calArea();
        if(area1 > area2)
            return -1;
        else if(area1 == area2)
            return 0;
        else
            return 1;
    }
}

2、Rectangle

public class Rectangle extends Shape
{
    
    
   double length,width;
   Rectangle(String name,double l,double w)
   {
    
    
       super(name);
       length=l;
       width=w;
   }
   void setlandw(double l,double w)
   {
    
    
       length = l;
       width = w;
   }
   double calArea()
   {
    
    
       return length * width;
   }
   double calPerimeter()
   {
    
    
       return (length + width) * 2;
   }
   public String toString()
   {
    
    
       String str = super.toString();
       str += "面积为:" + calArea();
       return str;
   }
   void print()
   {
    
    
       System.out.println(toString());
       System.out.println();
    }
}

3、Circle

public class Circle extends Shape
{
    
    
    double Radius;
    Circle(double r,String name)
    {
    
          
        super(name);
        Radius = r;
    }
    void setRadius(double r)
    {
    
    
        Radius = r;
    }
    double calPerimeter()
    {
    
    
        return Radius*2*Math.PI;
    }
    double calArea()
    {
    
    
        return Radius*Math.PI*Math.PI;
    }
    public String toString()
    {
    
    
        String str = super.toString();
        str += "面积为:"+calArea();
        return str;
    }
    void print()
    {
    
    
        System.out.println(toString());
        System.out.println();
    }
}

4、Shapes

import java.util.*;
public class Shapes
{
    
    
    private List<Shape> shapeList;
    public Shapes()
    {
    
    
        shapeList = new ArrayList<Shape>();
    }
    
    public void addShape(Shape shape)
    {
    
    
        shapeList.add(shape);
    }
    
    public int size()
    {
    
    
        return shapeList.size();
    }
    
    public Shape getShape(int index)
    {
    
    
        if(index < 0 || index >= shapeList.size())
            return null;
        return shapeList.get(index);
    }
    
    public void sort()
    {
    
    
        Collections.sort(shapeList);
    }
    
    public void print()
    {
    
    
        for(Shape shape : shapeList)
        {
    
    
            shape.print();
        }
    }
}

5、Test——测试主类

import java.util.*;
public class Test
{
    
    
   public static void main(String[] args)
    {
    
    
        Shapes shapes = new Shapes();
        int shapeNumber = 5;  
        for (int i = 0; i < shapeNumber; i ++)  
        {
    
    
            Shape shape = createShapeByRandom();
            shapes.addShape(shape);
        }
        
        shapes.sort();
        
        for (int i = 0; i < shapes.size(); i ++)
        {
    
    
            Shape shape = shapes.getShape(i);
            shape.print();
        }
    }
    
   public static Shape createShapeByRandom()
   {
    
    
       Random rand = new Random();
       int shapeType;  //1表示矩形,2表示圆形
       shapeType = rand.nextInt(2) + 1;
       Shape shape;
       if(shapeType == 1)
       {
    
    
           double length,width;
           length = Math.random() * 100;
           width = Math.random() * 100;
           shape = new Rectangle("矩形",length,width);
       }
       else
       {
    
    
           double radius;
           radius = Math.random() * 100;
           shape = new Circle(radius,"圆形");
       }
       return shape;
   }
}

Guess you like

Origin blog.csdn.net/passer__jw767/article/details/111003177