資源描述:
《云大java實驗5lab#5(陸歌浩)》由會員上傳分享,免費在線閱讀,更多相關內容在工程資料-天天文庫。
1、云南大學軟件學院實驗報告課程:Java程序設計實驗學期:2012-2013學年第一學期任課教師:陸歌浩專業(yè):軟件工程學號:20101120007序號:姓名:孫金文成績:實驗5ApplyingOOConceptsUsingJava一、實驗目的練習Java中面向對象概念的應用--學會使用現(xiàn)有對象和自定義類。二、實驗內容ProgramCreateaclasscalledCircle.javawithfieldsnamedradius,area,anddiameter.?IncludemethodsnamedsetRadius(),getRadius,andcomputeDiameter(),w
2、hichcalculatesthecircle'sdiameter,andcomputeArea(),whichcomputesthecircle'sarea.?Hint:?diameterofacircleistwiceitsradius.?Hint:?areaofacircleis3.14multipliedbythesquareoftheradius.?CreateaclasscalledTestCircle.javawhosemainmethoddeclares2Circleobjects.?UsingsetRadius()method,assignoneCircleasmall
3、radiusvalueandassignanotheralargerradiusvalue.?CallcomputeDiameter()andcomputeAreaforeachcircleanddisplaytheresults.Outputresultofusingcommandinput"58":Theareaofais78.5Thediameterofais10.0Theareaofbis200.96Thediameterofbis16.0實驗代碼:importjavax.swing.JOptionPane;publicclassTestCircle{publicstaticvo
4、idmain(String[]args){Circlea=newCircle();Circleb=newCircle();doublerad_a,rad_b;rad_a=Double.parseDouble(JOptionPane.showInputDialog("請輸入圓a的半徑:"));//接收圓a的半徑rad_b=Double.parseDouble(JOptionPane.showInputDialog("請輸入圓b的半徑:"));3//接收圓b的半徑a.setRadius(rad_a);//設置圓a的半徑b.setRadius(rad_b);//設置圓b的半徑Stringstr
5、1="Theareaofais";Stringstr2="Thediameterofais";Stringstr3="Theareaofbis";Stringstr4="Thediameterofbis";JOptionPane.showMessageDialog(null,str1+a.computeArea()+""+str2+a.computeDiameter()+""+str3+b.computeArea()+""+str4+b.computeDiameter(),"執(zhí)行結果(孫金文——20101120007)",JOptionPane.PLAIN_MESSAGE);
6、}}classCircle{privatedoubleradius;privatedoublearea;privatedoublediameter;staticfinaldoublePI=3.14;publicvoidsetRadius(doublerad)//設置半徑的方法{radius=rad;}publicdoublegetRadius()//返回半徑的方法{returnradius;}publicdoublecomputeDiameter()//計算直徑的方法{diameter=radius*2;returndiameter;}publicdoublecomputeArea()/
7、/計算面積的方法{area=PI*Math.pow(radius,2);returnarea;}}3運行截圖:3