上机练习1

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

上机练习11题目:使用简单工厂模式设计一个可以创建不同几何形状(如圆形、方形和三角形等)的绘图工具,每个几何图形都要有绘制draw()和擦除erase()两个方法,要求在绘制不支持的几何图形时,提示一个UnSupportedException。importjavax.xml.parsers.*;importorg.w3c.dom.*;importorg.xml.sax.SAXException;importjava.io.*;publicclassClient{publicstaticvoidmain(Stringargs[]){try{tuxingtx;Stringshape=XMLUtiltx.getshape();tx=txFactory.producetx(shape);tx.draw();tx.erase();}catch(Exceptione){System.out.println(e.getMessage());}}}classXMLUtiltx{//该方法用于从XML配置文件中提取形状,并返回该形状名称publicstaticStringgetshape(){try{//创建文档对象DocumentBuilderFactorydFactory=DocumentBuilderFactory.newInstance();DocumentBuilderbuilder=dFactory.newDocumentBuilder();Documentdoc;doc=builder.parse(newFile(configtx.xml));//获取包含图形名称的文本节点NodeListnl=doc.getElementsByTagName(shape);NodeclassNode=nl.item(0).getFirstChild();Stringshape=classNode.getNodeValue().trim();returnshape;}catch(Exceptione){e.printStackTrace();returnnull;}}}interfacetuxing{publicvoiddraw();publicvoiderase();}classtriangleimplementstuxing{publicvoiddraw(){System.out.println(正在绘制三角形);}publicvoiderase(){System.out.println(正在擦除三角形);}}classrectangleimplementstuxing{publicvoiddraw(){System.out.println(正在绘制方形);}publicvoiderase(){System.out.println(正在擦除方形);}}classcircleimplementstuxing{publicvoiddraw(){System.out.println(正在绘制圆形);}publicvoiderase(){System.out.println(正在擦除圆形);}}classtxFactory{publicstatictuxingproducetx(Stringshape)throwsException{if(shape.equalsIgnoreCase(triangle)){System.out.println(绘制三角形);returnnewtriangle();}elseif(shape.equalsIgnoreCase(rectangle)){System.out.println(绘制方形!);returnnewrectangle();}elseif(shape.equalsIgnoreCase(circle)){System.out.println(绘制圆形!);returnnewcircle();}else{thrownewException(对不起,不支持绘制该图形!);}}}2题目:使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,用Java语言实现该场景。现需要增加一个新的Robot类,如果传入参数R,则返回一个Robot对象,对代码进行修改并注意女娲的变化。importjavax.xml.parsers.*;importorg.w3c.dom.*;importorg.xml.sax.SAXException;importjava.io.*;publicclassClient{publicstaticvoidmain(Stringargs[]){try{Peoplep;Stringsex=XMLUtilpeople.getsex();p=Nvwa.producepeople(sex);p.makepeople();}catch(Exceptione){System.out.println(e.getMessage());}}}classXMLUtilpeople{//该方法用于从XML配置文件中提取性别,并返回该性别publicstaticStringgetsex(){try{//创建文档对象DocumentBuilderFactorydFactory=DocumentBuilderFactory.newInstance();DocumentBuilderbuilder=dFactory.newDocumentBuilder();Documentdoc;doc=builder.parse(newFile(configpeople.xml));//获取包含性别的文本节点NodeListnl=doc.getElementsByTagName(sex);NodeclassNode=nl.item(0).getFirstChild();Stringsex=classNode.getNodeValue().trim();returnsex;}catch(Exceptione){e.printStackTrace();returnnull;}}}interfacePeople{publicvoidmakepeople();}classManimplementsPeople{publicvoidmakepeople(){System.out.println(女娲造人——男);}}classWomanimplementsPeople{publicvoidmakepeople(){System.out.println(女娲造人——女);}}classRobotimplementsPeople{publicvoidmakepeople(){System.out.println(女娲造人——机器人);}}classNvwa{publicstaticPeopleproducepeople(Stringsex)throwsException{if(sex.equalsIgnoreCase(M)){System.out.println(女娲造出一个男孩!);returnnewMan();}elseif(sex.equalsIgnoreCase(W)){System.out.println(女孩造出一个女孩!);returnnewWoman();}elseif(sex.equalsIgnoreCase(R)){System.out.println(女孩造出一个机器人!);returnnewRobot();}else{thrownewException(女娲造人出错!);}}}

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功