java 定义一个圆类Circle 包含私有成员r,voidsetR(double)设置圆的半径,doublearea()计算圆的面积
展开全部
给你个参考的,用到了包,还有输入输出流,自己参考一下啊
package SimpleInput;
import java.io.*;
public class SimpleInput
{
public static String readString()
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1);
String String = "";
try
{
String = br.readLine();
}
catch (IOException ex)
{
System.out.println(ex);
}
return String;
}
public static double readDouble()
{
return Double.parseDouble(readString());
}
}
import SimpleInput.*;
/*********************************************************\
本类为圆类比较完善的定义
\*********************************************************/
class Circle
{
private double radius;
private double circumference;
private double circle;
public Circle(double r)
{
radius = r;
}
public void CircumferenceCal()
{
circumference = 2*Math.PI*radius;
}
public void CircleCal()
{
circle = Math.PI*radius*radius;
}
public String toString()
{
return "圆的半径:"+radius+'\n'
+ "圆的周长:"+circumference+'\n'
+ "圆的面积:"+circle;
}
}
/*********************************************************\
在自己的程序中进行异常处理
\*********************************************************/
class MyException extends Exception
{
public String Show(double r)
{
if (r < 0.0)
{
return ("圆的半径的值在此不能负值"+r);
}
else
{
return "";
}
}
}
public class Main
{
public static double r= 0.0;
static void Test()throws MyException
{
MyException me = new MyException();
if (r < 0.0)
{
throw me;
}
}
public static void main(String[] args)
{
System.out.print("please iniput the value of radius:");
try
{
r = SimpleInput.readDouble();
Test();
Circle c = new Circle(r);
c.CircumferenceCal();
c.CircleCal();
System.out.println(c);
}
/*********************************************************************\
此处参考了书本上是异常处理常用调试方法一节中的部分内容
\*********************************************************************/
catch(MyException me)
{
System.out.println(me.Show(r));
System.out.println(me.getMessage());
me.printStackTrace();
System.out.println("异常类型:"+me.getClass().getName());
}
}
}
package SimpleInput;
import java.io.*;
public class SimpleInput
{
public static String readString()
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1);
String String = "";
try
{
String = br.readLine();
}
catch (IOException ex)
{
System.out.println(ex);
}
return String;
}
public static double readDouble()
{
return Double.parseDouble(readString());
}
}
import SimpleInput.*;
/*********************************************************\
本类为圆类比较完善的定义
\*********************************************************/
class Circle
{
private double radius;
private double circumference;
private double circle;
public Circle(double r)
{
radius = r;
}
public void CircumferenceCal()
{
circumference = 2*Math.PI*radius;
}
public void CircleCal()
{
circle = Math.PI*radius*radius;
}
public String toString()
{
return "圆的半径:"+radius+'\n'
+ "圆的周长:"+circumference+'\n'
+ "圆的面积:"+circle;
}
}
/*********************************************************\
在自己的程序中进行异常处理
\*********************************************************/
class MyException extends Exception
{
public String Show(double r)
{
if (r < 0.0)
{
return ("圆的半径的值在此不能负值"+r);
}
else
{
return "";
}
}
}
public class Main
{
public static double r= 0.0;
static void Test()throws MyException
{
MyException me = new MyException();
if (r < 0.0)
{
throw me;
}
}
public static void main(String[] args)
{
System.out.print("please iniput the value of radius:");
try
{
r = SimpleInput.readDouble();
Test();
Circle c = new Circle(r);
c.CircumferenceCal();
c.CircleCal();
System.out.println(c);
}
/*********************************************************************\
此处参考了书本上是异常处理常用调试方法一节中的部分内容
\*********************************************************************/
catch(MyException me)
{
System.out.println(me.Show(r));
System.out.println(me.getMessage());
me.printStackTrace();
System.out.println("异常类型:"+me.getClass().getName());
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询