Java面向对象 - PTA上一道题的分享

本题描述

  1.定义抽象类Shape
  属性:不可变静态常量double PI,值为3.14
  抽象方法:public double getPerimeter(),public double getArea()

  2.RectangleCircle类均继承自Shape类。
  Rectangle类(属性:int width,length)、Circle类(属性:int radius)。
  带参构造方法为Rectangle(int width,int length),Circle(int radius)。toString方法(Eclipse自动生成)

  3.编写double sumAllArea方法计算并返回传入的形状数组中所有对象的面积和double sumAllPerimeter方法计算并返回传入的形状数组中所有对象的周长和

 4.main方法
 4.1 输入整型值n,然后建立n个不同的形状。如果输入rect,则依次输入宽、长。如果输入cir,则输入半径。
 4.2 然后输出所有的形状的周长之和,面积之和。并将所有的形状信息以样例的格式输出。

 提示:使用Arrays.toString
 4.3 最后输出每个形状的类型与父类型.使用类似shape.getClass() //获得类型, shape.getClass().getSuperclass() //获得父类型;

注意:处理输入的时候使用混合使用nextIntnextLine需注意行尾回车换行问题。


思考

  1. 你觉得sumAllArea和sumAllPerimeter方法放在哪个类中更合适?
  2. 是否应该声明为static?

输入样例:

4
rect
3 1
rect
1 5
cir
1
cir
2

输出样例:

38.84
23.700000000000003
[Rectangle [width=3, length=1], Rectangle [width=1, length=5], Circle [radius=1], Circle [radius=2]]
class Rectangle,class Shape
class Rectangle,class Shape
class Circle,class Shape
class Circle,class Shape


我写的代码,仅供参考:

import java.util.Arrays;
import java.util.Scanner;
public class Main 
{
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		Shape a[]=new Shape[n];
		for(int q=0;q<=n-1;++q)
		{
			String temp=in.next();
			if(temp.equals("rect"))
			{
				a[q]=new Rectangle(in.nextInt(),in.nextInt());
			}
			else
			{
				a[q]=new Circle(in.nextInt());
			}
		}
		in.close();
		System.out.printf("%.2f\n",Shape.sumAllPerimeter(a));
		System.out.println(Shape.sumAllArea(a));
		System.out.println(Arrays.toString(a));
		for(Shape e:a)
		{
			System.out.println(e.getClass()+","+e.getClass().getSuperclass());
		}
	}

}
abstract class Shape
{
	public static final double PI=3.14;
	public abstract double getPerimeter();
	public abstract double getArea();
	public static double sumAllArea(Shape a[])
	{
		double sum=0;
		for(Shape e:a)
		{
			sum+=e.getArea();
		}
		return sum;
	}
	public static double sumAllPerimeter(Shape a[])
	{
		double sum=0;
		for(Shape e:a)
		{
			sum+=e.getPerimeter();
		}
		return sum;
	}
}
class Circle extends Shape
{
	private int radius;
	Circle(int radius)
	{
		this.radius=radius;
	}
	public double getArea()
	{
		return PI*radius*radius;
	}
	public double getPerimeter()
	{
		return 2*PI*radius;
	}
	@Override
	public String toString() 
	{
		return "Circle [radius=" + radius + "]";
	}
}
class Rectangle extends Shape
{
	private int width;
	private int length;
    Rectangle(int width,int length)
	{
		this.width=width;
		this.length=length;
	}
	public double getArea()
	{
		return width*length;
	}
	public double getPerimeter()
	{
		return 2*(width+length);
	}
	@Override
	public String toString() 
	{
		return "Rectangle [width=" + width + ", length=" + length + "]";
	}
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值