日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

求圆形矩形面积java_Java求矩形面积和圆形面积的异常处理实例

發布時間:2023/12/20 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 求圆形矩形面积java_Java求矩形面积和圆形面积的异常处理实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目

有一個圓形和長方形。

都可以獲取面積,對于如果如果出現非法值,視為是獲取面積出現問題。

問題通過異常來表示。

現在對這個程序進行基本設計

代碼如下

package com.liuyanzhao;

class NoValueException extends RuntimeException {

NoValueException (String message) {

super(message);

}

}

interface Shape{

void getArea();

}

class Rec implements Shape {

private int len,wid;

Rec(int len,int wid) {

if(len<=0 || wid<=0) {

throw new NoValueException("長寬存在非法值");

}

this.len = len;

this.wid = wid;

}

public void getArea() {

System.out.println("長方形面積:"+len*wid);

}

}

class Circle implements Shape {

private int radius;

public static final double PI = 3.14;

Circle(int radius) {

if(radius<=0) {

throw new NoValueException("半徑長度非法");

}

this.radius = radius;

}

public void getArea() {

System.out.println("圓形面積:"+radius*radius*PI);

}

}

public class ExceptionDemo1 {

public static void main(String[] args) {

Rec r = new Rec(3, 4);

r.getArea();

Circle c = new Circle(-8);

c.getArea();

}

}

運行結果:

本文永久更新地址:http://liuyanzhao.com/3356.html

轉載請注明

總結

以上是生活随笔為你收集整理的求圆形矩形面积java_Java求矩形面积和圆形面积的异常处理实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。