PTA练习02 函数题

6-1 创建一个直角三角形类实现IShape接口 (10 分)

创建一个直角三角形类(regular triangle)RTriangle类,实现下列接口IShape。两条直角边长作为RTriangle类的私有成员,类中包含参数为直角边的构造方法。

interface IShape {// 接口

public abstract double getArea(); // 抽象方法 求面积

public abstract double getPerimeter(); // 抽象方法 求周长

}

###直角三角形类的定义:

直角三角形类的构造函数原型如下:
RTriangle(double a, double b);

其中 a 和 b 都是直角三角形的两条直角边。

裁判测试程序样例:


import java.util.Scanner;
import java.text.DecimalFormat;

interface IShape {
    public abstract double getArea();

    public abstract double getPerimeter();
}

/*你写的代码将嵌入到这里*/


public class Main {
    public static void main(String[] args) {
        DecimalFormat d = new DecimalFormat("#.####");
        Scanner input = new Scanner(System.in);
        double a = input.nextDouble();
        double b = input.nextDouble();
        IShape r = new RTriangle(a, b);
        System.out.println(d.format(r.getArea()));
        System.out.println(d.format(r.getPerimeter()));
        input.close();
    }
}

输入样例:

3.1 4.2

结尾无空行

输出样例:

6.51
12.5202

结尾无空行

class RTriangle implements IShape{
	private double a,b;
	
	public RTriangle(double a, double b) {
		this.a = a;
		this.b = b;
	}
	
	public double getArea() {
		return 0.5*a*b;
	}
	
	public double getPerimeter() {
		return a+b+Math.sqrt(a*a+b*b);
	}
}

 

 

6-2 从抽象类shape类扩展出一个圆形类Circle (10 分)

请从下列的抽象类shape类扩展出一个圆形类Circle,这个类圆形的半径radius作为私有成员,类中应包含初始化半径的构造方法。

public abstract class shape {// 抽象类

public abstract double getArea();// 求面积

public abstract double getPerimeter(); // 求周长

}

主类从键盘输入圆形的半径值,创建一个圆形对象,然后输出圆形的面积和周长。保留4位小数。

圆形类名Circle

裁判测试程序样例:

import java.util.Scanner;
import java.text.DecimalFormat;

abstract class shape {// 抽象类
     /* 抽象方法 求面积 */
    public abstract double getArea( );

    /* 抽象方法 求周长 */
    public abstract double getPerimeter( );
}

/* 你提交的代码将被嵌入到这里 */

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        DecimalFormat d = new DecimalFormat("#.####");// 保留4位小数
         double r = input.nextDouble( ); 
        shape c = new  Circle(r);

        System.out.println(d.format(c.getArea()));
        System.out.println(d.format(c.getPerimeter()));
        input.close();
    } 
}

输入样例:

3.1415926

结尾无空行

输出样例:

31.0063
19.7392

结尾无空行

class Circle extends shape
{
	double radius;
	final static double PI = 3.1415926535;
	public Circle(double radius) {
		this.radius = radius;
	}
	@Override
	public double getArea() {
		// TODO Auto-generated method stub
		return PI*radius*radius;
	}
	@Override
	public double getPerimeter() {
		// TODO Auto-generated method stub
		return 2*PI*radius;
	}
}

 

 

6-3 jmu-Java-03面向对象基础-覆盖与toString (3 分)

Person类,Company类,Employee类。

其中Employee类继承自Person类,属性为:

private Company company;
private double salary;

现在要求编写Employee类的toString方法,返回的字符串格式为:父类的toString-company的toString-salary

函数接口定义:

public String toString()

输入样例:


输出样例:

Li-35-true-MicroSoft-60000.0
public String toString() {
		return super.toString()+"-"+company.toString()+"-"+salary;
	}

 

6-4 图书和音像租赁 (12 分)

