Java Programming 5 th edition Chapter 10 Inheritance Programs Part 1 Number 3 on
ID: 3804445 • Letter: J
Question
Java Programming 5th edition Chapter 10 Inheritance Programs
Part 1
Number 3 on page 719 which creates a Point class with 2 instance variables; the xCoordinate and yCoordinate. It should have a default constructor and a values constructor. Also include a set method that sets both attributes, a get method for each attribute, and a method that redefines toString() to print the attributes as follows.
point: (x, y)
Part 2
Do number 4 on page 719 which creates a Circle class that extends the Point class above. It adds the radius, circumference, and area instance variables. It should have a default constructor and a values constructor (circumference and area will be 0.0). Include a set method that sets the coordinates and radius attributes, get methods that get each of the 3 new attributes, and a method that redefines toString() to print all attributes. Also, include methods to calculate the circumference and area of the circle. Use pie = 3.141593. Make sure your methods don’t repeat the code already written in the Point class.
Circumference = 2pier
Area = pier2
Part 3
Do number 5 on page 719 which creates a Cylinder class that extends the Circle class above. It adds the height, surfaceArea, and volume instance variables. It should have a default constructor and a values constructor (circumference and circleArea will be calculated from the Circle class, surfaceArea and volume will be 0.0). Include a set method for the center point coordinates, radius, height, circumference, and circleArea attributes (circumference and circleArea will be calculated from the Circle class), get methods that get each of the 3 new attributes, and a method that redefines toString() to print all attributes. Also, include methods to calculate the surfaceArea and volume of the cylinder. Make sure your methods don’t repeat the code already written in the Circle class.
surfaceArea = 2 * circleArea + circleCircumference * cylinderHeight
volume = circleArea * cylinderHeight
Hints:
1.In the values constructor and the set method you’ll need to call the methods to calculate circumference and area so that they have values to use in calculating the surface area and volume.
2.The methods in the Circle class to calculate circumference and area will need to return those values for use in the Cylinder class.
Explanation / Answer
Here is the class definition for above-mentioned problem-
Part-1
public class Point {
private double xCoordinate;
private double yCoordinate;
public Point() {
this.xCoordinate = 0;
this.yCoordinate = 0;
}
public Point(double xCoordinate, double yCoordinate) {
super();
this.xCoordinate = xCoordinate;
this.yCoordinate = yCoordinate;
}
public double getxCoordinate() {
return xCoordinate;
}
public void setxCoordinate(double xCoordinate) {
this.xCoordinate = xCoordinate;
}
public double getyCoordinate() {
return yCoordinate;
}
public void setyCoordinate(double yCoordinate) {
this.yCoordinate = yCoordinate;
}
@Override
public String toString() {
return "Point [xCoordinate=" + xCoordinate + ", yCoordinate=" + yCoordinate + "]";
}
}
Part-2
public class Circle extends Point {
private double radius;
private double circumference;
private double area;
private static final double PI = 3.141593;
public Circle() {
this.radius = 0.0;
this.circumference = 0.0;
this.area = 0.0;
}
public Circle(double xCoordinate, double yCoordinate, double radius) {
super(xCoordinate, yCoordinate);
this.radius = radius;
this.circumference = caluclateCircumference();
this.area = caluclateArea();
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getCircumference() {
return circumference;
}
public void setCircumference(double circumference) {
this.circumference = circumference;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
public double caluclateArea() {
return PI * radius * radius;
}
public double caluclateCircumference() {
return 2 * PI * radius;
}
@Override
public String toString() {
return "Circle [radius=" + radius + ", circumference=" + circumference + ", area=" + area
+ ", getxCoordinate()=" + getxCoordinate() + ", getyCoordinate()=" + getyCoordinate() + "]";
}
}
Part-3
public class Cylinder extends Circle {
private double height;
private double surfaceArea;
private double volume;
public Cylinder() {
this.height=0.0;
this.surfaceArea = 0.0;
this.volume = 0.0;
}
public Cylinder(double xCoordinate, double yCoordinate, double radius,double height) {
super(xCoordinate, yCoordinate, radius);
this.height = height;
this.surfaceArea = calculateSurfaceArea();
this.volume = calculateVolume();
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getSurfaceArea() {
return surfaceArea;
}
public void setSurfaceArea(double surfaceArea) {
this.surfaceArea = surfaceArea;
}
public double getVolume() {
return volume;
}
public void setVolume(double volume) {
this.volume = volume;
}
@Override
public String toString() {
return "Cylinder [height=" + height + ", surfaceArea=" + surfaceArea + ", volume=" + volume + ", getRadius()="
+ getRadius() + ", getCircumference()=" + getCircumference() + ", getArea()=" + getArea()
+ ", caluclateArea()=" + caluclateArea() + ", caluclateCircumference()=" + caluclateCircumference()
+ ", getxCoordinate()=" + getxCoordinate() + ", getyCoordinate()=" + getyCoordinate() + "]";
}
public double calculateSurfaceArea() {
return 2 * getArea() + getCircumference() * getHeight();
}
public double calculateVolume() {
return getArea() * getHeight();
}
}
//Driverclass for testing
class Driver{
public static void main(String[] args) {
Point point=new Point(0,0);
Circle circle=new Circle(point.getxCoordinate(),point.getyCoordinate(),7.0);
Cylinder2 c=new Cylinder2(point.getxCoordinate(),point.getyCoordinate(),5.0,10);
System.out.println("circle details :"+circle);
System.out.println("cylinder details :"+c);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.