1、设计一个圆形类(Circle),包括中心点坐标、半径值等成员变量;
用户创建两个圆形对象,并输入两个圆的中心坐标和各自的半径值,判断第二个圆是否在第一个圆内,还是和第一个圆重叠
2、编写一个帐户类实现银行帐户的概念,创建帐户类的对象,完成存款与取款等操作(要注意取款额不能大于账户内存款数额)
//新建包,名为java5,以下所有代码均属于包java5 //第一题第一个类Circle package java5; public class Circle{ public int arr[] = new int[2]; //创建一个数组来存放圆心坐标 public int r; //半径 Circle(int arr[],int r) { this.arr = arr; this.r = r; } public void judjeLocation(Circle c1,Circle c2) { //判断两个圆的位置关系 //定义一个centerDistance表示两个圆心之间的举例 double centerDistance = Math.sqrt((c1.arr[0]-c2.arr[0]) * (c1.arr[0]-c2.arr[0]) + (c1.arr[1]-c2.arr[1]) * (c1.arr[1]-c2.arr[1])); if (c1.r < c2.r) { int temp = c1.r; c1.r = c2.r; c2.r = temp; } if (centerDistance > (c1.r+ c2.r)) { System.out.println("这两个圆不相交!"); } else if(centerDistance < (c1.r+ c2.r)) { if (centerDistance < (c1.r-c2.r)) { System.out.println("第二个圆在第一个圆之内!"); } else { System.out.println("第二个圆和第一个圆重叠!"); } } else System.out.println("两个圆相切!"); } //第一题第二个类Test package java5; import java.util.*; public class Test{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int arr[] = new int[2]; System.out.print("请输入第一个圆心的横坐标:"); arr[0] = scanner.nextInt(); System.out.print("请输入第一个圆心的纵坐标:"); arr[1] = scanner.nextInt(); System.out.print("请输入第一个圆的半径:"); int r = scanner.nextInt(); int arr1[] = new int[2]; System.out.print("请输入第二个圆心的横坐标:"); arr1[0] = scanner.nextInt(); System.out.print("请输入第二个圆心的纵坐标:"); arr1[1] = scanner.nextInt(); System.out.print("请输入第二个圆的半径:"); int r1 = scanner.nextInt(); Circle c1 = new Circle(arr,r); Circle c2 = new Circle(arr1,r1); c1.judjeLocation(c1,c2); } } //第二题第一个类Account package java5; public class Account{ private int vacancies; //账户余额 Account (int vacancies) { this.vacancies = vacancies; } public int deposite(int money) { //存款方法 vacancies = vacancies + money; return this.vacancies; } public int draw (int money) { if (vacancies > money) { vacancies = vacancies - money; return vacancies; } else { System.out.println("余额不足!"); return 0; } } } //第二题第二个类Account_Test package java5; import java.util.*; public class Account_Test{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入最初始账户中余额:"); int vacancies = scanner.nextInt(); Account account = new Account(vacancies); System.out.println("该账户中的余额为:"+vacancies); System.out.print("请输入存款金额:"); int money1 = scanner.nextInt(); int x = account.deposite(money1); System.out.println("存款之后,账户余额为:"+x); System.out.print("请输入取款金额:"); int money2 = scanner.nextInt(); int y = account.draw(money2); System.out.println("取款之后,账户余额为:"+y); } }
执行结果可自行尝试,这里即略