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

熱點內容
js滑鼠移動圖顯示數據 瀏覽:203
微信網名帶念字 瀏覽:161
提取一個文件夾里的所有圖片 瀏覽:198
三星s5自帶文件管理器 瀏覽:356
如何將文檔數據按規定排序 瀏覽:660
word中的文件如何重命名 瀏覽:272
新基建中的數據中心是指什麼 瀏覽:728
pdf掃描文件插入圖片大小 瀏覽:362
java500錯誤原因 瀏覽:905
sx1278代碼 瀏覽:729
調試配置文件 瀏覽:780
企業培訓app如何選 瀏覽:38
adonet讀取資料庫 瀏覽:717
藍色代碼 瀏覽:500
怎麼進入網站後台管理系統 瀏覽:988
win7如何傳文件 瀏覽:649
微信贈送u盤文件 瀏覽:216
office2016激活文件名 瀏覽:841
列印要輸入文件名 瀏覽:59
虛擬串口用什麼編程 瀏覽:314

友情鏈接