In: Computer Science
Given the following TestDriver.java
public class TestDriver {
public static void main(String args[]) {
Point point = new Point(10, 5);
Circle circle = new Circle(10.5, 20, 19);
Cylinder cylinder = new Cylinder(12, 3.6, 20, 20);
System.out.println("Point");
System.out.println(point.toString()) ;
System.out.println("Circle");
System.out.println(circle.toString());
System.out.println("Cylinder");
System.out.println(cylinder.toString());
}
}
You will get the following result once you execute the TestDriver.java (after compiling it)
Point
(10, 5)
Circle
Center = (20, 19); Radius = 10.5
Cylinder
Center = (20, 20); Radius = 3.6; Height = 12.0
Please fill out the missing part of the following java programs for testDriver is working as above explained.
Please note that you can use multiple statements for an answer for each question.
public class Shape extends 1. _________(5pt) {
public double area() {
return 0;
}
public double volumn() {
return 0.0;
}
}
public class Point extends Shape{
private int x,y;
public Point() {
setPoint(0,0);
}
public Point(int x, int y) {
setPoint(x,y);
}
public void setPoint(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
}
public class Circle extends Point{
protected double radius;
public Circle(){
setRadius(0);
}
public Circle(double cr, int x, int y) {
2.__________(10pt)
}
public void setRadius(double cr) {
radius = (cr >=0 ? cr:0);
}
public double getradius() {
return radius;
}
public double area() {
return Math.PI* radius*radius;
}
public String toString() {
return "Center = " + super.toString() + "; Radius = " + radius;
}
}
public class Cylinder extends Circle{
protected double height;
public Cylinder() {
setHeight(0);
}
public Cylinder(double h, double radius, int x, int y) {
3. __________(10pt)
}
public void setHeight(double h) {
height = (h >= 0 ? h:0);
}
public double area() {
return 4.__________________________(5 pt);
//Assuming the area = 2*PI*r2 +2*Pi*r*height
}
public String toString() {
return super.toString() + "; Height = " + height;
}
}
Assuming that the toString of Circle is missing (completely deleted from Circle class)
What will be output with the following testDriver
5._______________________________________________________(10pt)
public class TestDriver {
public static void main(String args[]) {
Circle circle = new Circle(10.5, 20, 19);
Cylinder cylinder = new Cylinder(12, 3.6, 20, 20);
System.out.println("Circle");
System.out.println(circle.toString());
System.out.println("Cylinder");
System.out.println(cylinder.toString());
}
}
What will be output when the following JVMException is being executed. i.e. what will be prionted as a result of the following JVMExceptuon.jva
6.__________________________________________(10 pt)
public class JVMException {
public static void main(String[] args){
try{
faultyMethod();
} catch (OutOfMemoryError e){
System.out.println(" I am here at Catch");
e.printStackTrace();
fixMemoryLeak();
}
}
public static void faultyMethod(){
System.out.println(" I am here FaultyMethod");
throw new OutOfMemoryError("JVM is out of memory! Fix memory leak!");
}
public static void fixMemoryLeak(){
System.out.println("Fixing memory leak...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
finally {
System.out.println("The memory leak is fixed!");
}
}
}
All files :
************************************ TestDriver.java ***********************************************
public class TestDriver {
public static void main(String[] args) {
Point point = new Point(10, 5);
Circle circle = new Circle(10.5, 20, 19);
Cylinder cylinder = new Cylinder(12, 3.6, 20,
20);
// System.out.println("OUTPUT AFTER REMOVING
(toString()) CLASS FROM CIRCLE CLASSS");
System.out.println("Point");
System.out.println(point.toString()) ;
System.out.println("Circle");
System.out.println(circle.toString());
System.out.println("Cylinder");
System.out.println(cylinder.toString());
}
}
************************* Shape.java FILE *******************************************************
public class Shape extends Object{
public double area() {
return 0;
}
public double volumn() {
return 0.0;
}
}
********************************************* Point.java FILE ************************************************
public class Point extends Shape{
private int x,y;
public Point() {
setPoint(0,0);
}
public Point(int x, int y) {
setPoint(x,y);
}
public void setPoint(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
}
***************************** Circle.java FILE *************************************************************
public class Circle extends Point{
protected double radius;
public Circle(){
setRadius(0);
}
public Circle(double cr, int x, int y) {
super(x,y);
this.radius = cr;
}
public void setRadius(double cr) {
radius = (cr >=0 ? cr:0);
}
public double getradius() {
return radius;
}
public double area() {
return Math.PI* radius*radius;
}
public String toString() {
return "Center = " + super.toString() + "; Radius = " +
radius;
}
}
******************************************* Cylinder.java FILE **********************************************
public class Cylinder extends Circle{
protected double height;
public Cylinder() {
setHeight(0);
}
public Cylinder(double h, double radius, int x, int y) {
super(radius,x,y);
this.height = h;
}
public void setHeight(double h) {
height = (h >= 0 ? h:0);
}
public double area() {
return
super.area()+2*Math.PI*super.getradius()*this.height;
}
public String toString() {
return super.toString() + "; Height = " + height;
}
}
******************** Here is the screenshot of the specified problems ****************************
1. Sample O/P after filling all required blanks:
2. Fill in the blanks (1)
3. Fill in the blanks (2)
4. Fill in the blanks (3 and 4)
4. Output Console after removing toString() method from Circle:
One important point of Inheritence to note here :
* Whenever child class call a method that called method is first checked in childclass itself,if it is found then that defined method of child class will be executed else we will check for that method in upper heirarchy and wherever we counter first that will be executed.
Like here in part 5 first we tried to find out the (toString()) function in Circle class but it was not there, so we check for it parent class (Point) and that (toString()) function of Point class was get executed.
* "super" keyword is used to refer parent class,like we did here in solutions.
If You have any doubt let me know in comment section.
Thanks.