图书和音像店提供出租服务,包括图书和DVD的出租。图书包括书名(String,一个词表示)和价格(double),DVD包括片名(String,一个词表示)。它们都是按天出租,但租金计算方式却不同,图书的日租金为图书价格的1%,DVD的日租金为固定的1元。构造图书和DVD类的继承体系,它们均继承自Media类,且提供方法getDailyRent()返回日租金,构造音像店类MediaShop,提供静态函数double calculateRent(Media[] medias, int days)。 在main函数中构造了Media数组,包含图书和DVD的对象,调用calculateRent方法得到并输出租金,保留小数点两位

输入描述:

待租图书和DVD的数量

图书和DVD的详细信息

租借天数

输出描述:

总的租金

裁判测试程序样例:

import java.util.Scanner;

public class Main {
        public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Media[] ms  = new Media[n];
        for (int i=0; i<n; i++) {
            String type = sc.next();
            if (type.equals("book")) {
                ms[i] = new Book(sc.next(), sc.nextDouble());
            }else {
                ms[i] = new DVD(sc.next());
            }
        }
        double rent = MediaShop.calculateRent(ms, sc.nextInt());
        System.out.printf("%.2f", rent);
    }
}

/* 请在这里填写答案 */

输入样例:

5
book Earth 25.3
book Insights 34
dvd AI
dvd Transformer
book Sun 45.6
20

结尾无空行

输出样例:

60.98

结尾无空行

abstract class Media{
	abstract double getDailyRent();
}
class MediaShop{
	static double calculateRent(Media[] medias, int days) {
		double sum=0;
		for(Media m:medias)
		{
			if(m instanceof DVD)
			{DVD d=(DVD)m;
			sum+=d.getDailyRent()*days;
		}else if(m instanceof Book) {
			Book b=(Book)m;
			sum+=b.getDailyRent()*days;
		}
	}
	return sum;
}}
class Book extends Media{
	String name;
	double price;
	public Book(String name,double price){
		this.name=name;
		this.price=price;
	}
	public double getDailyRent() {
		return price*0.01;
	}
}
class DVD extends Media{
	String name;
	public DVD(String name){
		this.name=name;
	}
	public double getDailyRent() {
		return 1;
	}
}

 

6-5 使用继承设计:教师类。 (10 分)

使用继承设计:教师类。 使程序运行结果为:

Li 40 信工院
教师的工作是教学。

函数接口定义:

定义类Teacher, 继承Person

裁判测试程序样例:


class Person{
    String name;
    int age;
    Person(String name,int age){
        this.name = name;
        this.age = age;
    }
    void work(){

    }
    void show() {
        System.out.print(name+" "+age+" ");
    }
}

/* 请在这里填写答案 */

public class Main {

    public static void main(String[] args) {

         Teacher t = new Teacher("Li",40,"信工院");
         t.show();
         t.work();
    }
}

输入样例:

没有输入

输出样例:

Li 40 信工院
教师的工作是教学。

结尾无空行

class Teacher extends Person{
    String professional;
    Teacher(String name, int age, String professional) {
        super(name, age);
        this.professional=professional;
    }
    @Override
    void work(){
        System.out.println(professional);
        System.out.println("教师的工作是教学。");
    }
}

 

 

6-6 面积求和 (5 分)

由于各种图形 求面积的方式不一样,为了使编写的程序能够适应求不同形状的面积和,当新增加一个图形的时候,不需要修改其他类,采用面向接口的编程,其类图如下图所示:

绘图1.jpg

根据以上类图,你需要设计和编写如下类: ######1.IGeometry接口类 ,里面有double getArea()接口方法 2.Rect类实现接口,有属性 a,b,有参构造方法及对接口方法的实现 ######3.Circle类实现接口,有属性r,有参构造方法及对接口方法的实现,其中PI=3.14 4.TotalArea类完成对不同形状图形面积求和,有属性IGeometry[] tuxing 数组用来存储 不同形状图形对象,SetTuxing(IGeometry[] t) 给属性tuxing赋值,double computeTotalArea()方法用来计算tuxing数组中存放的图形的面积和,并返回面积和。

