Shape类

(1)Shape类:抽象方法setColor(String color)方法,用来给图形设置颜色。

抽象方法showShapeInfo()方法,用来显示图形的详细信息。

(2)Shape2D类:计算面积的方法area(),计算周长的方法perimeter()。

(3)Shape3D类:计算表面积方法surfaceArea(),计算体积方法volume()。

(4)分别创建Rectangle、Circle、Box、Sphere类。

(5)在测试类TestShape中

创建Rectangle、Circle、Box、Sphere类对象;

对长方形、圆形;立方体、球体各对象的属性进行初始化;

输出各对象面积、周长、表面积、体积等详细信息。

 

抽象类Shape2D以及Shape3D,继承抽象父类Shape;

普通类Circle类和Rectangle类,继承Shape2D;

普通类Box类和Sphere类,继承Shape3D。

 

package Shape;

public abstract class Shape {
	
	public String color = "white";
	
	public abstract String getColor();
	//setColor()用来给图形设置颜色;
	public abstract void setColor(String color);
	//showShapeInfo()方法,用来显示图形的详细信息;
	public abstract void showShapeInfo();
	
}

package Shape;

public abstract class Shape2D extends Shape {
	
	//计算面积的方法;
	public abstract double area();
    //计算周长的方法perimeter;
	public abstract double perimeter();
	//多态
	void show() {
		System.out.println("Shape2D.show()");
	}
	
}

package Shape;

public abstract class Shape3D extends Shape {
	
	//计算表面积方法
	public abstract double sarea();
	//计算体积方法
	public abstract double volume();
	
}

package Shape;

public class Circle extends Shape2D {
	public static double radius ;
	final double PI = 3.14;
	
	public Circle() {//默认无参方法
		this(0,"white");
	}
	
	//圆形方法
	public Circle(double initRadius,String initColor) {
		radius = initRadius;
		color  = initColor;
	}
	
	public String getColor() {
		return color;
	}
	
	public void setColor(String newcolor) {
		color  = newcolor;
	}
	
	public  double getRadius() {
		return radius;
	}
		
	public void setRadius(double newRadius) {
		radius = newRadius;
	}
				
	//圆形周长方法
	public double perimeter() {
		return (2*PI*radius);
	}
		
	//圆形面积方法
	public  double area() {
		return (PI*radius*radius);
	}
	
	//输出圆形信息
	public void showShapeInfo() {
		System.out.println("圆形周长是"+perimeter()+
    			"圆形面积是"+area()+"圆形的颜色是"+color);
	}
	//多态
	void show() {
		System.out.println("Circle.draw()");
    }
	
}

package Shape;

public class Rectangle extends Shape2D {
	
	private double length;
	private double width;
	public Rectangle() {//默认无参方法
		this(0,0,"white");
	}
	
	//长方形方法,参数有3,为长、宽、颜色;
	public Rectangle(double initLength, double initWidth,String initColor) {
		length = initLength;
		width  = initWidth;
		color  = initColor;
	}
	
	//正方形方法,参数有2,为边长,颜色;
	public Rectangle(double initLength,String initColor) {
		length = initLength;
		color  = initColor;
	}
			
	public String getColor() {
		return color;
	}
	
	public void setColor(String newcolor) {
		color  = newcolor;
	}
	
	public double getLength() {
		return length;
	}
	
	public void setLength(float newLength) {
		length = newLength;
	}
	
	public double getWidth() {
		return length;
	}
	
	public void setWidth(float newWidth) {
		width = newWidth;
	}
	
	//长方形的周长方法
	public double perimeter() {
		return (2*length+2*width);
	}
		
	//长方形的面积方法
	public double area() {
		return (length*width);
	}
	
	//正方形周长方法
	public double perimeter(double newLength) {
		return (4* length);
	}
		
	//正方形面积方法
	public double area(double newLength) {
		return ( newLength* newLength);
	}
	
	//输出长方形信息
	public void showShapeInfo() {
		System.out.println("长方形的周长是"+perimeter()+
    			"长方形面积是"+area()+"长方形的颜色是"+this.color);
	}
	
	//输出正方形信息、重载
	public void showShapeInfo(double newLength) {
		System.out.println("正方形的周长是"+perimeter( newLength)+
    			"正方形面积是"+area(newLength)+"正方形的颜色是"+this.color);
	}
	
	//多态
	void show() {
		System.out.println("Rectangle.draw()");
    }

}

package Shape;

public class Box extends Shape3D {
	
	//立方体的长宽高
	private double length;
	private double width;
	private double height;
	
	//默认无参方法
	public Box() {
		this(0,0,0,"white");
	}
	//长方体方法,参数有4,长、宽、高、颜色;
	public Box(double initLength,double initWidth, 
			double initHeight,String initColor){
		
		length = initLength;
		width  = initWidth;
		height = initHeight;
		color  = initColor;
	}
	
	//正方体方法,参数有2,边长、颜色;
	public Box(double newLength,String newColor){
		length = newLength;
		color  = newColor;
	}
	
	public String getColor() {
		return color;
	}
	
	public void setColor(String newColor) {
		color = newColor;
		
	}
	
