Question

Write a parent class Rectangle in Java with an area calculation method in dynamic binding and...

Write a parent class Rectangle in Java with an area calculation method in dynamic binding and another area calculation method in static binding. Write a child class Square with an area calculation method that overrides the Rectangle class’s area method. ( in Java use static or final for static binding.) Write a main program that calls both the dynamically bound method and the statically bound method a large number of times (say 1000 times), timing the calls to static binding method as well as to the dynamic binding method respectively. Compare the results and explain the difference.

Homework Answers

Answer #1

Java code for given problem is copied below with relevant comments. It creates two classes, Rectangle and Square (latter extends Rectangle class) and executes their area method under Main class large number of times.

square object we're creating under main of Square class is referenced by superclass, i.e., Rectangle and as such area_dynamic method is dynamically linked at run time while area_static is linked during compile time. Finally the execution timing is displayed as milliseconds and is timed using System.nanoTime() method.

Explanation: Even of multiple runs, it has been observed that static timing is always less than dynamic timing. As static methods are compiled during run time while dynamic one at run time, the latter always encounter overhead which costs the delay at run time and can be seen in our output.

Screenshot and sample run output is also copied at the bottom of this answer.

===============================================================

class Rectangle
{
    //Static method for area calculation
    static double area_static(double length, double breadth)
    {
        return length*breadth;
    }
  
    //Dynamic method for area calculation
    double area_dynamic(double length, double breadth)
    {
        return length*breadth;
    }
}

class Square extends Rectangle
{
    //Overloaded dynamic method for area calculation
    double area_dynamic(double length, double breadth)
    {
        return length*breadth;
    }
}

public class Main
{
   public static void main(String[] args) {
      
       //Declarations
       long startTime, endTime, i;
       double duration;
       double area;
      
       //Create an object of Square class with superclass reference
       Rectangle square = new Square();
      
       //Time the for loop for dynamic calls
       startTime = System.nanoTime();
       for(i=0; i<10000000; i++)
       {
            area = square.area_dynamic(i,i);       //Dynamically linked
       }
       endTime = System.nanoTime();
       duration = (double)(endTime - startTime)/1000000;   //Convert to milliseconds
       //Print execution time for dynamic execution
       System.out.println("Dynamically bound method execution time: " + duration + " ms");
      
       //Time the for loop for static calls
       startTime = System.nanoTime();
       for(i=0; i<10000000; i++)
       {
            area = square.area_static(i,i);         //Statically linked
       }
       endTime = System.nanoTime();
       duration = (double)(endTime - startTime)/1000000;   //Convert to milliseconds
       //Print execution time for static execution
       System.out.println("Statically bound method execution time: " + duration + " ms");
   }
}

===============================================================

Code Screenshot:

===============================================================

Output Screenshots:

Run 1:

Run 2:

Run 3:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(Java) Write the missing equals method needed for the Point class shown on the screen to...
(Java) Write the missing equals method needed for the Point class shown on the screen to work with this program. The missing method would be added to the Point class. class PointTestP3 {   public static void main(String[] args) {    Point p1 = new Point(5, 6);     Point p2 = new Point(5, 6);        System.out.println(p1.equals(p2)); // prints "true"   } }
Java Programing Exercise #3: Write a Java class that implements a static method – SortNumbers(int… numbers)...
Java Programing Exercise #3: Write a Java class that implements a static method – SortNumbers(int… numbers) with variable number of arguments. The method should be called with different numbers of parameters and does arrange the numbers in descending order. Call the method within main method of the driver classand display the results.
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object>...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object> obj) that reverses an ArrayList of any type of object. For example, if an ArrayList held 4 strings: "hi", "hello", "howdy", and "greetings" the order would become "greetings", "howdy", "hello", and "hi". Implement a recursive solution by removing the first object, reversing the ArrayList consisting of the remaining Objects, and combining the two. Use the following class ArrayListReverser to write and test your program....
Write a program in Java to have a Car class which is comparable. Make the Car...
Write a program in Java to have a Car class which is comparable. Make the Car class comparable and use speed to compare cars. Write a method in Main class that finds the minimum of 4 things (generically). Create 4 cars and pass the cars to the minimum method and find the smallest car. Have a toString method for the Car class. The main program should be able to find out how many cars are created so far by calling...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity (sit, walk, jog, bike, swim) and duration in minutes (integer), and returns the estimated calories expended (double). Calories per minute for a 150 lb person (source): sit: 1.4 walk: 5.4 run: 13.0 bike: 6.8 swim: 8.7 If the input is sit 2, the output is 2.8 Hints: Use an if-else statement to determine the calories per minute for the given activity. Return the calories...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods....
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method. Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT