Java MyShape抽象類 面向?qū)ο?/h1>
上傳人:回****
文檔編號(hào):133580784
上傳時(shí)間:2022-08-10
格式:DOC
頁(yè)數(shù):18
大?。?41KB
《Java MyShape抽象類 面向?qū)ο蟆酚蓵?huì)員分享,可在線閱讀,更多相關(guān)《Java MyShape抽象類 面向?qū)ο螅?8頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、Java MyShape抽象類(面向?qū)ο螅? 運(yùn)行成果:(參照代碼一) 1.MyCircle 類 public class MyCircle extends Shape{ private float radius; public MyCircle(int width, int height) { super(width, height); radius=width/2; this.x=3; this.y=45; // TODO Auto-generated constructor stub } @Override public
2、 float getArea() { // TODO Auto-generated method stub return (float) (Math.PI*radius*radius); } @Override public float getGirth() { // TODO Auto-generated method stub return (float) (Math.PI*2*radius); } public String toString(){ return "這是圓,周長(zhǎng)是:"+getGirth()+" 面積是:"+getG
3、irth(); } } 2,MyEllipse類 public class MyEllipse extends Shape{ public MyEllipse(int width, int height) { super(width, height); this.x=3; this.y=85; // TODO Auto-generated constructor stub } @Override public float getArea() { // TODO Auto-generated method stub retu
4、rn (float) (Math.PI *(width+height)/4*(width+height)/4); } @Override public float getGirth() { // TODO Auto-generated method stub return (float) ((float)2*Math.PI*(width+height)/2); } public String toString(){ return "這是一種橢圓,周長(zhǎng)是:"+getGirth()+" 面積是:"+getGirth(); } } 3 MyRec
5、tangle類 public class MyRectangle extends Shape{ public MyRectangle(int width, int height) { super(width, height); this.x=3; this.y=5; // TODO Auto-generated constructor stub } @Override public float getArea() { // TODO Auto-generated method stub return width*height; }
6、 @Override public float getGirth() { // TODO Auto-generated method stub return 2*width*height; } public String toString(){ return "這是矩形,周長(zhǎng)是:"+getGirth()+" 面積是:"+getGirth(); } } 4.MyTriangle類 public class MyTriangle extends Shape{ public MyTriangle(int width, int height)
7、 { super(width, height); this.x=3; this.y=125; // TODO Auto-generated constructor stub } @Override public float getArea() { // TODO Auto-generated method stub return (float) (width+height+Math.sqrt(width*width+height*height)); } @Override public float getGirth() {
8、 // TODO Auto-generated method stub return width*height*1/2; } public String toString(){ return "這個(gè)是三角形,周長(zhǎng)是:"+getGirth()+" 面積是:"+getGirth(); } } 5 Shape抽象類 public abstract class Shape { public int width; public int height; public int x; public int y; public Shape(int w
9、idth, int height){ this.width=width; this.height=height; } public abstract float getArea(); public abstract float getGirth(); } 6.Test類 import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class Test extends JPanel{ private Shape circle;
10、private Shape ellipse; private Shape triangle; private Shape rectangle; public Test(){ JFrame f=new JFrame("形狀家族"); circle=new MyCircle(10,10); ellipse=new MyEllipse(10,10); triangle=new MyTriangle(10,10); rectangle=new MyRectangle(10,10); f.setVisible(true); f.add(this);
11、 f.setSize(600,600); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new Test(); } @Override protected void paintComponent(Graphics g){ int[] xPoints=new int[]{tria
12、ngle.x,triangle.x+20,triangle.x}; int[] yPoints=new int[]{triangle.y,triangle.y,triangle.y-20}; g.drawOval(circle.x, circle.y, circle.width, circle.height); g.drawString(circle.toString(), circle.x+25, circle.y+9); g.drawOval(ellipse.x,ellipse.y, ellipse.width, ellipse.height); g.
13、drawString(ellipse.toString(), ellipse.x+25, ellipse.y+9); g.drawPolygon(xPoints, yPoints, 3); g.drawString(triangle.toString(), triangle.x+25, triangle.y+9); g.drawRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height); g.drawString(rectangle.toString(), rectangle.x+25, recta
14、ngle.y+9); } } 效果:(參照代碼二) 1. 源代碼: 1.1 Myshape package course.java.shape; public abstract class MyShape { public abstract float area(); public abstract float perimeter(); public abstract void display(); } 1.2 MyRectangle package course.java.shape; p
15、ublic class MyRectangle extends MyShape{ private float width,height; public MyRectangle(float width, float height) { super(); this.width = width; this.height = height; } public float area(){ return width*height; } public float perimeter(){ return
16、2*(width+height); } public void display(){ System.out.println("矩形"); System.out.println("長(zhǎng):" + width + ", 寬:" + height); } } 1.3 MyCircle package course.java.shape; public class MyCircle extends MyShape{ private final float PI=3.14f; private float radius;
17、 public MyCircle(float radius) { super(); this.radius = radius; } public float area(){ return PI*radius*radius; } public float perimeter(){ return 2*PI*radius; } public void display(){ System.out.println("圓形"); System.out.println("半徑:
18、" +radius); } } 1.4 MyEllipse package course.java.shape; public class MyEllipse extends MyShape{ private final float PI=3.14f; private float aLong,bShort; public MyEllipse(float a, float b) { super(); this.aLong = a; this.bShort = b; } public float area(){
19、 return 1.0f/4*PI*aLong*bShort; } public float perimeter(){ return PI*bShort+2*(aLong-bShort); } public void display(){ System.out.println("橢圓形"); System.out.println("長(zhǎng)軸長(zhǎng):" + aLong + ", 短軸長(zhǎng):" + bShort); } } 1.5 MyTriangle package course.java
20、.shape; public class MyTriangle extends MyShape{ private float a,b,c; public MyTriangle(float a, float b, float c) { super(); this.a = a; this.b = b; this.c = c; } public float area(){ float p=(a+b+c)/2; return (float)Math.sqrt(p*(p-a)*(p-b)*(p-c));
21、 } public float perimeter(){ return a+b+c; } public void display(){ System.out.println("三角形"); System.out.println("邊長(zhǎng):" + a + ", " + b + ", " + c); } } 1.6 Test測(cè)試程序 package course.java.shape; import java.awt.Color; import java.awt.Graphics;
22、 import javax.swing.JFrame; public class Test extends JFrame{ public Test(){ super("繪圖演示"); setSize(480,300); setVisible(true); } public void paint(Graphics g){ super.paint(g); g.setColor(Color.red); g.drawRect(50,50,6
23、0,40); g.setColor(Color.blue); g.drawOval(150,50,70,40); g.setColor(Color.black); g.drawOval(250,100,60, 60); g.setColor(Color.green); int xValues[]={100,150,200}; int yValues[]={200,180,240}; g.drawPolygon(xValues, yVal
24、ues, 3); } public static void main(String[] args) { // TODO Auto-generated method stub MyShape mc,mr,mt,me; mc=new MyCircle(3); mc.display(); System.out.println("圓形旳面積為:"+mc.area()); System.out.println("圓形旳周長(zhǎng)為:"+mc.perimeter()); mr=new MyRectan
25、gle(4,8); mr.display(); System.out.println("矩形旳面積為:"+mr.area()); System.out.println("矩形旳周長(zhǎng)為:"+mr.perimeter()); mt=new MyTriangle(3,4,5); mt.display(); System.out.println("三角形旳面積為:"+mt.area()); System.out.println("三角形旳周長(zhǎng)為:"+mt.perimeter()
26、); me=new MyEllipse(2,4); me.display(); System.out.println("橢圓形旳面積為:"+me.area()); System.out.println("橢圓形旳周長(zhǎng)為:"+me.perimeter()); JFrame.setDefaultLookAndFeelDecorated(true); Test application=new Test(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } UML圖:
- 溫馨提示:
1: 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 市教育局冬季運(yùn)動(dòng)會(huì)安全工作預(yù)案
- 2024年秋季《思想道德與法治》大作業(yè)及答案3套試卷
- 2024年教師年度考核表個(gè)人工作總結(jié)(可編輯)
- 2024年xx村兩委涉案資金退還保證書
- 2024年憲法宣傳周活動(dòng)總結(jié)+在機(jī)關(guān)“弘揚(yáng)憲法精神推動(dòng)發(fā)改工作高質(zhì)量發(fā)展”專題宣講報(bào)告會(huì)上的講話
- 2024年XX村合作社年報(bào)總結(jié)
- 2024-2025年秋季第一學(xué)期初中歷史上冊(cè)教研組工作總結(jié)
- 2024年小學(xué)高級(jí)教師年終工作總結(jié)匯報(bào)
- 2024-2025年秋季第一學(xué)期初中物理上冊(cè)教研組工作總結(jié)
- 2024年xx鎮(zhèn)交通年度總結(jié)
- 2024-2025年秋季第一學(xué)期小學(xué)語(yǔ)文教師工作總結(jié)
- 2024年XX村陳規(guī)陋習(xí)整治報(bào)告
- 2025年學(xué)校元旦迎新盛典活動(dòng)策劃方案
- 2024年學(xué)校周邊安全隱患自查報(bào)告
- 2024年XX鎮(zhèn)農(nóng)村規(guī)劃管控述職報(bào)告