	public double getLength() {
		return length;
	}

	public void setLength(double newLength) {
			length = newLength;
	}

	public double getWidth() {
		return width;
	}

	public void setWidth(double newWidth) {
		width = newWidth;
	}

	public double getHeight() {
		return height;
	}

	public void setHeigth(double newHeigth) {
		height = newHeigth;
	}
	
	//长方体表面积方法
	public double sarea() {
		return (length*width+length*height+width*height);
	}
	
	//正方体表面积方法
	public double sarea(double newLengtht) {
		return (length*length*6);
	}
	
	//长方体体积方法、重写
	public double volume() {
		return (length*width*height);
	}
	
	//正方体体积方法、重载
	public double volume(double newlength) {
		return (length*length*length);
	}
	
	//输出长方体信息
	public void showShapeInfo() {
		System.out.println("长方体的表面积是"+sarea()+
    			"长方体的体积是"+volume()+"长方体的颜色是"+this.color);
	}
	
	//输出正方体信息、重载
	public void showShapeInfo(double newLength) {
		System.out.println("正方体的表面积是"+sarea(newLength)+
    			"正方体面积是"+volume(newLength)+"正方体的颜色是"+this.color);
	}

}

package Shape;

import java.text.DecimalFormat;

public class Sphere extends Shape3D {
	private double bradius;
	private double height;
	private double l;
	final double PI = 3.14;
	
	//默认无参方法
	public Sphere() {
		this(0,0,"white");
	}

	//圆柱方法,参数有3,底面半径、高、颜色;
	public Sphere(double initBradius, double initHeight, String initColor) {
		bradius = initBradius;
		height  = initHeight;
		color   = initColor;
	}
	
	//圆锥方法,参数有4,底面半径、高、、母线、颜色;
	public Sphere(double initBradius,double initHeight,
			double initL, String initColor) {
		
		bradius = initBradius;
		height  = initHeight;
		     l  = initL;
		color   = initColor;
	}

	public String getColor() {
		return color;
	}
	
	public void setColor(String newColor) {
		color = newColor;
	}

	public double getBradius() {
		return bradius;
	}

	public void setBradius(double newBradius) {
		bradius = newBradius;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double newHeight) {
		height = newHeight;
	}
	
	public double getl() {
		return l;
	}

	public void setl(double newL) {
		height = newL;
	}
	
	//圆柱表面积方法
	public double sarea() {
		return(bradius*bradius*PI*2+2*PI*bradius*height);
	}
	
	//圆锥表面积方法
	public double sarea(double newBradius, double newL) {
		return (bradius*bradius*PI+PI*bradius*l);
	}
	
	//圆柱体积方法
	public double volume() {
		return(bradius*bradius*PI*height);
	}
	
	//圆锥体积方法
	public double volume(double newBradius, double newHeigth) {
		return(bradius*bradius*PI*height/3);
	}
	
	//输出圆柱信息
	public void showShapeInfo() {
		System.out.println("圆柱的表面积是"+sarea()+
    			"圆柱的体积是"+volume()+"圆柱的颜色是"+this.color);
	}
	
	//输出圆锥信息
	public void showShapeInfo(double newBradius,double newL, double newHeigth) {
		System.out.println("圆锥体的表面积是"+sarea(newBradius,newHeigth)+
    			"圆锥体体积是"+new DecimalFormat("0.00")
    			.format(volume(newBradius,newHeigth))+"圆锥体的颜色是"+this.color);
	}
	
}

package Shape;

//scanner方法需要的头文件
import java.util.Scanner;

public class TestShape{
	
	@SuppressWarnings("resource")
	public static void main(String[] args) {	
		while(true) {
	        //创建键盘录入对象
	        Scanner in=new Scanner(System.in);
	        System.out.println("----------------------------\n"
	        		        + "1.圆形 2.长方形 3.正方形 4.长方体 "
	        		        + "5.正方体 6.圆柱 7.圆锥 8.退出\n"
	        		        + "----------------------------\n"
	        		        + "请选择服务:");
	        
	        //读取int类型值,进入Switch Case语句;
			int number=in.nextInt();
			switch(number) {
			
			    case 1:
			    	try {
			    		
			    		//从键盘输入圆的信息
			    		System.out.print("请输入圆形半径和颜色:");
			    		double newRadius = in.nextDouble();
			    		String newColor = in.next();
			    		
			    		if (newRadius<=0) {
			    			System.out.println("不能小于0");
			    		}
			    		else {
			    			//创建Circle对象;
			    			Circle c =new Circle (newRadius,newColor);
			    			//通过调用showShapeInfo()输出详细信息;
			    			c.showShapeInfo();
			    			//多态
			    			Shape2D a = new Circle();  // 向上转型
			    			a.show();
			    			}
			    		}
			    			
			    	catch (java.util.InputMismatchException e) {
			    		System.out.println("输入错误,请重新输入");
			    		}
			    	break;
			    	
			    case 2:
			    	try {
			    	//从键盘输入长方形的信息;
				    	System.out.print("请输入长方形的长、宽和颜色:");
				    	double newLength = in.nextDouble();
				    	double newWidth = in.nextDouble();
			    	    String newColor = in.next();
			    	    
			    	    if(newLength <=0 ||newWidth <=0) {
			    	    	System.out.println("输入有误");
			    	    }
			    	    else {
			    	    	if (newLength==newWidth) {
			    	    		System.out.println("这是一个特殊的长方形");
			    	    		Rectangle squ = new Rectangle(newLength,newColor);
			    	    		//调用printInfo()输出详细信息。
			    	    		squ.showShapeInfo(newLength);
			    	    	}
			    	    	else {
			    	    		//创建Rectangle类对象:长方形;
			    	    		Rectangle rec = new Rectangle(newLength,newWidth,newColor);
			    	    		//通过调用showShapeInfo()输出详细信息;
			    	    		rec.showShapeInfo();
			    	    		//多态
			    	    		Shape2D b = new Rectangle();  // 向上转型
			    	    		b.show();
			    	    	}
			    	    }
			    	 }
			    	catch (java.util.InputMismatchException e) {
						System.out.println("输入错误,请重新输入");
					}
			    	break;  
			    	
			    case 3:
			    	try {
			    		//从键盘输入正方形的信息;
			    		System.out.print("请输入正方形边长和颜色:");
			    		double newLength = in.nextDouble();
			    		String newColor = in.next();
			    		
			    		if(newLength <=0 ) {
			    	    	System.out.println("输入有误");
			    	    }
			    		else {
			    			//创建Rectangle类对象:正方形
			    			Rectangle squ = new Rectangle(newLength,newColor);
			    			//调用printInfo()输出详细信息。
			    			squ.showShapeInfo(newLength);
			    		}
			    	}
			    	catch (java.util.InputMismatchException e) {
			    		System.out.println("输入错误,请重新输入");
			        }
			    	break;
			    	
			    case 4:
			    	try {
			    		//从键盘输入长方体的信息;
			    		System.out.print("请输入长方体长、宽、高、颜色:");
			    		double newLength = in.nextDouble();
			    		double newWidth = in.nextDouble();
			    		double newHeight = in.nextDouble();
			    		String newColor = in.next();
			    		
			    		if(newLength <=0 ||newWidth <=0 ||newHeight <=0) {
			    	    	System.out.println("输入有误");
			    	    }
			    		else {
			    			//创建Box类对象:长方体;
			    			Box cubo = new Box(newLength,newWidth,newHeight, newColor);
			    			//通过调用showShapeInfo()输出详细信息;
			    			cubo.showShapeInfo();
			    			}
			    	}
			    	catch (java.util.InputMismatchException e) {
						System.out.println("输入错误,请重新输入");
					}
			    	break;  
			    	
			    case 5:
			    	try {
			    		//从键盘输入正方体的信息;
			    		System.out.print("请输入正方体边长和颜色:");
			    		double newLength = in.nextDouble();
			    		String newColor = in.next();
			    		
			    		if(newLength <=0 ) {
			    	    	System.out.println("输入有误");
			    	    }
			    		else {
			    			//创建Box类对象:正方体;
			    			Box cube = new Box(newLength, newColor);
			    			//通过调用showShapeInfo()输出详细信息;
			    			cube.showShapeInfo(newLength);
			    		}
			    	}
			    	catch (java.util.InputMismatchException e) {
						System.out.println("输入错误,请重新输入");
					}
			    	break; 
			    	
			    case 6:
			    	try {
			    		//从键盘输入圆柱的信息;
			    		System.out.print("请输入圆柱半径、高、颜色:");
			    		double newBradius = in.nextDouble();
			    		double newHeight = in.nextDouble();
			    		String newColor = in.next();
			    		
			    		if(newBradius <=0 || newHeight <=0) {
			    	    	System.out.println("输入有误");
			    	    }
			    		else {
			    			//创建Sphere类对象:圆柱;
			    			Sphere cyl = new Sphere(newBradius, newHeight, newColor);
			    			//通过调用showShapeInfo()输出详细信息;
			    			cyl.showShapeInfo();
			    		}
			    	}
			    	catch (java.util.InputMismatchException e) {
						System.out.println("输入错误,请重新输入");
					}
			    	break; 
			    	
			    case 7:
			    	try {
			    		//从键盘输入圆锥的信息;
			    		System.out.print("请输入圆锥半径、母线、高、颜色:");
			    		double newBradius = in.nextDouble();
			    		double newL = in.nextDouble();
			    		double newHeigth = in.nextDouble();
						String newColor = in.next();
						
						if(newBradius <=0 || newL <= 0 || newHeigth <=0) {
			    	    	System.out.println("输入有误");
			    	    }
						else {
							//创建Sphere类对象:圆锥;
							Sphere cone = new Sphere(newBradius, newL, newHeigth,newColor);
							//通过调用showShapeInfo()输出详细信息;
							cone.showShapeInfo(newBradius, newL, newHeigth);
			    		}
			    	}
			    	catch (java.util.InputMismatchException e) {
						System.out.println("输入错误,请重新输入");
					}
			    	break; 
			    	
			    case 8:
			    	
			    	System.out.print("已退出");
			    	System.exit(0);
			}
	    }
	}
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值