導航:首頁 > 編程語言 > java程序100

java程序100

發布時間:2024-01-02 09:16:16

java100行以上源代碼,至少五個class以及一個interface,可以簡單點

下面是一個可能的Java源代碼,它包含了一個介面租沖薯(Shape)和五個類(Circle, Rectangle, Triangle, Square 和 Main)。它的功能是計算不同形狀的面積和周長。
//定義一個介面Shape,有兩判指個抽象方法:getArea()和getPerimeter()interface Shape { double getArea(); double getPerimeter();
}//定義一個類Circle,實現Shape介面class Circle implements Shape { //定義一個私有屬性radius,表示圓的半徑
private double radius; //定義一個公有構造方法,用於初始化radius
public Circle(double radius) { this.radius = radius;
} //實現getArea()方法,返回圓的面積
public double getArea() { return Math.PI * radius * radius;
} //實現getPerimeter()方法,返回圓的周長
public double getPerimeter() { return Math.PI * radius * 2;
}
}//定義一個類Rectangle,實現Shape介面class Rectangle implements Shape { //定義兩個私有屬性width和height,表示矩形的寬度和高度
private double width; private double height; //定義一個公有構造方法,用於初始化width和height
public Rectangle(double width, double height) { this.width = width; this.height = height;
} //實現getArea()方法,返回矩形的面積
public double getArea() { return width * height;
} //實現getPerimeter()方法,返回矩形的周長
public double getPerimeter() { return (width + height) *2;
}
}//定義一個類Triangle,實現Shape介面class Triangle implements Shape { //定義三個私有屬性a,b,c表示三角形的三條邊長
private double a; private double b; private double c; //定義一個公有構造方法,用於初始化a,b,c,並檢查是否滿足三角形條件(任意兩邊之和大於第三邊)
public Triangle(double a, double b, double c) throws Exception{ if (a + b > c && a + c > b && b + c > a) {
this.a = a; this.b = b;
this.c = c;
} else {
throw new Exception("Invalid triangle");
}
} //實現getArea()方法,返回三角形的面積(使用海倫公式)
public double getArea() { //計算半周長p
double p = (a + b + c) /2; //計算並返回面積s(使用Math.sqrt()函數求平方根)
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
} //實現getPerimeter()方法,返回三角形的周長
public double getPerimeter(){ return a + b + c;
}
}//定義一個類Square,繼承Rectangle類,並重寫構造方法和toString()方法class Square extends Rectangle { //重寫構造方法,在調用父類構造方法時傳入相弊者同的參數side作為width和height
public Square(double side){ super(side, side);
} //重寫toString()方法,在原來基礎上加上"Square:"前綴,並只顯示side屬性而不顯示width和height屬性(使用String.format()函數格式化字元串)
@Override
public String toString(){ return String.format("Square: side=%.2f", super.width); /* 或者直接使用super.getPerimeter()/4作為side */
/* return String.format("Square: side=%.2f", super.getPerimeter()/4); */

/* 注意:不能直接訪問super.side屬性,

㈡ [急!]java 求100階乘各位數的和 已有答案,求解釋!

import java.math.BigDecimal; //導入包
public class DigitFactorial //定義類
{
public static void main(String[] args)
{
BigDecimal big = new BigDecimal(1); //聲明一個BigDecimal對象

int i = 1;
while(i <= 100) //在i小於或等於100的時候執行下面的語句
{

big = big.multiply(new BigDecimal(i++)); //每次都用i的階乘的結果取乘以i+1,得到i+1
//的階乘,比如:i=1時, //big=1*2 時, big=1*2*3,最後得到100的階乘
}

String str = big.toString(); //把100的階乘轉換成字元串

System.out.println("Result: " + str); //輸出

int sum = 0;
for(i = 0; i < str.length();i++) //當i在0到str.length()之間時執行,也就是遍歷//str 的每一個字元
{
sum += Integer.parseInt(String.valueOf(str.charAt(i))); //把每一個字元都換成整數再相加,得到所有數的和
}

System.out.println("Sum is: " + sum); //輸出

}
}

BigDecimal和BigInteger用法的區別:
BigDecimal返回的是小數,可以進行四捨五入,用的較多;
BigInteger返回的是整數,用的較少
其他沒多少區別

閱讀全文

與java程序100相關的資料

熱點內容
安卓怎麼測量開機電壓 瀏覽:775
程序員素質大全 瀏覽:953
激戰伺服器不見了怎麼辦 瀏覽:983
安卓如何退回初始系統 瀏覽:952
用什麼手錶能代替app 瀏覽:707
女程序員熬夜體檢 瀏覽:717
解壓故事校園戀愛又煩惱 瀏覽:557
冰箱壓縮機放多久能啟動 瀏覽:173
軟體演算法發明專利 瀏覽:988
旁氏演算法 瀏覽:767
程序員那麼可愛電視劇免費觀看極速版 瀏覽:749
程序員那麼可愛陸漓孩子保住了嗎 瀏覽:566
如何獲取伺服器dns秒解 瀏覽:665
如何破解大黃蜂的加密文件 瀏覽:13
新概念英語第三冊pdf 瀏覽:403
分項詳細估演算法步驟 瀏覽:438
ipad桌面文件夾放大 瀏覽:893
我的世界基岩版怎麼進國際伺服器 瀏覽:518
福州醫院有沒有解壓艙 瀏覽:472
帶pwm的51單片機 瀏覽:918