/*
* Project: 顷饥 空间点
* Author: searchpcc(xixi,11级,20112866)
* File: TuXing.java
* Instruction: 主程序
* Time: 2012-11-07
*/
class Point2D{
int x
int y
public Point2D(){
}
public Point2D(int x, int y){
this.x = x
this.y = y
}
public void offset(int a, int b){
x = a
y = b
this.x = this.x + 1
this.y = this.y + 1
System.out.print(x)
System.out.print(y)
}
double distan(Point2D p1, Point2D p2){
return(Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)))
}
}
class Point3D extends Point2D{
int z
public Point3D(int x, int y, int z){
super(x, y)
this.z = z
}
public Point3D(){
}
public Point3D(Point2D p, int z){
super(p.x, p.y)
this.z = z
}
public void offset(int a, int b,int c){
x = a
y = b
z = c
this.x = this.x + 1
this.y = this.y + 1
this.z = this.z + 1
}
double distan(Point3D p1, Point3D p2){
return(Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)+(p1.z-p2.z)*(p1.z-p2.z)))
}
}
public class KongJian1{
public static void main(String[] args){
Point2D p1 = new Point2D(3,3)
p1.offset(3, 3)
Point2D p2 = new Point2D(2,2)
Point3D p3 = new Point3D(4,4,4)
p3.offset(4,2,3)//可以进行移动
Point3D p4 = new Point3D(5,5,5)
System.out.println("p1和p2之间的距离为:")
System.out.println(new Point2D().distan(p1,p2))
System.out.println("p3和p4间的距离为:")
System.out.println(new Point3D().distan(p3,p4))
}
}
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃世答了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强雀返返大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。
Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。
java编写二维坐标平移程序,主要是通过类继承Point2D,使用里面的方法来平移,如下代码:
class Point2D{
int x, y
Point2D(){ }
Point2D(int i,int j)
{
x=i
y=j
}
void offset(int a, int b)
{
x=x+a
y=y+b
}
void distance(Point2D a,Point2D b)
{
float m
含扒 m=(float)Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))
System.out.print("二维空间两点之间的距离:")
System.out.println("m="+m)
}
}
public class Point3D extends Point2D
{
int x,y,z
Point3D(int x,int y,int z)
{
this.x=x
this.y=y
this.z=z
渗老散 }
Point3D(Point2D p,int 丛氏z)
{
x=p.x
y=p.y
this.z=z
}
void offset(int a, int b,int c)
{
x=x+a
b=x+b
c=x+c
}
void distance(Point3D a,Point3D b)
{
float n
n=(float)Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.y-b.y)*(a.y-b.y))//计算两点之间的距离。
System.out.print("三维空间两点之间的距离:")
System.out.println("n="+n)
}
public static void main(String[] args)
{
Point2D p2d1=new Point2D(2,3)
Point2D p2d2=new Point2D(3,6)
Point3D p2d3=new Point3D(1,2,3)
Point3D p2d4=new Point3D(p2d1,3)
p2d1.distance(p2d1,p2d2)
p2d3.distance(p2d3,p2d4)//平移一段距离。
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)