/*接收computer类, Computer b=new Computer();声明对象, Scanner input=new Scanner(System.in);
int n=input.nextInt();用户输入数值为n赋值, System.out.println("结果为:"+b.getA(n));输出b的值*/
package qqq;
import java.util.*;
import qqq.Computer;
public class aaa {
public static void main(String args[]){
System.out.println("输入一个数字");
Computer b=new Computer();
Scanner input=new Scanner(System.in);
int n=input.nextInt();
System.out.println("结果为:"+b.getA(n));
}
}
题目2:
设计一个MyPoint类,表示一个具有x坐标和y坐标的点,该类包括:
- 两个私有成员变量x和y表示坐标值;
- 成员变量x和y的访问器和修改器
- 无参构造方法创建点(0,0);
- 一个有参构造方法,根据参数指定坐标创建一个点;
- distance方法(static修饰)返回参数为MyPoint类型的两个点对象之间的距离。
编写主类Test,在主类中输入两点坐标,创建两个点对象,利用distance()方法计算这两个点之间的距离。
/*从主类zzz中获取数,在qqq中利用public static double distance(ccc A, ccc B) {
double distance = Math.sqrt((A.x - B.x) * (A.x - B.x) + (A.y + B.y) * (A.y + B.y));
return distance;方法计算两点间的距离,然后返回主类zzz输出*/
package qqq;
import java.util.Scanner;
public class ccc {
double x;
double y;
public ccc() {
x = 0.0;
y = 0.0;
}
public ccc(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 distance(ccc A, ccc B) {
double distance = Math.sqrt((A.x - B.x) * (A.x - B.x) + (A.y + B.y) * (A.y + B.y));
return distance;
}
}
测试类
package qqq;
import qqq.MyPoint;
import java.util.Scanner;
public class zzz {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ccc[] p = new ccc[2];
System.out.println("输入第一个坐标");
double x = input.nextDouble();
double y = input.nextDouble();
p[0] = new ccc(x, y);
System.out.println("输入第二个坐标");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
p[1] = new ccc(x, y);
System.out.println("点(" + p[0].x + "," + p[0].y + ")" + "与点" + "(" + p[1].x + "," + p[1].y + ")" + "的距离是 "
+ ccc.distance(p[0], p[1]));
}
}
运行截图