Question

In: Computer Science

Given the following TestDriver.java public class TestDriver {       public static void main(String args[]) {             Point point...

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!");

        }

    }

}

Solutions

Expert Solution

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.


Related Solutions

Given the following TestDriver.java public class TestDriver {       public static void main(String args[]) {             Point point...
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...
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
public class OOPExercises {     public static void main(String[] args) {         A objA = new...
public class OOPExercises {     public static void main(String[] args) {         A objA = new A();         B objB = new B();         System.out.println("in main(): ");         System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());         objA.setA (222);         objB.setB (333.33);       System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());     } } Output: public class A {     int a = 100;     public A() {         System.out.println("in the constructor of class A: ");         System.out.println("a = "+a);         a =...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item below you must code the solution. You may not use any of the // methods found in the Arrays class or the Collections classes // You must use Java's built-in Arrays. You are welcome to use the Math and/or Random class if necessary. // You MUST put your code directly beneath the comment for each item indicated. String[] myData; // 1. Instantiate the given...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
//This is an ArrayReverser Problem.. import java.util.Scanner; public class Main { public static void main(String[] args)...
//This is an ArrayReverser Problem.. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // Read in the list of numbers int[] numbers; String input = sc.nextLine(); if (input.equals("")) { numbers = new int[0]; } else { String[] numberStrings = input.split(" "); numbers = new int[numberStrings.length]; for (int i = 0; i < numberStrings.length; i++) { numbers[i] = Integer.parseInt(numberStrings[i]); } }    // Reverse the list int[] resultArray = reverseArray(numbers);   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT