普通账户和支票账户
分数 20
全屏浏览
切换布局
作者 郑珺
单位 浙江传媒学院
编写一个Java程序,包含类Acount、CheckingAccount、Main,其中Main已经实现,请你编写Acount和CheckingAccount类。
(1)编写一个类Account表示普通账户对象,包含以下成员
①属性:
- id:私有,int型,表示账户编号;
- balance:私有,int型,表示账户余额;
②方法:
- Account(), 构造方法,id和balance都初始化为0;
- Account(int id,int balance),构造方法,用参数设置账户编号和余额;
- void setBalance(int balance):修改账户金额
- int getBalance():返回账户金额
- boolean withdraw(int money):从账户提取特定数额,如果余额不足,返回false;否则,修改余额,返回true;
- void deposit(int money):向账户存储特定数额。
- public String toString():将把当前账户对象的信息转换成字符串形式,例如id为123,余额为1000,返回字符串"The balance of account 123 is 1000"。
(2)编写一个Account类的子类CheckingAccount,表示支票账户对象,包含以下成员
①属性:
- overdraft:私有,int型,表示透支限定额;
②方法:
- CheckingAccount(), 构造方法,id、balance和overdraft都初始化为0;
- CheckingAccount(int id,int balance,int overdraft),构造方法,用参数设置账户编号、余额和透支限定额;
- boolean withdraw(int money):从账户提取特定数额,如果超出透支限定额,返回false;否则,修改余额,返回true;
(3)编写公共类Main,实现如下功能
- 根据用户输入的两个整数id和balance创建普通账户a;
- 输入一个整数n,表示对账户a有n笔操作;
- 每笔操作输入一个字符串和一个整数money(表示操作金额)
- 如果字符串为“withdraw”表示取现操作,如果操作成功,输出“withdraw ” + money + “success”,否则输出“withdraw ” + money + “failed”
- 如果字符串为“deposit”表示存入操作,完成操作后输出“deposit” + money + “success”
- 使用toString方法输出账户a信息。
- 根据用户输入的三个整数id、balance和overdraft创建支票账户b;
- 输入一个整数m,表示对账户b有m笔操作;
- 每笔操作输入一个字符串和一个整数money(表示操作金额)
- 如果字符串为“withdraw”表示取现操作,如果操作成功,输出“withdraw ” + money + “success”,否则输出“withdraw ” + money + “failed”
- 如果字符串为“deposit”表示存入操作,完成操作后输出“deposit” + money + “success”
- 使用toString方法输出账户b信息。
裁判测试程序样例:
import java.util.Scanner; public class Main{ public static void main(String args[]){ Scanner input = new Scanner(System.in); int n,m; Account a = new Account(input.nextInt(),input.nextInt()); n = input.nextInt(); for(int i=0; i < n; i++) { String op; int money; op = input.next(); money = input.nextInt(); if(op.equals("withdraw")) { if(a.withdraw(money)) { System.out.println("withdraw " + money + " success"); } else { System.out.println("withdraw " + money + " failed"); } } else if(op.equals("deposit")) { a.deposit(money); System.out.println("deposit " + money + " success"); } } System.out.println(a.toString()); CheckingAccount b = new CheckingAccount(input.nextInt(),input.nextInt(),input.nextInt()); m = input.nextInt(); for(int i=0; i < m; i++) { String op; int money; op = input.next(); money = input.nextInt(); if(op.equals("withdraw")) { if(b.withdraw(money)) { System.out.println("withdraw " + money + " success"); } else { System.out.println("withdraw " + money + " failed"); } } else if(op.equals("deposit")) { b.deposit(money); System.out.println("deposit " + money + " success"); } } System.out.println(b.toString()); } } /* 请在这里填写答案 */
输入样例:
1 100
5
withdraw 200
withdraw 100
deposit 50
deposit 100
withdraw 200
2 100 200
5
withdraw 200
withdraw 100
deposit 50
deposit 100
withdraw 200
输出样例:
withdraw 200 failed
withdraw 100 success
deposit 50 success
deposit 100 success
withdraw 200 failed
The balance of account 1 is 150
withdraw 200 success
withdraw 100 success
deposit 50 success
deposit 100 success
withdraw 200 failed
The balance of account 2 is -50
代码长度限制
16 KB
时间限制
800 ms
内存限制
64 MB
class Account {
private int id;
private int balance;
public Account() {
this.id = 0;
this.balance = 0;
}
public Account(int id, int balance) {
this.id = id;
this.balance = balance;
}
public boolean withdraw(int money) { // 取款
if (money > balance) {
return false;
} else {
balance -= money;
return true;
}
}
public void deposit(int money) { // 存款
balance += money;
}
public int getBalance() {
return balance;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setBalance(int balance) {
this.balance = balance;
}
@Override
public String toString() {
return "id = " + id + ", balance = " + balance;
}
}
class CheckingAccount extends Account {
private int overdraft;
public CheckingAccount() {
super();
this.overdraft = 0;
}
public CheckingAccount(int id, int balance, int overdraft) {
super(id, balance);
this.overdraft = overdraft;
}
@Override
public boolean withdraw(int money) {
if (this.getBalance() + overdraft >= money) {
this.setBalance(this.getBalance() - money);
return true;
}
return false;
}
}
圆和圆柱体1
分数 20
全屏浏览
切换布局
作者 郑珺
单位 浙江传媒学院
编写一个Java程序,包含类Circle、Cylinder、Main,其中Main已经实现,请你编写Circle和Cylinder类。
(1)编写类Circle,表示圆形对象,包含以下成员
①属性:
- radius:私有,double型,圆形半径;
②方法:
- Circle(double radius), 构造方法,用参数设置圆的半径
- Circle(),构造方法,将圆形初始化为半径为0。
- void setRadius(double r):用参数r设置radius的值
- double getRadius():返回radius的值
- double getArea(),返回圆形的面积
- double getPerimeter(),返回圆形的周长
- public String toString( ),将把当前圆对象的转换成字符串形式,例如圆半径为10.0,返回字符串"Circle(r:10.0)"。
(2)编写一个Circle类的子类Cylinder,表示圆柱形对象,包含以下成员
①属性:
- height:私有,double型,圆柱体高度;
②方法:
- Cylinder(double radius,double height), 构造方法,用参数设置圆柱体的半径和高度
- Cylinder(),构造方法,将圆柱体的半径和高度都初始化为0。
- void setHeight(double height):用参数height设置圆柱体的高度
- double getHeight():返回圆柱体的高度
- double getArea(),重写Circle类中的area方法,返回圆柱体的表面积
- double getVolume(),返回圆柱体的体积
- public String toString( ),将把当前圆柱体对象的转换成字符串形式,例如半径为10.0,高为5.0,返回字符串"Cylinder(r:10.0,h:5.0)"。
输入格式:
第一行输入一个整数n,表示有n个几何图形。
以下有n行,每行输入一个几何图形的数据。
每行先输入一个字符串表示几何图形的类型,“Circle”表示圆形,“Cylinder”表示圆柱体。
如果是圆形,输入一个浮点数表示其半径。
如果是圆柱体,输入两个浮点数分别表示其半径和高度。
输出格式:
如果是圆形,要求计算其面积和周长并输出。
如果是圆柱体,要求计算其面积和体积并输出。
注意,圆周率用Math.PI
裁判测试程序样例:
import java.util.Scanner; public class Main{ public static void main(String args[]) { Scanner input = new Scanner(System.in); int n = input.nextInt(); for(int i = 0; i < n; i++) { String str = input.next(); if(str.equals("Circle")) { Circle c = new Circle(input.nextDouble()); System.out.println("The area of " + c.toString() + " is " + c.getArea()); System.out.println("The perimeter of " + c.toString() + " is "+ c.getPerimeter()); } else if(str.equals("Cylinder")) { Cylinder r = new Cylinder(input.nextDouble(), input.nextDouble()); System.out.println("The area of " + r.toString() + " is " + r.getArea()); System.out.println("The volume of " + r.toString() + " is " + r.getVolume()); } } } } /* 请在这里填写答案 */
输入样例:
4
Circle 10.0
Cylinder 10.0 10.0
Circle 1.1
Cylinder 1.1 3.4
输出样例:
The area of Circle(r:10.0) is 314.1592653589793
The perimeter of Circle(r:10.0) is 62.83185307179586
The area of Cylinder(r:10.0,h:10.0) is 1256.6370614359173
The volume of Cylinder(r:10.0,h:10.0) is 3141.5926535897934
The area of Circle(r:1.1) is 3.8013271108436504
The perimeter of Circle(r:1.1) is 6.911503837897546
The area of Cylinder(r:1.1,h:3.4) is 31.101767270538957
The volume of Cylinder(r:1.1,h:3.4) is 12.924512176868411
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
class Circle {
private double radius;
public Circle() {
this.radius = 0;
}
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
@Override
public String toString() {
return "Circle(r:" + this.radius + ")";
}
}
class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
public Cylinder() {
super();
this.height = 0;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public double getArea() {
return (2 * Math.PI * this.getRadius() * this.height + 2 * Math.PI * this.getRadius() * this.getRadius());
}
public double getVolume() {
return Math.PI * this.getRadius() * this.getRadius() * this.height;
}
@Override
public String toString() {
return "Cylinder(r:" + getRadius() + ",h:" + height + ")";
}
}