Page 271 - CA_Blue( J )_Class10
P. 271

21 st
                  Some More Programs                                                 Century   #Coding & Computational Thinking
                                                                                       Skills
                               Design a class shape with the following data members and methods:
                  Program 1    class                        :      shape_sphere
                               Data Members
                               double radius, surface_area, volume

                               Method
                               void accept ()               :      Accepts the radius of the sphere
                               void cal_surfacearea()       :      Calculates the surfacearea of the sphere
                                                                                               2
                                                                   (Surface Area of a Sphere = 4πr )
                               void cal_volume()            :      Calculates the volume of the sphere
                                                                                             3
                                                                   (Volume of a Sphere = 4/3×πr )
                               void display ()              :      Prints the surface Area and Volume of the sphere
                   1   import java.util.*;

                   2   class shape_sphere
                   3   {

                   4       double radius,surface_area,volume;
                   5       void accept()

                   6       {

                   7           Scanner sc= new Scanner(System.in);
                   8           System.out.println("Enter Radius :");
                   9           radius=sc.nextInt();

                  10       }

                  11       void cal_surfacearea()
                  12       {

                  13           surface_area    = 4 * 3.142 * radius * radius;
                  14       }

                  15       void cal_volume()
                  16       {

                  17           volume= 4.0/3.0 * 3.142 * Math.pow(radius,3);
                  18       }

                  19       void display()















                                                                                                                       269
                                                                                      Class as the Basis of all Computation  269
   266   267   268   269   270   271   272   273   274   275   276