实训一:构建圆和矩形对象

简介: 实训一:构建圆和矩形对象

1)编程,定义圆类,构建若干个圆对象,输出它们的面积、周长和总个数。

//Circle.java    
class Circle{                                            //圆类
    private double radius;                              //半径字段
    private double x, y;                                //圆心坐标字段
    private static int num;                             //圆对象个数字段
    public static final double PI = 3.14159;            //圆周率常量字段
    public Circle(){                                    //构造方法1
        num ++; 
    }
    public Circle(double radius) throws Exception{      //构造方法2
        if (radius < 0) { throw new Exception("负数不能做圆半径"); }
        else {
            this.radius = radius;
            num ++;
        }
    }
    public double getRadius(){                          //获取半径方法
        return radius; 
    }
    public void  setRadius(double radius) throws Exception{  //设置半径方法
        if (radius < 0) { throw new Exception("负数不能做圆半径"); }
        else { this.radius = radius; }
    }
    public static int getNum(){                         //获取圆对象个数方法
        return num;
    }
    public double calcArea(){                           //计算面积方法
        return PI * radius * radius;
    }
    public double calcGirth(){                          //计算周长方法
        return 2 * PI * radius;
    }
}
//Train1.java
import java.util.Scanner;
public class Train1 {
    public static void main(String[] args){
        System.out.println(" ==== 构建圆对象并计算其面积周长 ====");
        try{
            Circle aCircle;
            double radius;
            String str;
            Scanner scan = new Scanner(System.in);
            while(true){    
                System.out.println("请输入半径(直接按回车键结束程序):");
                str = scan.nextLine();
                if (str.equals("")) { break; }
                radius = Double.parseDouble(str);
                aCircle = new Circle(radius);
                System.out.printf("构建了半径为%.2f的圆,圆面积%.2f、周长%.2f\n",
                        aCircle.getRadius(), aCircle.calcArea(), aCircle.calcGirth());
                System.out.printf("目前圆个数为 %d\n",Circle.getNum());
            }
            scan.close();
        }
        catch(Exception e){ System.out.println("异常:" + e); }
        finally { System.out.print("——程序结束。"); }
    }
}

2)编程,定义矩形类,构建若干个矩形对象,输出它们的面积、周长和总个数。

import java.util.Scanner;
class Rectangle{        //矩形类
    private double length;
    private double width;
    private static int num;    //矩形对象个数
    public Rectangle(){
        num ++; 
    }
    public Rectangle(double length, double width){
        this.length = length;
        this.width = width;
        num ++; 
    }
    public double calcArea(){   //计算面积
        return length * width;
    }
    public double calcGirth(){  //计算周长
        return (length + width) * 2;
    }
    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 int getNum(){ return num; }
}
public class Train2 {
    public static void main(String[] args){
        System.out.println(" ==== 构建矩形对象并计算其面积周长 ====");
        try{
            Rectangle aRectangle;
            double length, width;
            String str;
            Scanner scan = new Scanner(System.in);
            while(true){    
                System.out.println("请输入长度(直接按回车键结束程序):");
                str = scan.nextLine();
                if (str.equals("")) { break; }
                length = Double.parseDouble(str);
                System.out.println("请输入宽度:");
                str = scan.nextLine();
                width = Double.parseDouble(str);
                aRectangle = new Rectangle(length, width);
                System.out.printf("构建了长%.2f、宽%.2f的矩形,其面积%.2f、周长%.2f\n",
                        aRectangle.getLength(), aRectangle.getWidth(), aRectangle.calcArea(), aRectangle.calcGirth());
                System.out.printf("目前矩形个数为 %d\n",aRectangle.getNum());
            }
            scan.close();
        }
        catch(Exception e){ System.out.println("异常:" + e); }
        finally { System.out.print("——程序结束。"); }
    }
}

侵权联系删除


目录
打赏
0
0
0
0
0
分享
相关文章
|
6月前
ThreeJs创建球体
这篇文章讲解了如何在Three.js中创建并添加一个球体到场景中,包括定义球体的几何形状、赋予材质以及最终渲染球体的过程。
123 1
在圆的外面画一个正方形:Python实现与技术解析
本文介绍了如何使用Python的`matplotlib`库绘制一个圆,并在其外部绘制一个正方形。通过计算正方形的边长和顶点坐标,实现了圆和正方形的精确对齐。代码示例详细展示了绘制过程,适合初学者学习和实践。
105 9
|
6月前
ThreeJs创建圆环
这篇文章介绍了如何在Three.js中创建一个圆环(torus),包括设置圆环的几何形状、材质以及将其添加到场景中的具体步骤。
109 0
ThreeJs创建圆环
ThreeJS入门-创建一个正方体
这篇文章介绍了如何使用Three.js创建一个基本的正方体模型,并提供了实现这一功能的详细步骤和代码示例。
97 0
ThreeJS入门-创建一个正方体
|
8月前
|
API
【threejs教程】threejs中的边边角角:几何体详解
【8月更文挑战第6天】threejs中的几何体详解
194 4
|
8月前
|
C++
C++代码来计算一个点围绕另一个点旋转45度后的坐标
C++代码来计算一个点围绕另一个点旋转45度后的坐标
167 0
程序员必知:地图坐标转换器
程序员必知:地图坐标转换器
84 0
C# | [极坐标] 与 [平面直角系坐标] 的相互转换
极坐标和平面直角系坐标是常见的坐标系统,它们在不同的应用场景中都有重要的作用。而在计算机图形学、物理模拟和机器人控制等领域,我们经常需要在极坐标和平面直角系坐标之间进行转换。
256 2
C# | [极坐标] 与 [平面直角系坐标] 的相互转换
【python数据分析】绘制双Y轴坐标系
前言 Matplotlib绘制出的图形中会存在一些问题,例如:如何绘制双Y轴坐标系?如何去掉图形默认的边框?以及如何移动坐标到指定位置?下面我们就来看看如何解决
【python数据分析】绘制双Y轴坐标系