Question
It is often the case that the classes you write to represent real-world data types will inherit properties (data and methods) from other classes. In
It is often the case that the classes you write to represent real-world data types will inherit properties (data and methods) from other classes. In this assignment, your second task is to write a new class called Sphere and save it in a file named Sphere.java. Class Sphere must extend the class Circle and satisfy the following requirements: The center of the sphere should have an x coordinate, a y coordinate, and a z coordinate. It should also have a radius. All coordinates and dimensions should be of type double. Think carefully about which variables you will inherit from class Circle and, therefore, which new ones will you need to define in class Sphere. 1
There should be two constructors in class Sphere. The first one takes no arguments and sets the center of the sphere at (0.0,0.0,0.0) and the radius of the sphere to 1. The second constructor takes as input in the exact following order: the x, y, and z coordinates and then the radius of the sphere. Your class should override the getCenter method from class Circle so that it returns the 3-dimensional center of the Sphere object (x, y, z) as an array of doubles. Your class should overload the setCenter method from the class Circle so that it takes in x, y, and z coordinates (in that exact order), and sets the center of the Sphere object accordingly. Your method implementation here must utilize the setCenter method already defined in the super class Circle. Your class should override the getArea method from class Circle to make it return the (sur- face) area of the Sphere object instead. Your method implementation here must utilize the getArea computation already defined in the super class Circle. (You may invoke other meth- ods from the super class Circle as well, if needed.) Your class should define the method getVolume which returns the volume of the Sphere object (as a double value). Your method implementation here must utilize the getArea computation already defined in the super class Circle
The code of Class Circle:
// This class represents a circle shape public class Circle { // Instance variables (data members) of class Circle protected double radius; // the radius of the circle protected double x; // the x coordinate of the circle's center protected double y; // the y coordinate fo the circle's center // The default constructor with no argument public Circle(){ // Initializing the values of the instance variables radius = 1.0; x = 0.0; y = 0.0; } // Second constructor with given radius, but origin default public Circle(double r) { radius = r; x = 0.0; y = 0.0; } // Overloaded constructor // Parameter r should be the radius length // Parameter ex should be the x coordinate // Parameter why should be the y coordinate public Circle(double r, double ex, double why) { // TODO [2 pts] Complete this overloaded constructor ... // radius = r; x = ex; y = why; } // Additional getter methods for getX and getY public double getX() { return x; } public double getY() { return y; } // A public getter method for retrieving the radius public double getRadius() { return radius; } // A public getter method for retrieving the center coordinates public double[] getCenter() { double[] c = {this.x, this.y}; return c; } // A public getter method for computing and returning // the area of the circle public double getArea() { return radius * radius * Math.PI; } // A public method you need to write to // compute and return the circumference of the circle public double getCircumference() { // TODO: [2 pts] Add your code here ... // return Math.PI * radius * 2; } // A public method that compares the sizes of two circles: the circle // represented by the current object, and the circle passed as a parameter. // Example: circleA.isBiggerThan(circleB) should return true if circleA // has a larger area than circleB, false otherwise. // NOTE: You may need to modify the parameter list! public boolean isBiggerThan(Circle otherArea) { // TODO: [3 pts] Add your code here ... // return this.getArea()>otherArea.getArea(); } // A public method that takes as input an x coordinate (as a double) // and a y coordinate (a double), and returns true if the (x, y) coordinate // is inside the current circle, false otherwise. // NOTE: You may need to modify the parameter list! public boolean containsPoint(double x, double y) { // TODO: [6 pts] Add your code here ... // double dist = Math.sqrt(Math.pow(x - this.x, 2) + Math.pow(y - this.y, 2)); return dist <= radius; } // A public method named setRadius that sets this object's radius // based on the passed parameter (of type double). // The method should not return anything. public void setRadius(double r) { radius = r; } // TODO: [2 pts] Add your method setRadius here here ... // // A public method named setCenter that sets this object's center. // The method takes two doubles as parameters: ex and why. // It should set the x coordinate of the circle to ex, // and the y coordinate of the circle to why. // The method should not return anything. public void setCenter(double ex, double why) { x = ex; y = why; } // TODO: [2 pts] Add your method here ... // // Overriden method toString which should // return the string representation of this Circle object, as follows: // "This circle is centered at point// with radius " @Override public String toString() { // TODO: [1 pt] return "This circle is centered at point (" + x + "," + y + ") with radius " + radius; } // Override the method equals which is inherited from class Object // (similar to what we did in class Employee), and make it return true if the // two circles have equal areas, false otherwise // Important: Use the @Override annotation // TODO: [2 pts] Add your code here ... // @Override public boolean equals(Object newCircle) { if (newCircle == this) return true; if (!(newCircle instanceof Circle)) return false; Circle c = (Circle) newCircle; return this.getArea() == c.getArea(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started