import java.util.Scanner; public class Circle { private double radius; //半径 public Circle() { } public Circle(double radius) { this.radius = radius; } public void getArea() { double s; s=3.1415926*radius*radius; System.out.printf("面积:%-10f",s); } public static void main(String[] args) { Scanner in=new Scanner(System.in); double r; System.out.print("半径:"); r=in.nextDouble(); Circle c=new Circle(r); c.getArea(); } }
import java.util.Scanner; public class Tree { private int ages; //树龄 public Tree() { } public Tree(int ages) { this.ages=ages; } public void grow(int years) { ages+=years; } public void age() { System.out.println("树龄:"+ages); } public static void main(String[] args) { Scanner in=new Scanner(System.in); int a,b; System.out.print("请输入原有树龄:"); a=in.nextInt(); System.out.print("请输入生长的年数:"); b=in.nextInt(); Tree t=new Tree(a); t.grow(b); t.age(); } }
二
三.例题