1、将笛卡尔坐标系上的点定义为一个服务类point,point类求坐标系上两点间的距离。设计测试point服务类的应用程序主类,显示输出已创建对象间的距离。
代码一
package one;
public class point {
double x1;
double x2;
double y1;
double y2;
double dis;
public point(double a, double b, double c, double d) {
this.x1 = a;
this.y1 = b;
this.x2 = c;
this.y2 = d;
}
void distance() {
dis = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
System.out.println("第一个点坐标为" + x1 + " " + y1);
System.out.println("第一个点坐标为" + x2 + " " + y2);
System.out.println("x y两点之间距离为" + dis);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
point p = new point(1, 1, 4, 5);
p.distance();
System.out.println();
System.out.println("学号+姓名");
}
}
代码二
package one;
public class point {
private int x;
private int y;
public point(int x, int y) {
this.x = x;
this.y = y;
}
public double distance(point p1, point p2) {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
point point1 = new point(1, 1);
point point2 = new point(4, 5);
System.out.println(point1.distance(point1, point2));
System.out.println("学号+姓名");
}
}
2、某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
代码一
package one;
import java.util.Scanner;
public class Number {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个四位数:");
int x = sc.nextInt();
Number number = new Number();
System.out.println("加密之后的数字是:" + number.jiami(x));
System.out.println("解密之后的数字是:" + number.jiemi(number.jiami(x)));
System.out.println();
System.out.println("学号+姓名");
}
public int jiami(int x) {
int a = x / 1000;
int b = x % 1000 / 100;
int c = x % 100 / 10;
int d = x % 10;
a = (a + 5) % 10;
b = (b + 5) % 10;
c = (c + 5) % 10;
d = (d + 5) % 10;
x = 1000 * d + 100 * c + 10 * b + a;
return x;
}
public int jiemi(int x) {
int a = x / 1000;
int b = x % 1000 / 100;
int c = x % 100 / 10;
int d = x % 10;
a = (a + 5) % 10;
b = (b + 5) % 10;
c = (c + 5) % 10;
d = (d + 5) % 10;
x = 1000 * d + 100 * c + 10 * b + a;
return x;
}
}
代码二
package one;
import java.util.Scanner;
public class ex {
public int input() {
System.out.println(“请输入一个四位数”);
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
return number;
}
public void change(int number) {
int a, b, c, d;
a = number % 10;
b = number / 10 % 10;
c = number / 100 % 10;
d = number / 1000;
a = (a + 5) % 10;
b = (b + 5) % 10;
c = (c + 5) % 10;
d = (d + 5) % 10;
int temp;
temp = a;
a = d;
d = temp;
temp = b;
b = c;
c = temp;
System.out.println("交换后的数为" + d + c + b + a);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ex test = new ex();
int ex = test.input();
test.change(ex);
System.out.println("姓名+学号");
}
}
3、定义一个changFangTi的父类,里面有chang,kuan两个属性。和相应的构造方法和get、set方法。定义个changFangTi2的子类继承changFangti,里面有一个gao属性,有相应的get和set方法。和求体积的tiJi()方法。在main()方法中调用父类的方法。并求长方体的体积。
package one;
public class ChangFangTi {
private double chang;
private double kuan;
private double gao;
public ChangFangTi() {
}
public class ChangFangTi2 extends ChangFangTi {
public ChangFangTi2(int i, int j, int k) {
// TODO Auto-generated constructor stub
}
}
public ChangFangTi(double chang, double kuan, double gao) {
this.chang = chang;
this.kuan = kuan;
this.gao = gao;
}
public void setChang(double chang) {
this.chang = chang;
}
public double getChang() {
return this.chang;
}
public void setKuan(double kuan) {
this.kuan = kuan;
}
public double getKuan() {
return this.kuan;
}
public void setGao(double gao) {
this.gao = gao;
}
public double getGao() {
return this.gao;
}
public double mainji() {
return chang * kuan * gao;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ChangFangTi changfangti2 = new ChangFangTi(15, 10, 10);
System.out.println("长方体的体积是:" + changfangti2.mainji());
System.out.println("学号+姓名");
}
}
4、乐器(Instrument)分为、钢琴(Piano)、小提琴(Violin),各种乐器的弹奏(play)方法各不相同。 编写一个测试类 InstrumentTest,要求编写方法 testPlay,对各种乐器进行弹奏测试。要依据乐器的不同进行相应的弹奏。
package one;
public class InstrumentTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Instrument piano = new Piano();
Instrument violin = new Violin();
piano.play();
violin.play();
}
}
interface Instrument {
public abstract void play();
}
class Piano implements Instrument {
public void play() {
System.out.println(“钢琴演奏”);
}
}
class Violin implements Instrument {
public void play() {
System.out.println(“小提琴演奏”);
System.out.println(“姓名+学号”);
}
}
注:println(" ")里面是在英文状态下输入双引号。