導航:首頁 > 編程語言 > 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三角形梯形和圓形類相關的資料

熱點內容
哪個郵箱發文件快 瀏覽:454
優酷vip共享賬號密碼 瀏覽:473
車震電影韓劇 瀏覽:485
安卓軟體無法觸摸 瀏覽:757
鬼片電影免費1001鬼片電影免費 瀏覽:194
xp單個文件 瀏覽:685
網路電子地圖的主要類型 瀏覽:80
java提交表單亂碼 瀏覽:541
大數據金融的核心數據 瀏覽:141
全部動畫片免費觀看 瀏覽:479
李采潭飾演老師是什麼電影 瀏覽:239
jsp分頁查詢sql 瀏覽:397
歐洲電影三大家 瀏覽:846
如何在天天基金app看持基天數 瀏覽:764
什麼直播app優化 瀏覽:12
文件名陰影 瀏覽:619
小米note圖庫設置在哪個文件夾 瀏覽:604
家教高級課程兩個老師都叫什麼 瀏覽:776
湖畔女演員 瀏覽:298
姜銀慧家庭對抗 瀏覽:130

友情鏈接