7-3 jmu-Java-03面向对象基础-03-形状 (3 分)pta的java实验

1. 定义长方形类与圆形类Circle

长方形类-类名:Rectangle,private属性:int width,length
圆形类-类名:Circle,private属性:int radius

编写构造函数:
带参构造函数:Rectangle(width, length),Circle(radius)

编写方法:
public int getPerimeter(),求周长。
public int getArea(),求面积。
toString方法,使用Eclipse自动生成。

注意:

  1. 计算圆形的面积与周长,使用Math.PI
  2. 求周长和面积时,应先计算出其值(带小数位),然后强制转换为int再返回。

2. main方法

  • 输入2行长与宽,创建两个Rectangle对象放入相应的数组。
  • 输入2行半径,创建两个Circle对象放入相应的数组。
  • 输出1:上面2个数组中的所有对象的周长加总。
  • 输出2:上面2个数组中的所有对象的面积加总。
  • 最后需使用Arrays.deepToString分别输出上面建立的Rectangle数组与Circle数组

思考:如果初次做该题会发现代码冗余严重。使用继承、多态思想可以大幅简化上述代码。

输入样例:

1 2
3 4
7
1

输出样例:

69
170
[Rectangle [width=1, length=2], Rectangle [width=3, length=4]]
[Circle [radius=7], Circle [radius=1]]

 我先将代码粘贴上来,然后对代码进行详细解析



import java.util.*;//导入 java.util包中的类接口,假如你没这句话的话 如果你要用java.util包中的    类Scanner的话你就需要这样使用 java.util.Scanner sc = new java.util.Scanner();而import     java.util.*;代表你导入了java.util包中的所有类,,这样的话你使用 Scanner就没那么麻烦了Scanner sc = new Scanner();
import java.lang.Math;//包含用于执行基本数学运算的方法

public class Main {
	public static void main(String[] args){
		Scanner in=new Scanner(System.in);
		Rectangle rect[]=new Rectangle[2];//new两个长方形对象
		Circle cir[]=new Circle[2];
		int i;
        //输入长宽
		for(i=0;i<rect.length;i++)
		{
			rect[i]=new Rectangle(in.nextInt(),in.nextInt());
		}
		for(i=0;i<cir.length;i++)
		{
			cir[i]=new Circle(in.nextInt());
		}
        //输出周长
		System.out.println(rect[0].getPerimeter()+rect[1].getPerimeter()
				+cir[0].getPerimeter()+cir[1].getPerimeter());
		//输出面积
System.out.println(rect[0].getArea()+rect[1].getArea()+cir[0].getArea()+cir[1].getArea());
        //使用Arrays.deepToString分别输出上面建立的Rectangle数组与Circle数组
		System.out.println(Arrays.deepToString(rect));
		System.out.println(Arrays.deepToString(cir));
	}
}
//创建长方形类
	class Rectangle{
		private int width;
		private int length;//私有成员变量
		Rectangle(int width,int length)
		{
			this.width=width;
			this.length=length;//获取长和宽
		}
		public int getPerimeter()
		{
			return (width+length)*2;//定义求周长的方法
		}
		public int getArea()
		{
			return width*length;//定义求面积的方法
		}
		@Override
		public String toString() {
			return "Rectangle [width=" + width + ", length=" + length + "]";
		}

	}
	class Circle{
		private int radius;
		Circle(int radius){
			this.radius=radius;
		}
		public int getPerimeter()
		{
			return (int)(2*Math.PI*radius);
		}
		public int getArea()
		{
			return (int)(Math.PI*radius*radius);
		}
		@Override
		public String toString() {
			return "Circle [radius=" + radius + "]";
		}

	}


 对代码的部分知识进行讲解

1.

Rectangle(int width,int length)

        {

            this.width=width;

            this.length=length;

        }

为什么需要写此部分代码,作用何在:

我在网上也没有搜到相关的解释,以下为本人自己的一些想法,勿喷。

我们定义了长方形的类,然后在Main()函数中我们new了新的对象,也就是说,现在在内存空间里,已经分配了部分空间给长方形对象,我们从键盘输入了相应的长和宽,那么,要想将数据传到对象中去,就得调用函数Rectangle(int width,int length)。

2.Rectangle rect[]=new Rectangle[2];

此代码的意思就是new了一个数组,该数组是Rectangle类型的,不属于java基本类型,该数组的长度为2,即数组内存的是两个长方形对象的地址,指向两个长方形对象,而在长方形对象内存地址中,存储着长和宽等信息。

3.@Override

@Override 的作用是:如果想重写父类的方法,比如toString()方法的话,在方法前面加上@Override 系统可以帮你检查方法的正确性。

Override的用法:Override:java.lang.Override是一个marker annotation类型,它被用作标注方法。它说明了被标注的方法重载了父类的方法,起到了断言的作用。

4.System.out.println(Arrays.deepToString(cir));

Arrays.deepToString()主要用于数组中还有数组的情况

发布了5 篇原创文章 · 获赞 1 · 访问量 774

猜你喜欢

转载自blog.csdn.net/vymih/article/details/89925184