急求一道JAVA题目的答案
(1)构造一个类来描述屏幕上的一个点,该类的构成包括点的x和y两个坐标,以及一些对点进行的操作,包括:取得点的坐标值,对点的坐标进行赋值,求两个点的距离。(2)生成第一题...
(1)构造一个类来描述屏幕上的一个点,该类的构成包括点的x和y两个坐标,以及一些对点进行的操作,包括:取得点的坐标值,对点的坐标进行赋值,求两个点的距离。
(2)生成第一题中的五个点,并将此五个点放在一个数组中,对此五个点按x坐标进行排序并输出。 展开
(2)生成第一题中的五个点,并将此五个点放在一个数组中,对此五个点按x坐标进行排序并输出。 展开
3个回答
展开全部
import java.util.Random;
public class Point {
private double x,y;
public Point() {
x=0;
y=0;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public static double getdis(Point p1,Point p2){
double x=p1.getX()-p2.getX();
double y=p1.getY()-p2.getY();
double len=Math.hypot(x, y);
return len;
}
public static void sort(Point[]p){
for(int i=0;i<p.length;i++){
int index=i;
for(int j=i;j<p.length;j++){
if(p[index].x>=p[j].x)
index=j;
}
if(index!=i){
Point temp=p[index];
p[index]=p[i];
p[i]=temp;
}
}
for(int i=0;i<p.length;i++){
System.out.println("p["+i+"]=("+p[i].x+","+p[i].y+")");
}
}
public static void main(String[]args){
Point p1=new Point(3,0);
Point p2=new Point(0,4);
System.out.println("p1与p2之间的距离为:"+Point.getdis(p1, p2));
Random random=new Random();
Point []p=new Point[5];
for(int i=0;i<p.length;i++){
p[i]=new Point();
p[i].setX(random.nextInt(100));
p[i].setY((random.nextInt(100)));
}
Point.sort(p);
}
}
有问题可以hi我或追问
public class Point {
private double x,y;
public Point() {
x=0;
y=0;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public static double getdis(Point p1,Point p2){
double x=p1.getX()-p2.getX();
double y=p1.getY()-p2.getY();
double len=Math.hypot(x, y);
return len;
}
public static void sort(Point[]p){
for(int i=0;i<p.length;i++){
int index=i;
for(int j=i;j<p.length;j++){
if(p[index].x>=p[j].x)
index=j;
}
if(index!=i){
Point temp=p[index];
p[index]=p[i];
p[i]=temp;
}
}
for(int i=0;i<p.length;i++){
System.out.println("p["+i+"]=("+p[i].x+","+p[i].y+")");
}
}
public static void main(String[]args){
Point p1=new Point(3,0);
Point p2=new Point(0,4);
System.out.println("p1与p2之间的距离为:"+Point.getdis(p1, p2));
Random random=new Random();
Point []p=new Point[5];
for(int i=0;i<p.length;i++){
p[i]=new Point();
p[i].setX(random.nextInt(100));
p[i].setY((random.nextInt(100)));
}
Point.sort(p);
}
}
有问题可以hi我或追问
展开全部
package k;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Zuobiao {
private int x;
private int y;
public Zuobiao(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public double juli(Zuobiao z) {
return Math.pow(Math.pow(z.getX() - x, 2) + Math.pow(z.getY() - y, 2), 0.5);
}
public static void main(String[] args) {
Zuobiao a = new Zuobiao(1, 1);
Zuobiao b = new Zuobiao(2, 1);
Zuobiao c = new Zuobiao(3, 1);
Zuobiao d = new Zuobiao(4, 1);
Zuobiao e = new Zuobiao(5, 1);
List<Zuobiao> list = new ArrayList<Zuobiao>();
list.add(a);
list.add(c);
list.add(d);
list.add(e);
list.add(b);
Collections.sort(list, new Comparator<Zuobiao>() {
public int compare(Zuobiao a, Zuobiao b) {
return (int) (a.getX() - b.getX());
}
});
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Zuobiao {
private int x;
private int y;
public Zuobiao(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public double juli(Zuobiao z) {
return Math.pow(Math.pow(z.getX() - x, 2) + Math.pow(z.getY() - y, 2), 0.5);
}
public static void main(String[] args) {
Zuobiao a = new Zuobiao(1, 1);
Zuobiao b = new Zuobiao(2, 1);
Zuobiao c = new Zuobiao(3, 1);
Zuobiao d = new Zuobiao(4, 1);
Zuobiao e = new Zuobiao(5, 1);
List<Zuobiao> list = new ArrayList<Zuobiao>();
list.add(a);
list.add(c);
list.add(d);
list.add(e);
list.add(b);
Collections.sort(list, new Comparator<Zuobiao>() {
public int compare(Zuobiao a, Zuobiao b) {
return (int) (a.getX() - b.getX());
}
});
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Point {
private int posX;
private int posY;
public Point() {
super();
}
public Point(int posX, int posY) {
super();
this.posX = posX;
this.posY = posY;
}
public int getPosX() {
return posX;
}
public void setPosX(int posX) {
this.posX = posX;
}
public int getPosY() {
return posY;
}
public void setPosY(int posY) {
this.posY = posY;
}
}
import java.util.ArrayList;
import java.util.List;
public class PointTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Point p1 = new Point(10,10);
Point p2 = new Point(20,20);
Point p3 = new Point(50,30);
Point p4 = new Point(30,40);
Point p5 = new Point(40,50);
List<Point> list = new ArrayList<Point>();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
maoPao(list);
}
// 冒泡排序
public static void maoPao(List<Point> list) {
for(int i=0;i<list.size();i++){
for(int j=i+1;j<list.size();j++){
if(list.get(i).getPosX()>list.get(j).getPosX()){
Point p = list.get(i);
list.set(i, list.get(j));
list.set(j, p);
}
}
}
for(Point p:list){
System.out.println("x:"+p.getPosX()+"===y:"+p.getPosY());
}
}
}
private int posX;
private int posY;
public Point() {
super();
}
public Point(int posX, int posY) {
super();
this.posX = posX;
this.posY = posY;
}
public int getPosX() {
return posX;
}
public void setPosX(int posX) {
this.posX = posX;
}
public int getPosY() {
return posY;
}
public void setPosY(int posY) {
this.posY = posY;
}
}
import java.util.ArrayList;
import java.util.List;
public class PointTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Point p1 = new Point(10,10);
Point p2 = new Point(20,20);
Point p3 = new Point(50,30);
Point p4 = new Point(30,40);
Point p5 = new Point(40,50);
List<Point> list = new ArrayList<Point>();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
maoPao(list);
}
// 冒泡排序
public static void maoPao(List<Point> list) {
for(int i=0;i<list.size();i++){
for(int j=i+1;j<list.size();j++){
if(list.get(i).getPosX()>list.get(j).getPosX()){
Point p = list.get(i);
list.set(i, list.get(j));
list.set(j, p);
}
}
}
for(Point p:list){
System.out.println("x:"+p.getPosX()+"===y:"+p.getPosY());
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询