主类已经给出,请结合主类完成上面类的编写

裁判测试程序主类:

public class Main {

    public static void main(String[] args) {
         IGeometry [] tuxing=new IGeometry[29]; //有29个Geometry对象
          for(int i=0;i<tuxing.length;i++) {   //29个Geometry对象分成两类
              if(i%2==0)
                  tuxing[i]=new Rect(16+i,68);
              else if(i%2==1)
                  tuxing[i]=new Circle(10+i);
          } 
          TotalArea computer=new TotalArea();
          computer.setTuxing(tuxing);  
          System.out.printf("各种图形的面积之和:\n%.2f",computer.computerTotalArea());

    }
}

/* 请在这里填写答案  请结合主类代码,在代码框完成IGeometry接口类,Rect类,Circle类和TotalArea类*/

输入样例:

在这里给出一组输入。例如:

结尾无空行

输出样例:

在这里给出相应的输出。例如:

各种图形的面积之和:
58778.36

结尾无空行

interface IGeometry {
	double PI=3.14;
	double getArea();
}

 class Rect implements IGeometry {
	
	public double a;
	public double b;
	
	public Rect(double a,double b) {
		this.a=a;
		this.b=b;
	}
	
	public double getArea() {
		// TODO Auto-generated method stub
		return a*b;
	}

}

 class Circle implements IGeometry {

	public double r;
	
	public Circle(double r) {
		this.r=r;
	}
	
	
	public double getArea() {
		// TODO Auto-generated method stub
		return PI*r*r;
	}

}

 class TotalArea {
	IGeometry[] tuxing;
	
	public void setTuxing(IGeometry[] t) {
		tuxing=t;
	}
	
	public double computerTotalArea() {
		double sum=0;
		
		for(int i=0;i<tuxing.length;i++) {
			sum+=tuxing[i].getArea();
		}
		
		return sum;
	}
}

 

6-7 模拟题: 重写父类方法equals (5 分)

在类Point中重写Object类的equals方法。使Point对象x和y坐标相同时判定为同一对象。

裁判测试程序样例:

import java.util.Scanner;
class Point {
private int xPos, yPos;
public Point(int x, int y) {
    xPos = x;
    yPos = y;
    }
@Override
/* 请在这里填写答案 */
}
public class Main {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       Object p1 = new Point(sc.nextInt(),sc.nextInt());
       Object p2 = new Point(sc.nextInt(),sc.nextInt());
       System.out.println(p1.equals(p2));
      sc.close();
}
}

输入样例:

10 20
10 20

结尾无空行

输出样例:

true

结尾无空行

public boolean equals(Object d){
    if(this == d)
        return true;
    if(d instanceof Point)
    {
        Point p = (Point ) d; 
         if(this.xPos == p.xPos && this.yPos == p.yPos)
             return true;
    }
    return false;
}

 

6-8 普通账户和支票账户 (10 分)

编写一个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

结尾无空行

class Account{
	private int id;
	private int balance;
	Account(){
		id=0;
		balance=0;
	}
	Account(int id,int balance){
		this.id=id;
		this.balance=balance;
	}
	void setBalance(int balance) {
		this.balance=balance;
	}
	int getBalance() {
		return balance;
	}
	boolean withdraw(int money) {
		if(balance<money) {
			return false;
		}
		else {
			balance=balance-money;
			return true;
		}
	}
		void deposit(int money) {
			this.balance+=money;
		}
		public String toString() {
			return "The balance of account "+id+" is "+this.balance;
	}

}
class CheckingAccount extends Account{
	private int overdraft;
	
	public CheckingAccount(int a,int b,int c){
		super(a,b);
		this.overdraft=c;
	}
	
	boolean withdraw(int money) {
		if(money>(overdraft+this.getBalance())) {
			return false;
		}
		else {
			this.setBalance(this.getBalance()-money);
			return true;
		}
	}
}

 

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值