Now write a Demo class which contains main method in order to demonstrate the behavior of all classes.
- Write a class Point with instance variables as: xCoordinate(int) and yCoordinate(int). Write public instance methods (getters & setters) for each instance variable. Now, write a class Circle with private instance variables as radius and Pi (value is 3.14). Write public setter, getter for radius, Area method for calculating Area, Draw method for radius.
- Formula for calculating area=pi*r^2
- Formula for calculating radius= sqrt (xCordinate^2+yCordinate^2)
- Extend a Circle class from Point class such as Class Circle extends Point
- Write a Default constructor in Circle class that sets radius, xCoordinate & yCoordinate to some default value e.g. -1;
- Write a Parameterized constructor in Circle class which takes xCoordinate, yCoordinate and radius as a parameter. E.g.
Circle (int xCoordinate, int yCoordinate, double radius) {
this.xCoordinate= xCoordinate;
this.yCoordinate= yCoordinate;
this.radius=radius;
}
Now write a ColoredCirlce class with private instance variable as color (String) and public setter & getters. Extend the ColoredCircle from Circle and do the following operations.
- Write a default constructor that sets radius, xCoordinate, yCoordinate & color to some default value e.g. -1 or “empty”;
- Write a parameterized constructor that takes radius, xCoordinate, yCoordinate & color as a parameter and initialize instance variables (same as point c).
Now write a Demo class which contains main method in order to demonstrate the behavior of all classes.
- Call parameterized constructor of Circle class as: Circle circle=new Circle(2,3,4.5);
- Print attributes and behavior of circle as:
System.out.println(circle.getxCordinate());
System.out.println(circle.getyCordinate());
System.out.println(circle.getRadius());
System.out.println("Area="+circle.Area());
System.out.println("Drawing circle="+circle.Draw());
- Call parameterized constructor of ColoredCircle class as: ColoredCircle cCircle=new ColoredCircle(2,3,4.5, “green”);
- Print attributes of cCircle as
System.Out.println(cCircle.getXCoordinate()); System.Out.println(cCircle.getYCoordinate());
System.Out.println(cCircle.getRadius());
System.Out.println(cCircle.getColor());
System.out.println("Drawing circle="+cCircle.Draw());
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 1 images