java大学实用教程第四章例题

例4-1

public class ComputerCircleArea {

public static void main (String args[ ]){ double radius; double area; radius=163.16;

area=3.14*radius*radius;

System.out.printf(\半径是%5.3f的圆的面积:\\n%5.3f\\n\,radius,area); } }

例4-2

class Circle{ }

public class Example4_2 {

public static void main(String[] args) {

// TODO Auto-generated method stub double radius; double getArea(){ }

double area=3.14*radius*radius; return area;

Circle circle; circle=new Circle(); circle.radius=163.16;

double area=circle.getArea();

System.out.printf(\半径是%5.3f的圆的面积:\\n%5.3f\\n\,circle.radius,area); }

}

例4-3 class Lader{

double above,bottom,height;

}

Lader(){}

Lader(double a,double b,double h){ }

public void setAbove(double a){ }

public void setBottom(double b){ }

public void setHeight(double h){ }

double computerAera(){ }

return (above+bottom)*height/2.0; height=h; bottom=b; above=a; above=a; bottom=b; height=h;

public class Example4_1 {

public static void main(String[] args) {

// TODO Auto-generated method stub double area1=0,area2=0; Lader laderOne,laderTwo; laderOne=new Lader();

laderTwo=new Lader(10,88,20); laderOne.setAbove(16); laderOne.setBottom(26); laderOne.setHeight(100); laderTwo.setAbove(300); laderTwo.setBottom(500); area1=laderOne.computerAera(); area2=laderTwo.computerAera();

System.out.println(\的above,bottom和height:\ System.out.println(\的面积:\+area1); System.out.println(\的above,bottom和System.out.println(\的面积:\+area2);

+laderOne.above+\+laderOne.bottom+\+laderOne.height);

height:\+laderTwo.above+\+laderTwo.bottom+\+laderTwo.height);

}

}

System.exit(0);

例4-4 class Lader{ }

public class Example4_4 {

public static void main(String[] args) {

// TODO Auto-generated method stub Lader.bottom=60;

Lader laderOne,laderTwo;

System.out.println(\现在所有Lader对象的bottom都是\+Lader.bottom); laderOne=new Lader(); laderTwo=new Lader();

System.out.println(\的bottom:\+laderOne.getBottom()); System.out.println(\的bottom:\+laderTwo.getBottom()); laderOne.setAbove(11); laderTwo.setAbove(22); double above,height; static double bottom; void setAbove(double a){ }

void setBottom(double b){ }

double getAbove(){ }

double getBottom(){ }

return bottom; return above; bottom=b; above=a;

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4