Need comments added please. import java.util.*; class Point { private double X;
ID: 3665866 • Letter: N
Question
Need comments added please.
import java.util.*;
class Point
{
private double X;
private double Y;
public Point()
{
}
public double GetX()
{
return X;
}
public double GetY()
{
return Y;
}
public void SetX(double NX)
{
X = NX;
}
public void SetY(double NY)
{
Y = NY;
}
public double DistBetween(Point P)
{
return Math.sqrt((this.X-P.GetX())*(this.X-P.GetX()) + (this.Y-P.GetY())*(this.Y-P.GetY()));
}
public double Slope(Point P)
{
if((this.X - P.GetX()) == 0.0)
{
System.out.println("Slope is infinite");
return 0;
}
else
return (this.Y - P.GetY())/(this.X - P.GetX());
}
void print()
{
System.out.println("X: " + X + " Y: " + Y);
}
}
class Circle extends Point
{
double Rad;
public Circle()
{
}
double GetRad()
{
return Rad;
}
void SetRad(double NR)
{
Rad = NR;
}
double GetCircum()
{
return 2 * 3.14 * Rad;
}
double GetArea()
{
return 3.14 * Rad * Rad;
}
public void SetCen(Point P)
{
SetX(P.GetX());
SetY(P.GetY());
}
}
class Cylinder extends Circle
{
double Hght;
public Cylinder(double H, Circle C)
{
Hght = H;
SetRad(C.GetRad());
SetX(C.GetX());
SetY(C.GetY());
}
public double Vol()
{
return GetArea() * Hght;
}
public double SurArea()
{
return GetCircum() * Hght;
}
public void SetHght(double H)
{
Hght = H;
}
public double GetHght()
{
return Hght;
}
}
class Sphere extends Circle
{
public Sphere(double NR)
{
SetRad(NR);
}
public double Vol()
{
return (4/3) * 3.14 * GetRad() * GetRad() * GetRad();
}
public double SurArea()
{
return 4 * 3.14 * GetRad() * GetRad();
}
}
class Cone extends Circle
{
double Hght;
public Cone(double H, double NR)
{
Hght = H;
SetRad(NR);
}
public double Vol()
{
return (GetArea() * Hght) / 3.0;
}
public double SurArea()
{
double Rad = GetRad();
return 3.14 * Rad * (Rad + Math.sqrt(Hght * Hght + Rad * Rad));
}
public void SetHght(double H)
{
Hght = H;
}
public double GetHght()
{
return Hght;
}
}
Explanation / Answer
Code with comments
// importing all the classes of java.util package
import java.util.*;
// defining a class with name Point
class Point
{
// declaring two private member variables of type double
private double X;
private double Y;
// constructor
public Point()
{
}
// getter method for x
public double GetX()
{
// return value of x
return X;
}
// getter method for x
public double GetY()
{
// return value of x
return Y;
}
// Mutator function for x
public void SetX(double NX)
{
// set x to given value
X = NX;
}
// Mutator function for y
public void SetY(double NY)
{
// set y to given value
Y = NY;
}
// function to calculaye distance between given point and calling point
public double DistBetween(Point P)
{
// distance formula is sqrt((x1-x2)^2 + (y1-y2)^2)
return Math.sqrt((this.X-P.GetX())*(this.X-P.GetX()) + (this.Y-P.GetY())*(this.Y-P.GetY()));
}
// function to find slope of line joining given point and calling point
public double Slope(Point P)
{
// slope is calculated as ratio of difference in y co-ordinates to difference in x co-ordinates
// slope is infinite of x co-ordinate of both points are same
if((this.X - P.GetX()) == 0.0)
{
System.out.println("Slope is infinite");
return 0;
}
else
return (this.Y - P.GetY())/(this.X - P.GetX());
}
// function to print x and y co-ordinates of given point
void print()
{
System.out.println("X: " + X + " Y: " + Y);
}
}
// Circle class inherited from Point
class Circle extends Point
{
// new variable for radius
double Rad;
// constructor
public Circle()
{
}
// getter method for radius
double GetRad()
{
return Rad;
}
// mutator method for radius
void SetRad(double NR)
{
Rad = NR;
}
// function to calculate and return circumference
double GetCircum()
{
// circumference is 2*pi*radius
return 2 * 3.14 * Rad;
}
// function to calculate and return area of circle
double GetArea()
{
return 3.14 * Rad * Rad;
}
// function to set given point as center of circle
public void SetCen(Point P)
{
SetX(P.GetX());
SetY(P.GetY());
}
}
// Cylinder class inheriting from Circle
class Cylinder extends Circle
{
// new member variable height
double Hght;
// constructor takes height and circle object
public Cylinder(double H, Circle C)
{
Hght = H;
// get radius of circle and set it to cylinder
SetRad(C.GetRad());
// set x and y co-ordinates of circle and set then to cylinder
SetX(C.GetX());
SetY(C.GetY());
}
// function to calculate and return volume
public double Vol()
{
// area of circle multiplied with height gives volume
return GetArea() * Hght;
}
// function to calculate and return surface area
public double SurArea()
{
// circumference of circle multiplied with height gives surface area
return GetCircum() * Hght;
}
// mutator function for height
public void SetHght(double H)
{
Hght = H;
}
// getter method for height
public double GetHght()
{
return Hght;
}
}
// Sphere class inheriting from Circle
class Sphere extends Circle
{
// constructor with radius as parameter
public Sphere(double NR)
{
SetRad(NR);
}
// function to calculate and return volume
public double Vol()
{
// volume of sphere is 4/3*pi*rad^3
return (4/3) * 3.14 * GetRad() * GetRad() * GetRad();
}
// function to calculate and return surface area
public double SurArea()
{
// Surface area of sphere is 4*pi*rad^2
return 4 * 3.14 * GetRad() * GetRad();
}
}
// Cone class inheriting from Circle
class Cone extends Circle
{
// new member variable height
double Hght;
// constructor with height and radius as parameters
public Cone(double H, double NR)
{
Hght = H;
SetRad(NR);
}
// function to calculate and return volume
public double Vol()
{
// volume of cone is Area of cirle * height / 3
return (GetArea() * Hght) / 3.0;
}
// function to calculate and return surface area
public double SurArea()
{
double Rad = GetRad();
// surface area is pi*rad*(rad+sqrt(ht^2 + rad^2))
return 3.14 * Rad * (Rad + Math.sqrt(Hght * Hght + Rad * Rad));
}
// mutator method for height
public void SetHght(double H)
{
Hght = H;
}
// getter method for height
public double GetHght()
{
return Hght;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.