A. java 定義一個圓類Circle,就解
publicclassCircle{
//圓的半徑
privatedoubleradius;
//求圓的面積
publicdoublefindArea(){
returnMath.PI*radius*radius;
}
publicdoublegetRadius(){
returnradius;
}
publicvoidsetRadius(doubleradius){
this.radius=radius;
}
}
測試類自己去寫。
B. JAVA中怎麼定義一個圓類 要求如下
public class Circle {
private double r;
public Circle(double r) {
this.r = r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
/**
* 面積
*
* @return
*/
public double getArea() {
return Math.PI * Math.pow(r, 2);
}
/**
* 周長
*
* @return
*/
public double getLong() {
return Math.PI * r * 2;
}
}class Cylinder extends Circle {
private double h;
public Cylinder(double r, double h) {
super(r);
this.h = h;
}
public double getH() {
return h;
}
public void setH(double h) {
this.h = h;
}
/**
* 體積 = 底面積*高
*
* @return
*/
public double getVolume() {
return super.getArea() * h;
}
/***
* 表面積=測面積+2個底面積
*
* @return
*/
public double getSurfacearea() {
return super.getLong() * h + 2 * super.getArea();
}
}
測試代碼就不寫了
C. JAVA定義圓形類。
public class Circle{
private double r;
public void setR(double r){
this.r=r;
}
public double getArea(){
return Math.PI*r*r;
}
public double getPerimeter(){
return 2*Math.PI*r;
}
}
public class TestCircle{
public static void main(String [] args){
//兩個不同對象
Circle circle1 = new Circle();
Circle circle2 = new Circle();
//設置不同半徑
circle1.setR(2);
circle2.setR(3);
//顯示結果
system.out.println("半徑為2的圓面積是"+circle1.getArea()+", 周長是"+circle1.getPerimeter());
system.out.println("半徑為3的圓面積是"+circle2.getArea()+", 周長是"+circle2.getPerimeter());
}
}
D. 急求Java代碼,定義一個Circle(圓類型)
public class Exam
{
public static void main(String[] args)
{
Circle c=new Circle(3,4,5);
System.out.printf("圓心:(%f,%f),半徑:%f,面積:%f",c.x,c.y,c.r,c.countArea());
}
}
class Circle
{
public Circle()
{
this(0,0,0);
}
public Circle(double x,double y,double r)
{
this.x=x;
this.y=y;
this.r=r;
}
public double countArea()
{
return Math.PI*r*r;
}
/*private*/public double x,y,r;
}
E. java編程:定義一個圓circle類,包含如下內容
代碼如下:
importjava.util.Scanner;
classCircle{
privatedoubleradius;
publicCircle(doubleradius){
this.radius=radius;
}
publicdoublegetRadius(){
returnradius;
}
publicvoidsetRadius(doubleradius){
this.radius=radius;
}
publicdoublegetArea(){
returnMath.PI*radius*radius;
}
publicdoublegetPerimeter(){
return2*Math.PI*radius;
}
}
publicclassApp{
publicstaticvoidmain(String[]argv){
Scannerscanner=newScanner(System.in);
System.out.print("請輸入圓半徑:");
doubler=scanner.nextDouble();
Circlec=newCircle(r);
System.out.println("面積:"+c.getArea()+",周長:"+c.getPerimeter());
}
}
運行結果: