蒲 作业 工厂

package Test613;
import java.util.Scanner;

interface Shaped{//图形类哈哈
double getArea();
}

class Rectangle implements Shaped {//长方形类哈哈
double width;
double length;

public Rectangle(double width,double length) {
	this.width = width;
	this.length = length;
}

 
public double getArea() {
	// TODO Auto-generated method stub
	return width*length;
}

}

class Square2 extends Rectangle{//正方形类哈哈

public Square2(double width) {
	super(width, width);
	// TODO Auto-generated constructor stub
}
public double getArea() {
	return width* width;
}

}

class Triangle implements Shaped{//三角形类哈哈
double length1;
double length2;
double length3;
public Triangle(double length1,double length2,double length3) {
this.length1 = length1;
this.length2 = length2;
this.length3 = length3;
}
public double getArea() {
double num = (length1+length2+length3)/2;
return Math.sqrt(num(num-length1)(num-length2)*(num-length3));
}
}

class Prism {
Shaped shape;
double hight;
public Prism(Shaped shape,double hight) {//棱柱类哈哈
this.hight = hight;
this.shape = shape;
}
public double getVolumn() {
return shape.getArea()*hight;
}
public void setShape(Shaped shape) {
this.shape = shape;
}
}

class Circle implements Shaped{//圆类哈哈
double r;
final double PI=3.14;
Circle(double r){
this.r=r;
}
public double getArea(){
return PIrr;
}
}

class Factory{//工厂类哈哈哈
Shaped shape;
char gc;
Factory(char ch){
this.gc = ch;
}
public Shaped getShape(){
switch(gc){
case 't':shape=new Triangle(1,2,3);break;
case 'r':shape=new Rectangle(4,5);break;
case 's':shape=new Square2(6);break;
case 'c':shape=new Circle(7);break;
}
return shape;
}
public char getgc(){
return gc;
}
public void setShape(Shaped shape){
this.shape=shape;
}
public void setCh(char ch){
this.gc=ch;
}
}

public class tt {//主函数哈哈

public static void main(String[] args) {
	for(int i = 0;i<4;i++) {
		Scanner sc = new Scanner(System.in);
		String str = sc.next();
		char ch = str.charAt(0);
		Factory fa=new Factory(ch);
		Prism pri=new Prism(fa.getShape(),6);
		pri.setShape(fa.getShape());
		double vol=pri.getVolumn();
		System.out.println(vol);
	}

}
原文地址:https://www.cnblogs.com/papapa613/p/11674663.html