Question

In: Computer Science

This lab is an exercise of using Java’s and C++’s dynamic binding features. The uncompleted programs...

This lab is an exercise of using Java’s and C++’s dynamic binding features. The uncompleted programs to run in Java and in C++ are given below. Note that you need to create a driver class with the main method to run the Java program. Also, you need to write the main function to run the C++ program. Perform the following activities:

  1. predict the output of the Java program.
  2. run the Java program and compare your prediction to the actual output of the program.
  3. Complete the C++ program such that it prints the same output as the Java program does. You need to use the given classes to generate the desired output.
  4. Turn in your Java and C++ program files (.java and .cpp files) in Blackboard Learn.

The Java Program is:

class A

{ public void p()

{ System.out.println(“A.p”); }   public void q()

{ System.out.println(“A.q”); }   public void r()

{ p(); q(); }

}

class B extends A { public void p()

{ System.out.println(“B.p”); }

}

class C extends B

{ public void q()

{ System.out.println(“C.q”); }   public void r()

{ p(); q(); }

}

A a;

C c = new C(); a = c; a.r(); a = new B();

a.r(); a = new C();

a.r();

The C++ Program is:

class A

{ public:

virtual void p()

            { cout << “A.p” << endl; }        void q()          { cout << “A.q” << endl; }    virtual void r()

            { p(); q(); }

};

class B : public A

{ public:

void p()

            { cout << “B.p” << endl; }

}; class C : public B

{ public:

void q()

            { cout << “C.q” << endl; }          void r()

            { p(); q(); }

};

help me pls, i cant run java part. do i need to change something?

Solutions

Expert Solution

JAVA :

class A

{ public void p()

{ System.out.println(“A.p”); 

}  

 public void q()

{ System.out.println(“A.q”); 

}  

 public void r()

{ p();

  q(); 
}

}

class B extends A {

  public void p()

{ System.out.println(“B.p”); 

}

}

class C extends B

{ public void q()

{ System.out.println(“C.q”);

}  

 public void r()

{ p(); 

  q(); 

}

}

class Driver 

{ public static void main(String args[])

{

A a;

C c = new C(); 

a = c;

a.r();

a = new B();

a.r(); 

a = new C();

a.r();

}

}

C++ :

#include<iostream>

using namespace std;

class A

{ public : virtual void p()

            { cout << “A.p” << endl; 

}        

void q()          

{ cout << “A.q” << endl; 

}   

 virtual void r()

            { p();

 q(); 

}

};

class B : public A

{ public : void p()

            { cout << “B.p” << endl; 

}

}; 

class C : public B

{ public : void q()

            { cout << “C.q” << endl;

 }          

void r()

            { p(); 

q(); }

};

int main()

{

A a;

C c;

B b;

c.r();

b.r();

c.r();

}

Common Output of Java & C++ :

B.p

C.q

B.p

A.q

B.p

C.q


Related Solutions

What features of the binding site hinder oxygen binding to Hb? What feature of the binding...
What features of the binding site hinder oxygen binding to Hb? What feature of the binding site facilitates oxygen binding to Hb? Explain the Bohr effect using the theory of allosteric modulation of oxygen binding to hemoglobin.
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
(C++) All programs will be included! This lab gives you a little practice with linked lists...
(C++) All programs will be included! This lab gives you a little practice with linked lists ·Debug insertSorted() and implement sorted() in numberlist.cpp ·Hint: insertSorted() is just missing a few parts. What is in the function can work fine once you add the right code, though you are free to rewrite it ·You need to have main.cpp, numberlist.h and numberlist.cpp in this project, and if you are using Code::Blocks, remember to set the Build Options to indicate you want to...
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
Electrocardiogram Lab (ECG) This Exercise is done using the Ph.I.L.S CD   Do the lab #22. ECG...
Electrocardiogram Lab (ECG) This Exercise is done using the Ph.I.L.S CD   Do the lab #22. ECG and Exercise.Or just click here to there Look at your data and compare each time interval for the resting and post-exercise state. Are any of the time intervals approximately constant? With the exception of the R-R interval, which time interval shows the greatest difference between resting and post-exercise? Explain this observation. If the coronary arteries fill when the heart muscle is relaxed, what happens...
Write a program using c, c++, or java that have four dynamic memory partitions of size...
Write a program using c, c++, or java that have four dynamic memory partitions of size 100 KB, 500 KB, 200 KB, and 450 KB. The program should accept from user the number of processes and their sizes. Then output the assignment of processes using the next fit algorithm (specifying which process, if any, is block).
Write a C++ program using dynamic arrays that allows the user to enter the last names...
Write a C++ program using dynamic arrays that allows the user to enter the last names of the candidates in a local election and the number of votes received by each candidate. The program must ask the user for the number of candidates and then create the appropriate arrays to hold the data. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should...
Transcription factor binding sites. There are a number of software programs that can scan a nucleotide...
Transcription factor binding sites. There are a number of software programs that can scan a nucleotide sequence for responsive elements. Use one of these online software programs to scan the many responsive elements that may bind to the transcription factor YY1 exists in this region?
C++ In this lab you will be using nested for loops to print out stars in...
C++ In this lab you will be using nested for loops to print out stars in a Diamond pattern such as this: * *** ***** ******* ********* *********** ************* *************** ************* *********** ********* ******* ***** *** * For example , if number of rows=8 then the above pattern is derived. You are to take the input for the number of lines(rows) from a file named "input_diamond" and output the pattern into both the terminal and an output file named "output_diamond".
using lunix or C programming to answer this lab please fill in the blanks with the...
using lunix or C programming to answer this lab please fill in the blanks with the answere being highlighted, so i can understand. First, type the following command:             sort employee What is the order that employee is sorted in? ___________________________________________ Give a brief description of how the file is sorted. _____________________________________________________________________________________________________________________________________________________________________________________________________ Now, sort on the field for last name.         sort +1 employee Look at the sorted file. Are all the names sorted in alphabetical order? ______________________ Give a brief description...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT