Homework 9
1.
Circle
class (10 points)Define and implement a class called
Circle
which has the following instance variable, constructors and methods.Instance Variable:
private double radius
- the radius of theCircle
object.Constructors:
public Circle()
- default constructor which sets the radius of theCircle
object to one (unit circle).public Circle(double radius)
- constructor which sets the radius of theCircle
object to the specified value.Methods:
public double getRadius()
- accessor method which returns the radius of theCircle
object.public void setRadius(double radius)
- mutator method which sets the radius of theCircle
object to the specified value.public double getArea()
- method which calculates and returns the area of theCircle
object.public double getCircumference()
- method which calculates and returns the circumference of theCircle
object.public String toString()
- method which returns aString
containing the radius, circumference and area of theCircle
object in the following format:radius = 4.0 circumference = 25.132741228718345 area = 50.26548245743669public boolean equals(Circle c)
- method which returns true if the radius of the [calling]Circle
object is equal to the radius of the passed [parameter]Circle
object, false otherwise.public static double area(double radius)
- static method calculates and returns the area of a circle with the specified radius.public static double circumference(double radius)
- static method calculates and returns the circumference of a circle with the specified radius.NOTES:
- Both the
getArea
andgetCircumference
methods must use the respective static methods to calculate the area and circumference.- You must use the value of p stored in the
Math
class (i.e.Math.PI
).
2.
Cylinder
class (10 points)Define and implement a class called
Cylinder
which has the following instance variables, constructors and methods.Instance Variables:
private double length
- the length of theCylinder
object.private Circle circle
- stores the radius of theCylinder
object.Constructors:
public Cylinder()
- default constructor which sets both the diameter (= radius*2) and length of theCylinder
object to one.public Cylinder(double diameter, double length)
- constructor which sets the diameter and length of theCylinder
object to the specified values.Methods:
public double getDiameter()
- accessor method which returns the diameter of theCylinder
object.public void setDiameter(double diameter)
- mutator method which sets the diameter of theCylinder
object to the specified value.public double getLength()
- accessor method which returns the length of theCylinder
object.public void setLength(double length)
- mutator method which sets the length of theCylinder
object to the specified value.public Circle getCircle()
- accessor method which returns thecircle
instance variable for theCylinder
object.public double getVolume()
- method which calculates and returns the volume of theCylinder
object.public double getSurfaceArea()
- method which calculates and returns the surface area of theCylinder
object.public String toString()
- method which returns aString
containing the diameter, length, surface area and volume of theCylinder
object in the following format:diameter = 4.0 length = 2.0 surface area = 37.6991118431 volume = 25.1327412287public boolean equals(Cylinder c)
- method which returns true if the diameter (or radius) and length of the [calling]Cylinder
object is equal to the diameter (or radius) and length of the passed [parameter]Cylinder
object, false otherwise.NOTES:
- The diameter of the
Cylinder
object should be stored (as a radius) in thecircle
instance variable, which is aCircle
object!.- Utilize the methods available with the
Circle
object to make implementing the methods for theCylinder
class easier. Most of the methods should be implemented in one line!
3.
Pipe
class (10 points)Define and implement a class called
Pipe
which is derived from theCylinder
class and has the following instance variable, constructor and methods.Instance Variable:
private boolean isThreaded
- true if thePipe
object is threaded, false otherwiseConstructor:
public Pipe(double diameter, double length, boolean isThreaded)
- constructor which sets the diameter, length and thread state of thePipe
object to the specified values.Methods:
public boolean isThreaded()
- method which returns the value of theisThreaded
instance variable.public void thread()
- method which sets the value of theisThreaded
instance variable to true.public void getSurfaceArea()
- method which calculates and returns the surface area of thePipe
object.public String toString()
- method which returns aString
containing the diameter, length, surface area, volume and thread state of thePipe
object in the following format:diameter = 4.0 length = 2.0 surface area = 37.6991118431 volume = 50.2654824574 threaded = truepublic boolean equals(Pipe p)
- method which returns true if the diameter (or radius), length and thread state of the [calling]Pipe
object is equal to the diameter (or radius), length and thread state of the passed [parameter]Pipe
object, false otherwise.NOTES:
- Most, if not all, of the constructor and method implementations should be one line. Take advantage of the inherited methods and parent constructors when applicable.
- You must override the
getSurfaceArea
method inherited from theCylinder
class, since the surface area of a pipe is differnt than that of a cylinder (i.e. a pipe is a hollow cylinder).- Assume the thickness of the pipe is infintesimally small.
4.
PipeTest
class (5 points)Define and implement a class called
PipeTest
which demonstrates the complete functionality of only thePipe
class.NOTES:
- To demonstrate the functionality of the
toString
method, simply pass thePipe
object to theSystem.out.println
method. This will result in thetoString
method of the passedPipe
object being called and it's return value being printed to the screen.