Java-类与对象部分编程题

package cn.ALAN_CF.thinking_5;

import java.util.Scanner;

/**
 * 给定图形接口定义如下:
 * interface Shape{
 * float getArea();//求面积
 * float getPerimeter();//求周长
 * }
 * 请实现以上接口,定义圆形类(用数字1表示)、矩形类(用数字2表示)。
 * 从键盘输入图形类别(1代表圆,2代表矩形)和相应的参数,计算并输出相应图形的面积和周长,结果保留小数点后2位数据。
 * 输入格式:
 * [
 *    输入数据包含多行,第一行一个整数n,表示接下来共有n个图形对象需要生成。
 *    每个图形数据占2行,第一行为数字1或2,表示图形类别,第二行为生成图形的参数。
 * ]
 * 输出格式:
 * [
 *    每个图形对应的面积和周长。
 * ]
 * 输入样例:
 * 2
 * 1
 * 1.0
 * 2
 * 1.0 2.0
 * 输出样例:
 * 3.14 6.28
 * 2.00 6.00
 * @author 15328
 */
public class Shape {
    public static void show (int x, Shaped[] shapes ){
        for(int i = 0; i < x; i++){
            System.out.printf("%.2f",shapes[i].getArea());
            System.out.print(" ");
            if(i != x-1) {
                System.out.printf("%.2f\n",shapes[i].getPerimeter());
            }
            else{
                System.out.printf("%.2f",shapes[i].getPerimeter());
            }
        }
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        Shaped [] shapes = new Shaped[x];
        int t;
        double r,l,w;
        for(int i = 0; i < x; i++){
            t = input.nextInt();
            switch(t){
                case 1:{
                    r = input.nextDouble();
                    shapes[i] = new Circles(r);
                }
                break;
                case 2: {
                    l = input.nextDouble();
                    w = input.nextDouble();
                    shapes[i] = new Rectangles(l,w);
                }
                break;
                default:
                    System.out.println("非法输入");
            }
        }
        show(x,shapes);
    }
}
interface Shapes{
    /**定义接口*/
    float getArea();//求面积
    float getPerimeter();//求周长
}
class Shaped implements Shapes{
    protected double r;
    protected double width;
    protected double length;

    public Shaped(double r){
        this.r = r;
    }
    public Shaped(double length,double width){
        this.length = length;
        this.width = width;
    }
    @Override
    public float getArea() {
        return 0;
    }

    @Override
    public float getPerimeter() {
        return 0;
    }
}
class Circles extends Shaped{

    public Circles(double r){
        super(r);
    }

    @Override
    public float getArea(){
        return (float) (Math.PI * this.r * this.r);
    }
    @Override
    public float getPerimeter(){
        return (float) (2.0 * Math.PI * this.r);
    }
}
class Rectangles extends Shaped{
    public Rectangles (double length,double width){
        super(length,width);
    }

    @Override
    public float getArea() {
        return (float) (this.length * this.width);
    }

    @Override
    public float getPerimeter() {
        return (float) (this.length * 2.0 + this.width * 2.0);
    }
}

在这里插入图片描述

package cn.Alan_DS;
/**7-3 声明图书类,记录图书总册数,利用静态变量赋值。 (10分)
        声明一个图书类,其数据成员为书名、编号(利用静态变量实现自动编号)、
        书价,并拥有静态数据成员册数,记录图书的总册数;在构造方法中,
        利用静态变量为对象的编号赋值,在主方法中定义对象数组,并求出总册数。
        输出格式:
        请输出每本图书对应的书名,书号,书价以及总图书数。
        输出样例:
        书名:Java程序设计, 书号:1, 书价:34.5
        书名:数据结构, 书号:2, 书价:44.8
        书名:C++程序设计, 书号:3, 书价:35.0
        图书总册数为:3
 * @author 15328*/
public class Book {
    private  String name;
    private  int number;
    private  double price;
    private static int auto = 0;//给每个对象设置自动编号
    private static int num = 0;//记录总册数
    public Book(String name,double price){
        auto++;
        this.name = name;
        this.number = auto;
        this.price = price;
        num++;
    }
    public static void main(String[] args) {
        Book [] books = {
                new Book("Java程序设计",34.5),
                new Book("数据结构",44.8),
                new Book("C++程序设计",35.0)
        };
        for(int i = 0; i < books.length; i++) {

            System.out.println("书名:" + books[i].name + ", 书号: " +
                    books[i].number + ", 书价:" + books[i].price);
        }
        System.out.print("图书总册数为: " + num);
    }
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值