今天给各位分享javapoint类和line类的知识,其中也会对java编写point类进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
1、Java 定义一个Line类。该类包含两个Point型的实例变量。用以表示一条线段的两个端点。2、麻烦给我讲解一下point类和line类啊!举个简单的例子,我是学C++的,初学者……谢谢啦~3、关于JAVA的point的题 急!!!!!4、定义点Point类作为基类,在此基础上派生出直线Line类和圆Circle类,在Circle类的基础上派生出Cylinder类。5、java 用Point构造直线的问题.6、定义point类和line类表示点和线,定义setpoint类,包含两个line类成员和一个表示直线交点的point成员,
Java 定义一个Line类。该类包含两个Point型的实例变量。用以表示一条线段的两个端点。
package hello;
class Point {
double x, y;
Point(double x, double y) {
this.x = x;
this.y = y;
}
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Point)) return false;
Point p = (Point) o;
return x == p.x y == p.y;
}
public String toString() { return “(” + x + “, ” + y + “)”; }
}
class Line {
Point p1, p2;
Line(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}
double length() {
double dx = p1.x – p2.x, dy = p1.y – p2.y;
return Math.sqrt(dx * dx + dy * dy);
}
boolean isHorizontal() { return p1.y == p2.y; }
boolean isVertical() { return p1.x == p2.x; }
double slope() { return (p1.y – p2.y) / (p1.x – p2.x); }
Point midPoint() { return new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2); }
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Line)) return false;
Line line = (Line) o;
return (p1.equals(line.p1) p2.equals(line.p2)) ||
(p1.equals(line.p2) p2.equals(line.p1));
}
}
public class newbeett {
public static void main(String[] args) {
Point p1 = new Point(1, 2), p2 = new Point(3, 4);
Line line = new Line(p1, p2);
System.out.println(“线段长度为:”);
System.out.println(line.length());
System.out.println(“是否为水平线段:”);
System.out.println(line.isHorizontal());
System.out.println(“是否为垂直线段:”);
System.out.println(line.isVertical());
System.out.println(“线段斜率为:”);
System.out.println(line.slope());
System.out.println(“线段中点为:”);
System.out.println(line.midPoint());
System.out.println(“俩条线段是否相等:”);
System.out.println(line.equals(line));
}
}
麻烦给我讲解一下point类和line类啊!举个简单的例子,我是学C++的,初学者……谢谢啦~
//编写一个程序,测试输入的长度能否构成一个三角形
/*#includeiostream.h
class Line
{
int len;
public:
Line(int n){len=n;}
Line operator +(Line l)
{
int x=len+l.len ;
return Line(x);
}
bool operator (Line l)
{
return(lenl.len) ? 1:0;
}
};
void main()
{
Line a(10),b(5),c(14);
if(a+bca+cbb+ca)
cout”能够成一个三角形”endl;
else
cout”不能够成一个三角形”endl;
}*/
//重载逗号运算符
/*#includeiostream.h
#includemalloc.h
class Point
{
int x,y;
public:
Point(){};
Point(int l,int w){x=l;y=w;}
void disp(){cout”面积”x*yendl;}
Point operator,(Point r)
{
Point temp;
temp.x=r.x;
temp.y=r.y;
return temp;
}
Point operator+(Point r)
{
Point temp;
temp.x=r.x;
temp.y=r.y;
return temp;
}
};
void main()
{
Point r1(3,3),r2(5,8),r3(2,4);
r1.disp();
r2.disp();
r3.disp();
r1=(r1,r2+r3,r3);
r1.disp();
}*/
你自己看一下书呗,point和line只不过是类的名称而已,你也没有说这两个类之间的关系,让其实现什么样的功能,你先把类的一章学会吧
关于JAVA的point的题 急!!!!!
public class Point {�
private double x, y;�
public point(){}//当你在类中写出有参构造之后,系统不会再分配一个无参构造给该类。
public Point(double a, double b) {
setPoint(a, b);
}�
public void setPoint(double a, double b) {�
x=a;�
y=b;�
}�
public double getX() {return x;}
public double getY() {return y;}�
}�
�
public class Line extends Point {�
private double x, y, endX, endY;�
public Line(){}
public Line(double x1, double y1, double x2, double y2) {
super(x1,y1);//调用父类构造,若不写,子类调用的是父类的默认构造
public setLine(x1, y1, x2, y2);
}�
public void setLine(double x1, double y1, double x2, double y2) {�
x=x1;�
y=y1;�
endX=x2;�
endY=y2;�
}�
public double getX() {return x ;}�
public double getY() {return y ;}�
public double getEndX() {return endX;}�
public double getEndY() {return endY;}�
public double length() {�
return Math.sqrt((endX-x) * (endX-x) + (endY-y) * (endY-y));
}�
}
public class Circle extends Point {�
private double radius; // 子类受保护的成员变量,代表圆的半径�
public Circle(double radius){
} // 圆的构造方法�
public Circle(){
}
public void setRadius(double radius){
this.radius = radius;
} // 设定半径值的方法�
public double getRadius(){
return radius;
} // 返回半径值的方法�
public double area(){
return 3.14*radius*radius
} // 返回圆面积的方法�
}
改动有如下几点:int全部改为double
添加默认的构造,因为在子类中不写调用父类的构造,系统也会先去调用父类的默认构造。
为了良好的封装性,属性的修饰符都为private
若有问题,可以联系我。
定义点Point类作为基类,在此基础上派生出直线Line类和圆Circle类,在Circle类的基础上派生出Cylinder类。
是.Net的么?还是java的啊?如果是这两门语言的问题 我都能给你解决去 嘻嘻
java 用Point构造直线的问题.
public Line() 为系统默认构造函数,可以不写;
但如果该类为另一个类的基类,同时还用到有参构造函数时,如public Line(Point p1, Point p2)和public Line(int x1, int y1, int x2, int y2),则必须有public Line()构造函数。
public Line(Point p) 的作用是当收到一个Point类参数时初始化Line类。
定义point类和line类表示点和线,定义setpoint类,包含两个line类成员和一个表示直线交点的point成员,
#include iostream
#include cmath
using namespace std;
class Point //Point类声明
{
public:
Point(int xx=0, int yy=0)
Point(Point p);
int GetX()
int GetY()
private:
int X,Y;
};
Point::Point(Point p) //拷贝构造函数的实现
{
X=p.X;
Y=p.Y;
cout”Point拷贝构造函数被调用”endl;
}
//类的组合
class Line //Line类的声明
{
public: //外部接口
Line (Point xp1, Point xp2);
Line (Line );
double GetLen()
private: //私有数据成员
Point p1,p2; //Point类的对象p1,p2
double len;
};
//组合类的构造函数
Line:: Line (Point xp1, Point xp2)
:p1(xp1),p2(xp2)
{
cout”Line构造函数被调用”endl;
double x=double(p1.GetX()-p2.GetX());
double y=double(p1.GetY()-p2.GetY());
len=sqrt(x*x+y*y);
}
//组合类的拷贝构造函数
Line:: Line (Line Seg): p1(Seg.p1), p2(Seg.p2)
{
cout”Line拷贝构造函数被调用”endl;
len=Seg.len;
}
//主函数
int main()
{
Point myp1(1,1),myp2(4,5); //建立Point类的对象
Line line(myp1,myp2); //建立Line类的对象
Line line2(line); //利用拷贝构造函数建立一个新对象
cout”The length of the line is:”;
coutline.GetLen()endl;
cout”The length of the line2 is:”;
coutline2.GetLen()endl;
}
javapoint类和line类的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java编写point类、javapoint类和line类的信息别忘了在本站进行查找喔。