7-1 jmu-Java-03 object-oriented foundation -04- shape - Inheritance

The title describes:

Defines an abstract class Shape
properties: immutable static const double PI, a value of 3.14,
abstract methods: public double getPerimeter (), public double getArea ()

Rectangle and Circle classes inherit from the Shape class.
Rectangle class (property: int width, length), Circle class (property: int radius).
Parameterized constructor Rectangle (int width, int length) , Circle (int radius).
Method toString (Eclipse automatically generated)
prepared double sumAllArea method of calculating the shape of the incoming and return all the objects in the array area and a
calculation method of double sumAllPerimeter and returns the perimeter and shape of the array of all incoming objects.

The main method
4.1 input integer value n, then n different shapes establishment. If the input rect, and then enter the length and width. If the input cir, then re-enter the radius.
4.2 then the output of all the shapes and the perimeter, and area of. And all sample shape information output format. Tip: Use Arrays.toString.
4.3 supertype final output type of each shape using similar shape.getClass () (obtained type), shape.getClass () getSuperclass () ( obtained supertype);.
Note: When the input process using mixed with nextInt nextLine should pay attention to the end of line carriage return line feed problem.
Code draw water. . . . .

import java.util.Scanner;
abstract class Shape{
	final double PI=3.14;
	public abstract double getPerimeter();
	public abstract double getArea();
}
class Rectangle extends Shape{
	int width,length;
	public Rectangle(int r_width,int r_length){
		width=r_width;
		length=r_length;
	}
	public double getPerimeter(){
		return (width+length)*2;
	}
	public double getArea(){
		return (width*length);
	}
    public String toString(){
		return "[width="+width+", length="+length+"]";
	} 
}
class Circle extends Shape{
	int radius;
	public Circle(int c_radius){
		radius=c_radius;
	}
	public double getPerimeter()
	{
		return (2*PI*radius);
	}
	public double getArea()
	{
		return (PI*radius*radius);
	}
    public String toString(){
		return "[radius="+radius+"]";
	} 
}
public class Main{
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		sc.nextLine();
		Shape x[]=new Shape[n];
		double sumAllArea=0,sumAllPerimeter=0;
		for(int i=0;i<n;i++){
			String m=sc.next();
			if(m.equals("rect")){
				int width=sc.nextInt();
                int length=sc.nextInt();
				sc.nextLine();
				x[i]=new Rectangle(width,length);
			}
			else if(m.equals("cir")){
				int radius=sc.nextInt();
				sc.nextLine();
				x[i]=new Circle(radius);
			}
			sumAllArea =sumAllArea + x[i].getArea();
			sumAllPerimeter =sumAllPerimeter + x[i].getPerimeter();
		}
		sc.close();
		System.out.println(sumAllPerimeter);
		System.out.println(sumAllArea);
		System.out.print("[");
		for(int i=0;i<n;i++){
			if(i!=0){
				System.out.print(", ");
			}
			if(x[i] instanceof Rectangle){
				System.out.print("Rectangle ");
				System.out.print(x[i].toString());
			}
			else{
				System.out.print("Circle ");
				System.out.print(x[i].toString());	
			}
		}
		System.out.println("]");
		for(int i=0;i<n;i++){
			if(x[i] instanceof Rectangle){
				System.out.println("class Rectangle,class Shape");
			}
			else{
				System.out.println("class Circle,class Shape");
			}
		}
	}
	
}

Well, almost running about to be all right, if there is optimization program, you can leave Oh, I wish you good luck.

Guess you like

Origin blog.csdn.net/qq_43201999/article/details/90047677