Java复习8(PTA)

普通账户和支票账户

分数 20

全屏浏览

切换布局

作者 郑珺

单位 浙江传媒学院

编写一个Java程序,包含类Acount、CheckingAccount、Main,其中Main已经实现,请你编写Acount和CheckingAccount类。

(1)编写一个类Account表示普通账户对象,包含以下成员

①属性:

  1. id:私有,int型,表示账户编号;
  2. balance:私有,int型,表示账户余额;

②方法:

  1. Account(), 构造方法,id和balance都初始化为0;
  2. Account(int id,int balance),构造方法,用参数设置账户编号和余额;
  3. void setBalance(int balance):修改账户金额
  4. int getBalance():返回账户金额
  5. boolean withdraw(int money):从账户提取特定数额,如果余额不足,返回false;否则,修改余额,返回true;
  6. void deposit(int money):向账户存储特定数额。
  7. public String toString():将把当前账户对象的信息转换成字符串形式,例如id为123,余额为1000,返回字符串"The balance of account 123 is 1000"。

(2)编写一个Account类的子类CheckingAccount,表示支票账户对象,包含以下成员

①属性:

  1. overdraft:私有,int型,表示透支限定额;

②方法:

  1. CheckingAccount(), 构造方法,id、balance和overdraft都初始化为0;
  2. CheckingAccount(int id,int balance,int overdraft),构造方法,用参数设置账户编号、余额和透支限定额;
  3. boolean withdraw(int money):从账户提取特定数额,如果超出透支限定额,返回false;否则,修改余额,返回true;

(3)编写公共类Main,实现如下功能

  1. 根据用户输入的两个整数id和balance创建普通账户a;
  2. 输入一个整数n,表示对账户a有n笔操作;
  3. 每笔操作输入一个字符串和一个整数money(表示操作金额)
  • 如果字符串为“withdraw”表示取现操作,如果操作成功,输出“withdraw ” + money + “success”,否则输出“withdraw ” + money + “failed”
  • 如果字符串为“deposit”表示存入操作,完成操作后输出“deposit” + money + “success”
  1. 使用toString方法输出账户a信息。
  2. 根据用户输入的三个整数id、balance和overdraft创建支票账户b;
  3. 输入一个整数m,表示对账户b有m笔操作;
  4. 每笔操作输入一个字符串和一个整数money(表示操作金额)
  • 如果字符串为“withdraw”表示取现操作,如果操作成功,输出“withdraw ” + money + “success”,否则输出“withdraw ” + money + “failed”
  • 如果字符串为“deposit”表示存入操作,完成操作后输出“deposit” + money + “success”
  1. 使用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,表示圆形对象,包含以下成员

①属性:

  1. radius:私有,double型,圆形半径;

②方法:

  1. Circle(double radius), 构造方法,用参数设置圆的半径
  2. Circle(),构造方法,将圆形初始化为半径为0。
  3. void setRadius(double r):用参数r设置radius的值
  4. double getRadius():返回radius的值
  5. double getArea(),返回圆形的面积
  6. double getPerimeter(),返回圆形的周长
  7. public String toString( ),将把当前圆对象的转换成字符串形式,例如圆半径为10.0,返回字符串"Circle(r:10.0)"。

(2)编写一个Circle类的子类Cylinder,表示圆柱形对象,包含以下成员

①属性:

  1. height:私有,double型,圆柱体高度;

②方法:

  1. Cylinder(double radius,double height), 构造方法,用参数设置圆柱体的半径和高度
  2. Cylinder(),构造方法,将圆柱体的半径和高度都初始化为0。
  3. void setHeight(double height):用参数height设置圆柱体的高度
  4. double getHeight():返回圆柱体的高度
  5. double getArea(),重写Circle类中的area方法,返回圆柱体的表面积
  6. double getVolume(),返回圆柱体的体积
  7. 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 + ")";
    }
}

Java中,为了编写一个反映"分数 15 雇员和经理"场景的程序,我们可以创建两个相关的:Employee(员工)和Manager(经理)。这两个可以都继承自一个更基础的Worker,并包含共同属性如ID、姓名等。同时,Manager作为Employee的一个特殊别,可能有额外的属性比如管理的团队或者特定的评分职责。 下面是一个简单的示例: ```java // Worker,代表一般的雇员 class Worker { private String id; private String name; // 构造函数和 getter/setter 方法省略... // 公共方法,如计算总分数,这里假设每个雇员都有一个固定的分数 public int getScore() { return 0; // 这里可以根据实际需求填充分数值 } } // Employee,继承自Worker,可以是普通员工 class Employee extends Worker { // 构造函数和 getter/setter 方法省略... // 如果所有员工都有分数,这里可以直接调用super的方法 @Override public int getScore() { return super.getScore(); // 或者直接返回15或其他固定值 } } // Manager,继承自Employee,添加特有的管理角色 class Manager extends Employee { private String team; // 构造函数和 getter/setter 方法以及管理团队的方法省略... // 可能需要重写getScore方法,体现经理可能不同的评分规则 @Override public int getScore() { // 在这里添加经理的评分逻辑,例如考虑管理绩效等因素 return super.getScore() + 5; // 示例:经理比普通员工多5分 } } // 程序主入口部分: public class Main { public static void main(String[] args) { Employee employee = new Employee(); employee.setId("E001"); employee.setName("张三"); Manager manager = new Manager(); manager.setId("M001"); manager.setName("李四"); manager.setTeam("销售部"); System.out.println("员工 " + employee.getName() + " 的分数: " + employee.getScore()); System.out.println("经理 " + manager.getName() + " 的分数: " + manager.getScore()); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值