
供参考
public class Circle {public Circle(double cx, double cy, double r) {
centerX = cx;
centerY = cy;
radius = r;
}
public Circle() {
this(0, 0, 0);
}
public void setCenter(double cx, double cy) {
centerX = cx;
centerY = cy;
}
public void setRadius(double r) {
radius = r;
}
static enum PosRelation {INSIDE_CIRCLE, ON_CIRCLE, OUTSIDE_CIRCLE };
PosRelation judgePosRelation(double pointX, double pointY) {
double d = (pointX - centerX) (pointX - centerX) + (pointY - centerY) (pointY - centerY);
return d < radius radius PosRelationINSIDE_CIRCLE :
(d > radius radius PosRelationOUTSIDE_CIRCLE :
PosRelationON_CIRCLE);
}
private double centerX, centerY, radius;
public static void main(String []args) {
Circle c = new Circle(4, 5, 6);
PosRelation relation = cjudgePosRelation(9, 9);
Systemoutprintln("This Point:(9,9) is " + relationtoString());
}
}
不怎么明白题意,随便写一个
public interface ShapePara {
public int getArea();//获得图形的面积
public int getCircumference();//获得图形的周长
}
public class Circle implements ShapePara {
public double radius;//圆的半径
private double x;//圆心的横坐标
protected double y;//圆心的纵坐标
public Circle(double radius) {
super();
thissetRadius(radius);
thissetCenter(000, 000);
}
@Override
public int getArea() {
return (int) (MathPIMathpow(radius, 2));
}
@Override
public int getCircumference() {
return (int) (MathPI2radius);
}
//获取半径
public double getRadius(){
return thisradius;
}
//设置圆心坐标
public void setCenter(double x, double y){
thisx = x;
thisy = y;
}
//设置radius域
public void setRadius(double radius){
thisradius = radius;
}
public static void main(String[] args) {
Circle circle = new Circle(500);
}
}
分类: 电脑/网络 >> 程序设计 >> 其他编程语言
问题描述:
1 定义一个“圆”类,该圆类的数据成员包括:圆心点位置及圆的半径;方法成员有:设置圆心位置、获取圆的圆心位置及构造方法。要求构造方法可以接收圆心位置参数,而半径使用缺省值1
2 定义以上圆的子类,使它具有获取半径方法、设置半径方法和构造方法,要求构造方法可同时接收圆心位置及半径两个参数
3 编写完整的程序实现上述两个圆类的对象,并且分别调用各种方法,对比这些方法的执行结果
解析:
package mycirclesubCircle; 创建包
import javaio;
public class CircleClass
{
public static void main(String args[])
{
myCircle cir1=new myCircle(10,10);
mySubCircle subCir=new mySubCircle(20,20,20);
Systemoutprintln("父类中坐标为:" + cir1getX() + "和" + cir1getY());
subCirsetR(120);
Systemoutprintln("子类中" + subCirtoString());
}
}
class myCircle
{
float x;
float y;
float r;
myCircle()
{
}
/重载构造函数/
myCircle(float px,float py)
{
x=px;
y=py; 设置圆心位置
r=1; 设置默认半径为1
}
float getY()
{
return y;
}
float getX()
{
return x;
}
public String toString()
{
String s="位置为:" + x + "," + y
+"半径为:" + r;
return s;
}
}
class mySubCircle extends myCircle
{
mySubCircle(float px,float py,float pr)
{
x=px;
y=py;
r=pr;
}
void setR(float pr)
{
thisr=pr;
}
float getR()
{
return thisr;
}
public String toString()
{
String s="位置为:" + x + "," + y
+"半径为:" + r;
return s;
}
}
运行成功,运行环境JDK+WinXP
//圆心坐标在此题中无用,是否是需求有问题。
public class Circle {
double x;
double y;
double r;
double area;
double length;
public static void main(String[] args) {
Circle c = new Circle();
csetR(2);
cgetArea();
cgetLength();
csetXY(0, 0);
}
void setXY(double x, double y) {
thisx = x;
thisy = y;
}
void setR(double r) {
thisr = r;
}
void getArea() {
area = javalangMathPI r r;
}
void getLength() {
length = 2 javalangMathPI r;
}
}
public class Coordinate {
public static void main(String[] args) {
double x = 05;//测试点横坐标
double y = 03;//测试点纵坐标
final double r = 1;//半径为1
boolean isInCircle = isPointInCircle(x, y, r);
Systemoutprintln("点(" + x + "," + y + ")" + (isInCircle "在": "不在") + "以原点(0,0)为圆心半径为" + r + "的圆内!");
}
private static boolean isPointInCircle(double x, double y, double r) {
//园内的点必须满足x < 1 && y <1并且xx + yy < 1
if(Mathabs(x) >= r || Mathabs(y) >= r){
return false;
}
return Mathpow(x, 2) + Mathpow(y, 2) < 1;
}
}
------------------
点(05,03)在以原点(0,0)为圆心半径为10的圆内!
import javautilArrays;
import javautilDate;
public class Test {
@SuppressWarnings("deprecation")
public static void main(String args[]) {
Systemoutprintln("problem 1:");
Date[] dates = {
new Date(109, 5, 5),
new Date(105, 1, 1),
new Date(108, 4, 4),
new Date(106, 2, 2),
new Date(107, 3, 3),
};
Arrayssort(dates);
for (Date date : dates) {
Systemoutprintln(date);
}
Systemoutprintln("problem 2:");
Circle circle1 = new Circle(new Point(1, 1), 1);
Systemoutprintln(circle1);
Circle circle2 = new Circle(2);
Systemoutprintln(circle2);
circle2setCenter(new Point(2, 2));
Systemoutprintln(circle2);
circle1setRadius(3);
Systemoutprintln(circle1);
}
}
class Point {
private double x;
private double y;
Point(double x, double y) {
thisx = x;
thisy = y;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
}
class Circle {
private Point center;
private double radius;
public Circle(Point center, double radius) {
thiscenter = center;
thisradius = Mathmax(radius, 0);
}
public Circle(double radius) {
this(new Point(0, 0), radius);
}
public double getArea() {
return MathPI radius radius;
}
public void setCenter(Point center) {
thiscenter = center;
}
public void setRadius(double radius) {
thisradius = Mathmax(radius, 0);
}
public String toString() {
return center + " radius: " + radius
+ " area: " + getArea();
}
}
点类:
圆类:
测试类:
结果:(如果要保留小数点,可以不用toString()方法输出,将圆类属性用getter方法获得,并将各个值格式化后输出即可)
以上就是关于java定义一个圆类用来表示二维空间中的一个圆,包括了圆心坐标和半径全部的内容,包括:java定义一个圆类用来表示二维空间中的一个圆,包括了圆心坐标和半径、有道java的编程题,求各位路过的大神解答一下,感激涕零≥﹏≤ 1.(1)编写一个接口ShapeP、JAVA定义一个圆获取半径类等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)