Homework 9
1.
Circleclass (10 points)Define and implement a class called
Circlewhich has the following instance variable, constructors and methods.Instance Variable:
private double radius- the radius of theCircleobject.Constructors:
public Circle()- default constructor which sets the radius of theCircleobject to one (unit circle).public Circle(double radius)- constructor which sets the radius of theCircleobject to the specified value.Methods:
public double getRadius()- accessor method which returns the radius of theCircleobject.public void setRadius(double radius)- mutator method which sets the radius of theCircleobject to the specified value.public double getArea()- method which calculates and returns the area of theCircleobject.public double getCircumference()- method which calculates and returns the circumference of theCircleobject.public String toString()- method which returns aStringcontaining the radius, circumference and area of theCircleobject 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]Circleobject is equal to the radius of the passed [parameter]Circleobject, 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
getAreaandgetCircumferencemethods must use the respective static methods to calculate the area and circumference.- You must use the value of p stored in the
Mathclass (i.e.Math.PI).
2.
Cylinderclass (10 points)Define and implement a class called
Cylinderwhich has the following instance variables, constructors and methods.Instance Variables:
private double length- the length of theCylinderobject.private Circle circle- stores the radius of theCylinderobject.Constructors:
public Cylinder()- default constructor which sets both the diameter (= radius*2) and length of theCylinderobject to one.public Cylinder(double diameter, double length)- constructor which sets the diameter and length of theCylinderobject to the specified values.Methods:
public double getDiameter()- accessor method which returns the diameter of theCylinderobject.public void setDiameter(double diameter)- mutator method which sets the diameter of theCylinderobject to the specified value.public double getLength()- accessor method which returns the length of theCylinderobject.public void setLength(double length)- mutator method which sets the length of theCylinderobject to the specified value.public Circle getCircle()- accessor method which returns thecircleinstance variable for theCylinderobject.public double getVolume()- method which calculates and returns the volume of theCylinderobject.public double getSurfaceArea()- method which calculates and returns the surface area of theCylinderobject.public String toString()- method which returns aStringcontaining the diameter, length, surface area and volume of theCylinderobject 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]Cylinderobject is equal to the diameter (or radius) and length of the passed [parameter]Cylinderobject, false otherwise.NOTES:
- The diameter of the
Cylinderobject should be stored (as a radius) in thecircleinstance variable, which is aCircleobject!.- Utilize the methods available with the
Circleobject to make implementing the methods for theCylinderclass easier. Most of the methods should be implemented in one line!
3.
Pipeclass (10 points)Define and implement a class called
Pipewhich is derived from theCylinderclass and has the following instance variable, constructor and methods.Instance Variable:
private boolean isThreaded- true if thePipeobject is threaded, false otherwiseConstructor:
public Pipe(double diameter, double length, boolean isThreaded)- constructor which sets the diameter, length and thread state of thePipeobject to the specified values.Methods:
public boolean isThreaded()- method which returns the value of theisThreadedinstance variable.public void thread()- method which sets the value of theisThreadedinstance variable to true.public void getSurfaceArea()- method which calculates and returns the surface area of thePipeobject.public String toString()- method which returns aStringcontaining the diameter, length, surface area, volume and thread state of thePipeobject 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]Pipeobject is equal to the diameter (or radius), length and thread state of the passed [parameter]Pipeobject, 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
getSurfaceAreamethod inherited from theCylinderclass, 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.
PipeTestclass (5 points)Define and implement a class called
PipeTestwhich demonstrates the complete functionality of only thePipeclass.NOTES:
- To demonstrate the functionality of the
toStringmethod, simply pass thePipeobject to theSystem.out.printlnmethod. This will result in thetoStringmethod of the passedPipeobject being called and it's return value being printed to the screen.