導航:首頁 > 編程語言 > java三角形梯形和圓形類

java三角形梯形和圓形類

發布時間:2024-04-24 03:44:52

Ⅰ 在java中創建一個父類-圖形類,用draw方法。 分別在創建3個子類:三角形、矩形、圓形

packagecom.;

/**
*抽象圖形
*@authorGERRARD
*/
abstractclassGraph{

abstractvoiddraw();
}

/**
*三角形
*@authorGERRARD
*/
classTriangleextendsGraph{

@Override
voiddraw(){
System.out.println("三角形");
}
}

/**
*矩形
*@authorGERRARD
*/
classRectangleextendsGraph{

@Override
voiddraw(){
System.out.println("矩形");
}
}

/**
*圓形
*@authorGERRARD
*/
classRoundextendsGraph{

@Override
voiddraw(){
System.out.println("圓形");
}
}
packagecom.;

/**
*測試類
*@authorGERRARD
*/
publicclassTestGraph{

publicstaticvoidmain(String[]args){

Graphg1=newTriangle();
Graphg2=newRectangle();
Graphg3=newRound();
g1.draw();
g2.draw();
g3.draw();
}

}

Ⅱ 用java編程。利用多態編程創建一個Square類,實現求三角形,正方形和圓形的面積

寫了個長方形的類 其他的照葫蘆畫瓢好了
^_^
public class Square
{
private double width;
private double height;

public Square()
{
width=0.0;
height=0.0;
}
public double getWidth()
{return width;}

public void setWidth(double newwidth)
{width=newwidth;}

public double getHeight()
{return height;}

public void setHeight(double newheight)
{height=newheight;}

public double computeArea()
{return width*height;}
}

public class TestSquare
{
public static void main(String[] args)
{
Square t=new Square();
t.setWidth(1);
t.setHeight(2);
System.out.println("width="+t.getWidth());
System.out.println("height="+t.getHeight());
System.out.println("area="+t.computeArea());
}
}

Ⅲ 1.求解用java寫(如三角形,矩型,圓)的的周長,面積,要求用到繼承,多態,抽象類,介面,內部類等。

//抽象的形狀類
public abstract class Shape{ }

//介面
public interface IDisplay{
void display(); //顯示圖形的基本信息
double getArea(); //計算面積
double getGirth(); //計算周長
}

//三角形類
public class Triangle extends Shape implements IDisplay{
protected double a;
protected double b;
protected double c;

public Triangle(double a, double b, double c){
this.a = a; this.b = b; this.c = c;
}

@Override public double getArea() {
double s = (a + b + c) / 2;
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}

@Override public double getGirth() {
return this.a + this.b + this.c;
}

@Override public void display() {
System.out.println("三角形");
System.out.println("邊長:" + a + ", " + b + ", " + c);
}
}

//矩形類
public class Rectangle extends Shape implements IDisplay {
protected double width; protected double height;

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

@Override public double getArea() {
return this.width * this.height;
}

@Override public double getGirth() {
return 2 * ( this.width + this.height);
}

@Override public void display() {
System.out.println("矩形");
System.out.println("寬:" + this.width + ", 高:" + this.height);
}
}

//圓類
public class Circle extends Shape implements IDisplay {
protected double radius;

public Circle(double radius){
this.radius = radius;
}

@Override public double getArea() {
return Math.PI * this.radius * this.radius;
}

@Override public double getGirth() {
return 2 * Math.PI * this.radius;
}

@Override public void display() {
System.out.println("圓");
System.out.println("半徑:" + this.radius);
}
}

Ⅳ JAVA編程題:設計三個圖形類

真不知道這是考寫代碼還是考數學。
給你一個思路吧,定義一個抽象類表示專圖形,有顏色屬屬性、粗細屬性、求面積方法、比較大小的方法。
然後在定義一個類表示三角形,繼承這個抽象類,三角形類有三個屬性,分別表示它的三個頂點坐標。
也定義一個類表示矩形,繼承抽象類,它有兩個屬性,分別表示它左上角和右下角的坐標。
再定義一個類表示圓形,它有兩個屬性,分別表示圓心和圓上任一點的坐標。

閱讀全文

與java三角形梯形和圓形類相關的資料

熱點內容
蘋果6s和蘋果7哪個更好 瀏覽:453
iphone4s相當於安卓 瀏覽:520
如何把qq文件傳入微信 瀏覽:192
德陽怎樣做網站多少錢 瀏覽:353
python判斷文件創建 瀏覽:646
給pdf文件編輯目錄 瀏覽:684
手機qq名字修改不了 瀏覽:114
華為手機怎麼看地圖數據 瀏覽:895
怎麼壓縮文件並且安裝 瀏覽:926
三國淘app什麼時候上線 瀏覽:218
微信朋友圈佔位白圖 瀏覽:662
打開移動數據網速慢為什麼 瀏覽:67
微信連接不了網路連接失敗 瀏覽:2
網頁代碼庫 瀏覽:204
南陽違法app有哪些 瀏覽:341
qq2012日本版60閃退 瀏覽:840
蘋果6底部屏幕與機身裂開 瀏覽:57
怎麼損壞電腦系統文件 瀏覽:458
橫斷面研究如何統計數據 瀏覽:223
斗戰神30級以後怎麼升級快 瀏覽:390

友情鏈接