導航:首頁 > 編程語言 > 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相關的資料

熱點內容
mcs51單片機外部引腳ea 瀏覽:893
蘋果手機怎麼給app給予信用 瀏覽:10
java實型 瀏覽:148
php判斷顯示 瀏覽:695
聯網的單片機 瀏覽:441
安卓錄屏怎麼保存到相冊 瀏覽:350
c語言與單片機 瀏覽:350
tt伺服器是什麼意思 瀏覽:188
賓士app怎麼修改桌面 瀏覽:53
bat演算法面試題 瀏覽:132
因為加密演算法不同 瀏覽:659
會計員app怎麼下載 瀏覽:41
手機小視頻app怎麼刪掉 瀏覽:503
程序員陳一鳴與妹妹 瀏覽:31
個人所得稅app怎麼採集 瀏覽:530
博易主圖指標源碼公式 瀏覽:242
電腦里pdf怎麼打開 瀏覽:489
山東農信app怎麼放貸 瀏覽:565
二年級學不學機器人編程 瀏覽:506
awklinux分割字元串 瀏覽:450