java生成circle对象,并调用draw方法:class Circle{double x,y,rpublic Circle(){this.x=0this.y=0this.r=0}。
使用静态方法创建对象public static Circle createInstance(){createCount++return new Circle()}使用静态变量保存创建次数private static int createCountpublic int getCreateCount(){return createCount}。
语言规范:
JCP维护的规范包括Java ME、Java SE、Java EE、XML、OSS、JAIN等。组织成员可以提交JSR(Java Specification Requests),通过特定程序以后,进入到下一版本的规范里面。
JSR是早期提议和最终发布的Java平台规范的具体描述。通常,一个新的JSR的提出是为了增加或者规范Java平台的功能。某个具体的JSR由专家组共同来制定,工作由组长协调。
例如,CLDC1.0(Connected Limited Device Configuration,JSR30)由Sun公司的Antero Taivalsaari担任组长,同时专家组的成员还包括Siemens、Motorola、Nokia、Symbian等。Java ME平台规范是在JSR68中定义的,规范组长是Sun公司的Jon Courtney。
按照题目要求编写的圆,圆锥和测试类的Java程序如下
Test.java文件内容如下
class Circle{
private double r
private String color
public Circle(double r){
this.r=r
}
public double area(){
return Math.PI*r*r
}
public double perimeter(){
return Math.PI*2*r
}
public double getR(){
return this.r
}
public void setR(double r){
this.r=r
}
public String getColor(){
return this.color
}
public void setColor(String color){
this.color=color
}
public String toString(){
return "圆的半径为"+r+",颜色为"+color
}
}
class Cone{
private Circle c
private double h
private String color
public Cone(Circle c,double h){
this.c=c
this.h=h
}
public double volume(){
return 1.0/3*c.area()*h
}
public Circle getCircle(){
return this.c
}
public void setCircle(Circle c){
this.c=c
}
public double getH(){
return this.h
}
public void setH(double h){
this.h=h
}
public String getColor(){
return this.color
}
public void setColor(String color){
this.color=color
}
public String toString(){
return "圆锥的底面积为"+c.area()+",高为"+h+",颜色为"+color
}
}
public class Test{
public static void main(String[] args){
Circle circle1=new Circle(2.5)
circle1.setColor("红色")
System.out.println(circle1.toString())
System.out.println("圆的面积为"+circle1.area())
System.out.println("圆的周长为"+circle1.perimeter())
Cone circlar1=new Cone(circle1,2.7)
circlar1.setColor("蓝色")
System.out.println(circlar1.toString())
System.out.println("圆锥的体积为"+circlar1.volume())
}
}
import java.awt.Pointpublic class Circle{
private Point centerPoint
private int r
public Circle(){}
public Circle(Point p,int r){
this.r=r
this.centerPoint=p
}
public int getR(){return this.r}
public void setR(int r){this.r=r}
public Point getCenPoint(){return this.centerPoint}
public void setcenterPoint(Point p){this.centerPoint=p}
public boolean isInside(Point point){
double ar=Math.sqrt((point.x-centerPoint.x)*(point.x-centerPoint.x)+(point.y-centerPoint.y)*(point.y-centerPoint.y))
if(ar>=this.r){
return false
}else{
return true
}
}
}
public class test{
public static void main(){
Circle aCircle=new Circle(new Point(0,0),10)
Point p1=new Point(5,5)
Point p2=new Point(15,0)
if(aCircle.isInside(p1)){
System.out.println("p1在圆的内部")
}else{
System.out.println("p1不在圆的内部")
}
Point p2=new Point(15,0)
if(aCircle.isInside(p2)){
System.out.println("p2在圆的内部")
}else{
System.out.println("p2不在圆的内部")
}
}
}
如果你愿意,自己可以把圆的参数都改成float,或者double。这事是简单的一个例子
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)