Write a program in Java. Program: Inheritance A point in the x-y plane is repres
ID: 3729198 • Letter: W
Question
Write a program in Java.
Program: Inheritance
A point in the x-y plane is represented by its x-coordinate and y-coordinate. Design the class Point that can store and process a point in the x-y plane. You should then perform operations on a point, such as showing the point, setting the coordinates of the point, printing the coordinates of the point, returning the x-coordinate, and returning the y-coordinate.
Every circle has a center and a radius. Given the radius, we can determine the circle’s area and circumference. Given the center, we can determine its position in the x-y plane. The center of a circle is a point in the x-y plane. Design the class Circle that can store the radius and center of the circle. Because the center is a point in the x-y plane and you designed the class to capture the properties of a point above, you must derive the class Circle from the class Point. You should be able to perform the usual operations on a circle, such as setting the radius, printing the radius, calculating and printing the area and circumference, and carrying out the usual operations on the center.
Every cylinder has a base and height, where the base is a circle. Design the class Cylinder that can capture the properties of a cylinder and perform the usual operations on a cylinder. Derive this class from the class Circle designed above. Some of the operations that can be performed on a cylinder are as follows: calculate and print the volume, calculate and print the surface area, set the height, set the radius of the base, and set the center of the base.
Write a program to test various operations on a point, circle and cylinder. Your program should be well documented both internally and externally.
Explanation / Answer
Point.java
public class Point {
//Declaring instance variables
private int x,y;
//Zero Argumented constructor
public Point() {
super();
}
//Parameterized constructor
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
//Getter and Setter methods
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
/* toString() method which displays
* the contents of an object inside it
*/
@Override
public String toString() {
return "Point ("+ x + "," + y +")";
}
}
___________________
Circle.java
import java.text.DecimalFormat;
public class Circle extends Point {
//Declaring instance variables
private double radius;
//Zero Argumented constructor
public Circle()
{
this.radius=0.0;
}
//Parameterized constructor
public Circle(int x, int y,double radius) {
super(x, y);
setRadius(radius);
}
//Getter and Setter methods
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
if(radius>=0)
this.radius = radius;
else
{
System.out.println("Invalid.Radius Must be Positive.");
radius=0.0;
}
}
public double calArea()
{
return Math.PI*getRadius()*getRadius();
}
/* toString() method which displays
* the contents of an object inside it
*/
@Override
public String toString() {
DecimalFormat df=new DecimalFormat("#.##");
System.out.println(super.toString());
return "Circle# Radius=" + radius + " Area of Circle ="+df.format(calArea());
}
}
________________________
Cylinder.java
import java.text.DecimalFormat;
public class Cylinder extends Circle {
//Declaring instance variables
private double height;
//Zero Argumented constructor
public Cylinder() {
super();
}
//Parameterized constructor
public Cylinder(int a,int b,double r,double height) {
super(a,b,r);
this.height = height;
}
//Getter and Setter methods
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double calVolume()
{
return Math.PI*getRadius()*getRadius()*getHeight();
}
/* toString() method which displays
* the contents of an object inside it
*/
@Override
public String toString() {
DecimalFormat df=new DecimalFormat("#.##");
System.out.println(super.toString());
return "Cylinder# Height=" + height + " Volume Of the Cylinder ="+df.format(calVolume());
}
}
__________________________
Application.java
import java.util.ArrayList;
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
//Declaring variables
int x,y;
double radius,height;
//Creating an ArrayList Object
ArrayList<Object> arl=new ArrayList<Object>();
/* Scanner class object is used to
* read the inputs entered by the user
*/
Scanner sc=new Scanner(System.in);
//Getting the point coordinates entered by the user
System.out.print("Enter the X coordinate of Point :");
x=sc.nextInt();
System.out.print("Enter the Y coordinate of Point :");
y=sc.nextInt();
//Getting the radius of the circle enterd by the user
System.out.print("Enter radius of the Circle :");
radius=sc.nextDouble();
//Getting the height of the circle entered by the user
System.out.print("Enter Height of the Cylinder :");
height=sc.nextDouble();
/* Creating a Point class object by passing
* the x,y coodinates as arguments
*/
Point p=new Point(x, y);
//Adding the Point class object to the ArrayList
arl.add(p);
/* Creating a Circle class object by passing
* the x,y coodinates and radius as arguments
*/
Circle c=new Circle(x, y, radius);
//Adding the Circle class object to the ArrayList
arl.add(c);
/* Creating a Cylinder class object by passing
* the x,y coodinates, radius,height as arguments
*/
Cylinder cyl=new Cylinder(x, y, radius, height);
//Adding the Cylinder class object to the ArrayList
arl.add(cyl);
//Displaying Each object Information
System.out.println("_____Displaying each Object Information_____");
for(Object o:arl)
{
System.out.println(o.toString());
System.out.println("_________________");
}
}
}
________________________
Output:
Enter the X coordinate of Point :3
Enter the Y coordinate of Point :5
Enter radius of the Circle :4.5
Enter Height of the Cylinder :5
_____Displaying each Object Information_____
Point (3,5)
_________________
Point (3,5)
Circle#
Radius=4.5
Area of Circle =63.62
_________________
Point (3,5)
Circle#
Radius=4.5
Area of Circle =63.62
Cylinder#
Height=5.0
Volume Of the Cylinder =318.09
_________________
___Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.