任务1.
class Person {
protected String name;
protected int age;
protected String gender;
public Person() {
}
}
/**
* 2015年6月14日
*
* @author 12052010 TODO 测试继承
*
*/
class Student extends Person {
public Student() {
}
public String getInfo() {
return "\nName: " + this.name + " Age: " + this.age + " Sex: "
+ this.gender;
}
public static void main(String[] args) {
Student[] stus = new Student[4];
for (int index = 0; index < stus.length; index++) {
stus[index] = new Student();
stus[index].name = "stu" + index;
stus[index].age = 20 - index;
stus[index].gender = (0 == (int) (Math.random() * 2)) ? "male"
: "female";
}
System.out.println("\n------------Student's Information-----------");
for (Student stu : stus) {
System.out.println(stu.getInfo());
}
}
}
-------------------------------------------------------------------------
import java.util.Scanner;
class Circle {
private final static double PI = 3.141596d;// π
public double radius;// 半径
public Circle() {
}
public Circle(double r) {
this.radius = r;
}
/**
* 返回圆的面积
*
* @return
*/
public double getArea() {
return this.radius * this.radius * PI;
}
/**
* 返回圆的周长
*
* @return
*/
public double getPerimeter() {
return 2 * this.radius * PI;
}
/**
* 显示圆的信息
*/
public void disp() {
System.out.println("Cicle's radius: " + this.radius + " Area:"
+ getArea() 大旁+ " Perimeter: " + getPerimeter());
}
}
/**
* 2015年6月14日
*
* @author 12052010 TODO 完成测试,望采纳
*
*/
public class Cylinder extends Circle {
private static Scanner input = new Scanner(System.in);
public double height;// 高度
Cylinder(double r, double h) {
super(r);// 设置圆的半径
this.height = h;
}
/* 获取圆柱的高度 */
public double getHeight() {
return this.height;
}
/* 获取圆柱的提职 */
public double getVol() {
return super.getArea() * this.height;
}
/* 显示圆柱的体积 */
public void dispVol() {
System.out.println("\nThis is Cylinder vol: " + getVol());
}
public static void main(String[] args) {
double radius, h;
do {
System.out.print("请输进半径(radius>=0): ");
radius = input.nextDouble();
明唤 System.out.print("请输进高度(height>=0): ");
h = input.nextDouble();
} while (radius < 0 || h < 0);
Cylinder cylinder = new Cylinder(radius, h);
cylinder.disp();// 显示圆的面积
滚槐橡 cylinder.dispVol();// 显示圆柱的体积
}
}