用面向抽象的思想计算柱体的体积。
要求:至少能计算三种不同形状的柱体体积,并总结抽象类的使用方法和特点。
package ch005;
import java.util.Scanner;
public class Text {
public static void main(String[] args) {
System.out.println("请输入所要计算图形形状(1、圆柱体\t 2、三棱柱\t 3、长方体\t 4、结束计算):");
Scanner scan=new Scanner(System.in);
boolean flag=true;
while(flag){
int shape=scan.nextInt();
switch (shape){
case 1:
double dim=scan.nextDouble();
double height=scan.nextDouble();
Cylinder cylinder=new Cylinder(dim,height);
System.out.println("圆柱体的体积为:"+cylinder.callVolume());
break;
case 2:
double length=scan.nextDouble();
double width=scan.nextDouble();
double height2=scan.nextDouble();
Cuboid cuboid=new Cuboid(length,width,height2);
System.out.println("长方体的体积为:"+cuboid.callVolume());
break;
case 3:
double base=scan.nextDouble();
double bottomEdgeheight=scan.nextDouble();
double height3=scan.nextDouble();
ThreePrism threePrism=new ThreePrism(base, bottomEdgeheight,height3);
System.out.println("三棱柱的体积为:"+threePrism.callVolume());
break;
case 4:
flag=false;
System.out.println("您退出了计算!");
break;
default:
System.out.println("暂无算法");
break;
}
}
scan.close();
}
}
package ch005;
public class Cuboid extends Shape{
double length;
double width;
double height;
public Cuboid(double length,double width,double height){
this.height=height;
this.length=length;
this.width=width;
}
@Override
public double callVolume() {
return length*height*width;
}
}
package ch005;
public class Cylinder extends Shape{
double dim;
double height;
public Cylinder(double dim,double height){
this.dim=dim;
this.height=height;
}
@Override
public double callVolume() {
return 3.14*dim*dim*0.5*height;
}
}
package ch005;
public class ThreePrism extends Shape {
double base;
double bottomEdgeHeight;
double height;
public ThreePrism(double base,double bottomEdgeHeight,double height){
this.base=base;
this.bottomEdgeHeight=bottomEdgeHeight;
this.height=height;
}
@Override
public double callVolume() {
return base*bottomEdgeHeight*height/6.0;
}
}
用面向接口的思想的解决问题。
(1)为学校开发这样一个小系统,包含类型:教师、学校、打印机,具体要求如下:
教师以及学校都具有方法:输出详细信息
学校具有属性:打印机,能够通过学校的打印机打印教师或学校的详细信息
系统要具备良好的可扩展性与可维护性
(2)为刚才完成的系统增加一种新的类型:学员(Student),具体要求如下:
学员具有detail方法,负责输出学员详细信息
能够通过学校的打印机打印学员的详细信息
系统要具备良好的可扩展性与可维护性
(3)升级上述的学校系统,要求:
打印机有多种类型,比如:黑白打印机、彩色打印机等
学校可能配备其中任意一款打印机,负责打印教师、或者学校的详细信息
系统要具备良好的可扩展性与可维护性
要求:分三步走,继而完成整个程序。总结接口的使用方法和特点。
package ttt;
interface Introduceable {
public String detail();
}
interface PrinterFace {
public void print(String content);
}
public class AccpTest {
public static void main(String[] args){
//创建学校实例
AccpSchool3 school=new AccpSchool3();
//为学校配备黑白打印机
school.setPrinter(new BlackPrinter());
school.print(school);
//为学校配备彩色打印机
school.setPrinter(new ColorPrinter());
school.print(school);
}
}
class AccpSchool3 implements Introduceable{
private PrinterFace printer;//打印机
public void setPrinter(PrinterFace p){
this.printer=p;
}
public String detail(){
return"这里是学校";
}
public void print(Introduceable intro){
printer.print(intro.detail());
}
}
class BlackPrinter
implements PrinterFace{
public void print(String content){
System.out.println("黑白打印:");
System.out.println(content);
}
}
class ColorPrinter
implements PrinterFace{
public void print(String content){
System.out.println("彩色打印:");
System.out.println(content);
}
}
class Printer {
public void print(String content){
System.out.println("开始打印:");
System.out.println(content);
}
}
class School2
implements Introduceable{
private Printer printer = new Printer();
//输出学校详细信息
public String detail(){
return"这里是学校";
}
public void print(Introduceable intro){
printer.print(intro.detail());
}
}
class Teacher2
implements Introduceable{
public String detail(){
return "本人是教师";
}
}