JAVA面向对象题。
创建一个圆Circle类,为该类提供两个方法,方法一用于求圆的面积,方法二用于求圆的周长,同时为该类提供一个变量r表示半径,一个常量PI表示圆周率。为该类提供一个无参的构...
创建一个圆Circle类,为该类提供两个方法,方法一用于求圆的面积,方法二用于求圆的周长,同时为该类提供一个变量r表示半径,一个常量PI表示圆周率。为该类提供一个无参的构造方法,用于初始化r的值为1;且提供一个有参的构造方法,参数用于初始化半径r的值
展开
5个回答
推荐于2018-03-23
展开全部
这个是我刚刚写的,可以直接运行的。应该能够满足你的需求吧。
public class Circle {
private float r;
private final static double PI = Math.PI;
Circle()
{
this.r = 1;
}
Circle(float radius)
{
this.r = radius;
}
private double getPerimeter()
{
return PI*2*r;
}
private double getArea()
{
return PI*r*r;
}
private double getRadius()
{
return r;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle myCircle = new Circle(3);
System.out.println("Radius is "+myCircle.getRadius()+" Perimeter is "+myCircle.getPerimeter()+" Area is "+myCircle.getArea());
}
}
public class Circle {
private float r;
private final static double PI = Math.PI;
Circle()
{
this.r = 1;
}
Circle(float radius)
{
this.r = radius;
}
private double getPerimeter()
{
return PI*2*r;
}
private double getArea()
{
return PI*r*r;
}
private double getRadius()
{
return r;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle myCircle = new Circle(3);
System.out.println("Radius is "+myCircle.getRadius()+" Perimeter is "+myCircle.getPerimeter()+" Area is "+myCircle.getArea());
}
}
展开全部
public class Circle {
private static final double PI=3.14;//常量PI
private int r;//半径
//无参构造器
public Circle() {
this.r=1;
}
//有参构造器
public Circle(int r) {
this.r = r;
}
//计算周长
public double getPerimeter(int r) {
double perimeter = 0;
perimeter = 2 * PI * r;
return perimeter;
}
//计算面积
public double getArea(int r) {
double area = 0;
area = PI * r * r;
return area;
}
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
}
希望对你有帮助~
private static final double PI=3.14;//常量PI
private int r;//半径
//无参构造器
public Circle() {
this.r=1;
}
//有参构造器
public Circle(int r) {
this.r = r;
}
//计算周长
public double getPerimeter(int r) {
double perimeter = 0;
perimeter = 2 * PI * r;
return perimeter;
}
//计算面积
public double getArea(int r) {
double area = 0;
area = PI * r * r;
return area;
}
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
}
希望对你有帮助~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Rectangle
{
private double width;
private double height;
//无参构造器
public Rectangle()
{}
//有参构造器
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
//属性的get和set方法定义
public void setWidth(double width)
{
this.width = width;
}
public double getWidth()
{
return this.width;
}
public void setHeight(double height)
{
this.height = height;
}
public double getHeight()
{
return this.height;
}
//计算周长的方法
private double getPerimeter()
{
return (width+height)*2;
}
//计算面积的方法
private double getArea()
{
return width*height;
}
public static void main(String[] args)
{
Rectangle rec = new Rectangle(3.6,5.8);
System.out.println("The perimeter of Rectangle is:"+rec.getPerimeter());
System.out.println("The area of Rectangle is:"+rec.getArea());
}
}
{
private double width;
private double height;
//无参构造器
public Rectangle()
{}
//有参构造器
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
//属性的get和set方法定义
public void setWidth(double width)
{
this.width = width;
}
public double getWidth()
{
return this.width;
}
public void setHeight(double height)
{
this.height = height;
}
public double getHeight()
{
return this.height;
}
//计算周长的方法
private double getPerimeter()
{
return (width+height)*2;
}
//计算面积的方法
private double getArea()
{
return width*height;
}
public static void main(String[] args)
{
Rectangle rec = new Rectangle(3.6,5.8);
System.out.println("The perimeter of Rectangle is:"+rec.getPerimeter());
System.out.println("The area of Rectangle is:"+rec.getArea());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Circle{
double area,round;
double r;
final double PI=Math.PI;
public Circle(){
r=1.0;
}
public Circle(double i){
this.r=i;
}
public double getArea(double r){
area=PI*r*r;
return area;
}
public double getRound(double r){
round=PI*r*2;
return round;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Circle{
private int r=1;
private static final double PI=3.1415;
public Circle(){}
public Circle(int r){this.r=r}
double Perimeter(){return 2*PI*r;}
double Area(){return PI*r^2}
public static void main(string[] args){
System.out.println("半径55的圆面积"+(new Circle(55).Area())+"\n半径66的圆周长"+(new Circle(33).Perimeter()));
}
private int r=1;
private static final double PI=3.1415;
public Circle(){}
public Circle(int r){this.r=r}
double Perimeter(){return 2*PI*r;}
double Area(){return PI*r^2}
public static void main(string[] args){
System.out.println("半径55的圆面积"+(new Circle(55).Area())+"\n半径66的圆周长"+(new Circle(33).Perimeter()));
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询