练习
2. (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)。
(2)定义类Kids继承ManKind,并包括
成员变量int yearsOld;
方法printAge()打印yearsOld的值。
(3)定义类KidsTest,在类的main方法中实例化Kids的对象someKid,用该对象访问其父类的成员变量及方法。
package com.xxx.exer;
public class ManKind {
private int sex;
private int salary;
public ManKind() {
}
public ManKind(int sex, int salary) {
this.sex = sex;
this.salary = salary;
}
public void manOrWoman() {
if(sex == 1) {
System.out.println("man");
}else {
System.out.println("woman");
}
}
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 void employeed() {
if(salary == 0) {
System.out.println("no job");
}else {
System.out.println("job");
}
}
}
package com.xxx.exer;
public class Kids extends ManKind{
private int yearsOld;
public Kids() {
}
public Kids(int yearsOld) {
this.yearsOld = yearsOld;
}
public int getYearsOld() {
return yearsOld;
}
public void setYearsOld(int yearsOld) {
this.yearsOld = yearsOld;
}
public void printAge() {
System.out.println("I am " + yearsOld + " years old.");
}
}
package com.xxx.exer;
public class KidsTest {
public static void main(String[] args) {
Kids someKid = new Kids(12);
someKid.printAge();
someKid.setSalary(0);
someKid.setSex(1);
someKid.employeed();
someKid.manOrWoman();
}
}
3. 根据下图实现类。在CylinderTest类中创建Cylinder类的对象,设置圆 柱的底面半径和高,并输出圆柱的体积。
package com.xxx.exer1;
public class Circle {
private double radius;
public Circle() {
radius = 1.0;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double findArea() {
return Math.PI * radius * radius;
}
}
package com.xxx.exer1;
public class Cylinder extends Circle{
private double length;
public Cylinder() {
length = 1.0;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double findVolume() {
return findArea() * length;
}
}
package com.xxx.exer1;
public class CylinderTest {
public static void main(String[] args) {
Cylinder cy = new Cylinder();
cy.setRadius(2.1);
cy.setLength(3.4);
System.out.println("圆柱的体积为:" + cy.findVolume());
System.out.println("圆柱的底面积为:" + cy.findArea());
}
}
Eclipse debug调试
测试问题代码
package com.xxx.java;
public class DebugTest {
public static void main(String[] args) {
int i = 10;
int j = 20;
System.out.println("i = " + i + ", j = " + j);
DebugTest test = new DebugTest();
int max = test.getMax(i, j);
System.out.println("max = " + max);
}
private int getMax(int k, int m) {
int max = 0;
if (k < m) {
max = k;
} else {
max = m;
}
return max;
}
}
代码没报错,但结果不对
package com.xxx.java;
public class DebugTest1 {
public static void main(String[] args) {
int[] arr = new int[] {1,2,3,4,5