Guest User

Untitled

a guest
Jun 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. /* Write a Cylinder class whose objects can be characterised by two double values: one to denote the radius and the other to denote the height of a cylinder. a) Provide a constructor without parameters which would initialise the class fields to some default values, and another constructor which would take two parameters and initialise the fields to the values specified by the user. */
  2.  
  3. public class Cylinder {
  4.  
  5.   private double radius; // data field. private so that only stuff in this class can access it
  6.   private double height; // save as above
  7.  
  8.   public Cylinder () {radius = 0.0; height = 0.0;} // constructor with no arguments - note: you don't need to specify a return type for constructors
  9.   public Cylinder (double a, double b) {radius = a; height = b;} // constructor with two arguments
  10.  
  11.   // this is a method (a method is just what a function in a class is called. it's public, so any other class can call it, and it allows other classes to change the data stored in this class.
  12.   // e.g., say in another class you had something like 'Cylinder awesome = new Cylinder();'
  13.   // to change the cylinder radius, you could then go 'awesome.changeRadius(1.0);'
  14.   public void changeRadius (double i) {
  15.     radius = i;
  16.   }
  17.  
  18.   //same as above
  19.   public void changeHeight (double i) {
  20.     height = i;
  21.   }
  22.  
  23. }
Add Comment
Please, Sign In to add comment