实验内容:
1、设计一个圆形类(Circle),包括中心点坐标、半径值等成员变量;
用户创建两个圆形对象,并输入两个圆的中心坐标和各自的半径值,判断第二个圆是否在第一个圆内,还是和第一个圆重叠
2、编写一个帐户类实现银行帐户的概念,创建帐户类的对象,完成存款与取款等操作(要注意取款额不能大于账户内存款数额)
内容一代码:
package java_experiments;
import java.util.Scanner;
class Circle {
int x, y, r;
public Circle(int x, int y, int r) {
this.x = x;
this.y = y;
this.r = r;
}
public static void judge(Circle c1, Circle c2) {
int distance1 = (c1.x - c2.x) * (c1.x - c2.x) + (c1.y - c2.y) * (c1.y - c2.y);
int distance2 = (c1.r - c2.r) * (c1.r - c2.r);
int distance3 = (c1.r + c2.r) * (c1.r + c2.r);
if (distance1 <= distance2) { // 圆心之间距离小于等于半径之差的平方
System.out.println("两圆包含");
} else if (distance1 > distance3) {// 圆心之间距离大于于半径之和之差的平方
System.out.println("两圆为其他关系");
} else {
System.out.println("两圆重叠");
}
}
}
public class five_one {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入第一个圆心位置及半径:");
int x1 = input.nextInt();
int y1 = input.nextInt();
int r1 = input.nextInt();
Circle one = new Circle(x1, y1, r1);
System.out.println("输入第二个圆心位置及半径:");
int x2 = input.nextInt();
int y2 = input.nextInt();
int r2 = input.nextInt();
Circle two = new Circle(x2, y2, r2);
Circle.judge(one, two);
}
}
实验结果
内容二代码:
package java_experiments;
import java.util.Scanner;
class Account {
String name;
int cardId;
double money = 0;
public void saveMoney(double inmoney) {
money += inmoney;
System.out.println("存款" + inmoney + "元成功");
System.out.println("当前账户总金额:" + money + "\n");
}
public void drawMoney(double outmoney) {
if (outmoney <= money) {
money -= outmoney;
System.out.println("取款" + outmoney + "元成功");
System.out.println("余额" + money + "\n");
} else
System.out.println("余额不足\n");
}
}
public class five_two {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Account person = new Account();
while (true) {
System.out.println("1.账户设置");
System.out.println("2.存钱");
System.out.println("3.取钱");
System.out.println("0.退出");
System.out.print("==输入菜单==:");
int num = input.nextInt();
switch (num) {
case 1:
System.out.println("名称:");
person.name = input.next();
System.out.println("卡号:");
person.cardId = input.nextInt();
System.out.println("初始金额:" + person.money + "\n");
break;
case 2:
System.out.println("账户" + person.cardId + "存款金额:");
float inmoney = input.nextFloat();
person.saveMoney(inmoney);
break;
case 3:
System.out.println("账户" + person.cardId + "余额" + person.money);
System.out.println("取款金额:");
float outmoney = input.nextFloat();
person.drawMoney(outmoney);
break;
}
if (num == 0) {
System.out.println(person.name + "您已退出\n");
break;
}
}
}
}