Methods in String Class The colored to comment. |
|
Introduction class Circles { double radius; public Circles (double r){ radius = r; } public double Circumference (){ return 2 * Math.PI * radius; } public double Surface (){ return Math.PI * Math.pow(radius,2); } public void Display (String Kind){ System.out.println("\n\t The circle" + Kind + " has the following features: \n\t Radius = " + radius + "\n\t Circumference = " + Circumference () + "\n\t Surface = " + Surface ()); } } public class TestCircles{ public static void main (String[] args){ final int MAX = 7; Circles [] circle = new Circles [MAX]; for (int k = 0; k <= 6; k++){ circle[k] = new Circles (k+1); } int NumCircles = 4; Display (circle, NumCircles); } static void Display (Circles [] circle, int m){ System.out.println ("\n The features of the " + m + " circles are as follows:\n"); for (int i = 0; i < m; i++) circle [i].Display (" of index " + i); System.out.println(); } II. The execution gives: how to build an array: ConstructorName [] ItsName = new ConstructorName [MaxIdex] ConstructorName [index].Methode(); C:\Java>javac TestCircles.java C:\Java>java TestCircles The features of the 4 circles are as follows: The circle of index 0 has the following features: Radius = 1.0 Circumference = 6.283185307179586 Surface = 3.141592653589793 The circle of index 1 has the following features: Radius = 2.0 Circumference = 12.566370614359172 Surface = 12.566370614359172 The circle of index 2 has the following features: Radius = 3.0 Circumference = 18.84955592153876 Surface = 28.274333882308138 The circle of index 3 has the following features: Radius = 4.0 Circumference = 25.132741228718345 Surface = 50.26548245743669 |