1.计算圆柱体面积
package J5;
/*
* 根据下图实现类。在CylinderTest类中创建Cylinder类的对象
* 设置圆柱的底而半径和高,并输出圆柱的体积。
Circle(圆)
-radius :double
Circle():构造器,将radius属性初始化为1
+setRadius(double radius):void
+getRadius():double
+findArea():double 计算圆的面积
*/
public class Circle {
private double radius;
public Circle(double radius) {
radius = 1;
this.radius = radius;
}
public Circle() {
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double findArea() {
return Math.PI * radius * radius;
}
}
package J5;
/*
* 根据下图实现类。
* 在CylinderTest类中创建Cylinder类的对象,设置圆柱的底而半径和高,并输出圆柱的体积。
* Cylinder(圆柱)
-length:double
Cylinder():构造器,将length属性初始化为1
+setLength(double length):void
+getLength():double
+findVolume():double计算圆柱体积
*/
public class Cylinder extends Circle{
private double length;
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public Cylinder(double length) {
length = 1;
this.length = length;
}
public Cylinder() {
}
public double findVolume() {
return findArea() * getLength();
}
}
package J5;
/*
* 在CylinderTest类中创建Cylinder类的对象,设置圆柱的底而半径和高,并输出圆柱的体积。
*/
public class CylinderTest {
public static void main(String[] args) {
Cylinder c1 = new Cylinder();
c1.setLength(3);
c1.setRadius(2);
double CylinderArea = c1.findVolume();
System.out.println("圆柱面积为" + CylinderArea);
double CircleArea = c1.findArea();
System.out.println("圆的面积" + CircleArea);
}
}
运行示意图
2.单纯练习继承性
package J5;
/*
* (1)定义一个ManKind类,包括
>成员变量int sex和int salary;
>方法void manOrWoman():根据sex的值显示“man”(sex==1)或者“woman”(sex==0);
>方法void employeed():根据salary的值显示“no job”(salary==0)或者“job”(salary!=0)。
*/
public class ManKind {
private int sex;
private int salary;
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public ManKind(int sex, int salary) {
this.sex = sex;
this.salary = salary;
}
public ManKind() {
}
public void manOrWoman() {
if (sex == 1) {
System.out.println("men");
} else if (sex == 0){
System.out.println("woman");
}
}
public void employeed() {
String j = (salary == 0)?"no job":"job";
System.out.println(j);
}
}
package J5;
/*
* (2)定义类Kids继承ManKind,并包括
>成员变量int yearsOld;
>方法printAge()打印yearsOld的值。
*/
public class Kids extends ManKind{
private int yearsOld;
public Kids(int yearsOld) {
this.yearsOld = yearsOld;
}
public Kids() {
}
public void printAge() {
System.out.println(yearsOld);
}
public int getYearsOld() {
return yearsOld;
}
public void setYearsOld(int yearsOld) {
this.yearsOld = yearsOld;
}
}
package J5;
/*
* (3)定义类ManKindAndKids,
* 在类的main方法中实例化Kids的对象someKid,用该对象访问其父类的成员变量及方法。
*/
public class ManKindAndKids {
public static void main(String[] args) {
Kids someKid = new Kids(12);
int yearsOld = someKid.getYearsOld();
System.out.println(yearsOld);
someKid.setSalary(0);
someKid.employeed();
someKid.setSex(0);
someKid.manOrWoman();
}
}
运行结果示意图
3.再次单纯练习继承性
package J5;
/*
* 定义一个学生类Student,它继承自Person类
Person Student
name: String number:long
sex:char int:math
age:int int:english
int:computer
+Person(name:String sex:char age:int) +Student(n:String s:char a:int k:long m:int e:int c:int)
+toString():String
+aver():double
+max():int
+min():int
+toString():String
*/
public class Person {
String name;
char sex;
int age;
public Person() {
}
public Person(String name, char sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String toString() {
return name;
}
}
package J5;
/*
* 定义一个学生类Student,它继承自Person类
Person Student
name: String number:long
sex:char int:math
age:int int:english
int:computer
+Person(name:String sex:char age:int) +Student(n:String s:char a:int k:long m:int e:int c:int)
+toString():String
+aver():double
+max():int
+min():int
+toString():String
*/
public class Student extends Person {
int computer;
long number;
int math;
int english;
public Student(String n, char s, int a, int c, long k, int m, int e) {
this.computer = c;
this.number = k;
this.math = m;
this.english = e;
this.computer = c;
this.english = e;
this.sex = s;
}
public double aver() {
return (computer + math + english) / 3;
}
public int max() {
if (math > computer) {
return (math > english)?math:english;
}else{
return (computer > english)?computer:english;
}
}
public int min() {
if (math < computer) {
return (math < english)?math:english;
}else{
return (computer < english)?computer:english;
}
}
}
package J5;
public class PersonAndStudent {
public static void main(String[] args) {
Student s1 = new Student("moon", '女', 18, 88, 123, 99, 101);
double aver = s1.aver();
System.out.println("平均分为" + aver);
int max = s1.max();
System.out.println("最高分为" + max);
int min = s1.min();
System.out.println("最低分为" + min);
String name = s1.toString();
System.out.println("姓名为" + name);
}
}
运行结果