【程式語言】JAVA的期末上機測驗|物件導向程式設計題目(protected,extends,constructor,static function)
Published: (Updated: )
by .test.java
class Circle {
protected int radius;
protected static double pi;
Circle(){
radius = 3;
pi = 3.14;
}
static Circle bigger(Circle c1 ,Circle c2){
if (c1.area() > c2.area()){
return c1;
}
else {
return c2;
}
}
Circle(int r , double p){
radius = r;
pi = p;
}
double area() {
return radius*radius*pi;
}
}
class Disk extends Circle {
protected int price;
Disk(){}
Disk(int p , int r) {
this.price = p;
radius = r;
}
void show() {
System.out.println("The price for thie disk is " + price + " with radius " + radius);
}
}
public class test {
public static void main(String[] args) {
Circle c1 = new Circle();
Circle c2 = new Circle(4,3.14);
Disk d1 = new Disk(100,10);
Circle b = Circle.bigger(c1,c2);
Circle[] arr = new Circle[5];
arr[0] = c1;arr[1] = c2;arr[2] = d1;
arr[3] = new Circle(2,3.14); arr[4] = new Disk(150,12);
System.out.println("Area for c1 is " + c1.area());
System.out.println("Area for c2 is " + c2.area());
System.out.println("The bigger one is " + b.area());
d1.show();
System.out.println("The total area:" + addArea(arr));
}
static Double addArea(Circle[] ar) {
double sum = 0;
for (int i = 0 ; i < ar.length ; i++) {
sum += ar[i].area();
}
return sum;
}
}